Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

projects schema and router + adding to models #270

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import shopRouter from "./services/shop/shop-router";
import staffRouter from "./services/staff/staff-router";
import versionRouter from "./services/version/version-router";
import userRouter from "./services/user/user-router";
import projectRouter from "./services/project/project-router";

// import { InitializeConfigReader } from "./middleware/config-reader";
import { ErrorHandler } from "./middleware/error-handler";
Expand Down Expand Up @@ -63,6 +64,7 @@ app.use("/shop/", database, shopRouter);
app.use("/staff/", database, staffRouter);
app.use("/version/", versionRouter);
app.use("/user/", database, userRouter);
app.use("/project/", database, projectRouter);

// Docs
app.use("/docs/json", async (_req, res) => res.json(await getOpenAPISpec()));
Expand Down
11 changes: 11 additions & 0 deletions src/common/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UserAttendance, UserFollowing, UserInfo } from "../services/user/user-s
import { AnyParamConstructor, IModelOptions } from "@typegoose/typegoose/lib/types";
import { StaffShift } from "../services/staff/staff-schemas";
import { NotificationMappings, NotificationMessages } from "../services/notification/notification-schemas";
import { Project, ProjectMappings } from "../services/project/project-schema";
import { PuzzleItem } from "../services/puzzle/puzzle-schemas";

// Groups for collections
Expand All @@ -24,6 +25,7 @@ export enum Group {
MENTOR = "mentor",
NEWSLETTER = "newsletter",
NOTIFICATION = "notification",
PROJECT = "project",
PUZZLE = "puzzle",
REGISTRATION = "registration",
SHOP = "shop",
Expand Down Expand Up @@ -85,6 +87,11 @@ enum UserCollection {
FOLLOWING = "following",
}

enum ProjectCollection {
PROJECTS = "projects",
MAPPINGS = "mappings",
}

export function generateConfig(collection: string): IModelOptions {
return {
schemaOptions: { collection: collection, versionKey: false },
Expand Down Expand Up @@ -140,6 +147,10 @@ export default class Models {
NotificationCollection.MESSAGES,
);

// Projects
static ProjectInfo: Model<Project> = getModel(Project, Group.PROJECT, ProjectCollection.PROJECTS);
Bahl-Aryan marked this conversation as resolved.
Show resolved Hide resolved
static ProjectMapping: Model<ProjectMappings> = getModel(ProjectMappings, Group.PROJECT, ProjectCollection.MAPPINGS);
Bahl-Aryan marked this conversation as resolved.
Show resolved Hide resolved

// Puzzle
static PuzzleItem: Model<PuzzleItem> = getModel(PuzzleItem, Group.PUZZLE, PuzzleCollection.RUNES_AND_RIDDLES);

Expand Down
1 change: 1 addition & 0 deletions src/middleware/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum Tag {
NEWSLETTER = "Newsletter",
NOTIFICATION = "Notification",
PROFILE = "Profile",
PROJECT = "Project",
PUZZLE = "Puzzle",
REGISTRATION = "Registration",
S3 = "S3",
Expand Down
170 changes: 170 additions & 0 deletions src/services/project/project-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { Router } from "express";
import { StatusCode } from "status-code-enum";
import { Role } from "../auth/auth-schemas";
import specification, { Tag } from "../../middleware/specification";

import {
NoTeamFoundError,
NoTeamFoundErrorSchema,
Project,
ProjectProjectsSchema,
ProjectsSchema,
CreateProjectRequestSchema,
UserAlreadyHasTeamError,
UserAlreadyHasTeamErrorSchema,
//ProjectMappingSchema,
//PathTypeSchema,
//TrackTypeSchema
} from "./project-schema";

//import { EventIdSchema, SuccessResponseSchema } from "../../common/schemas";
import { z } from "zod";
import Models from "../../common/models";
import { getAuthenticatedUser } from "../../common/auth";
import { UserIdSchema } from "../../common/schemas";
//import Config from "../../common/config";
//import crypto from "crypto";

const projectRouter = Router();

// GET details of specific team using ownerId (staff only)
projectRouter.get(
"/owner/:ownerId/",
specification({
method: "get",
path: "/project/owner/{ownerId}/",
tag: Tag.PROJECT,
role: Role.STAFF,
summary: "Retrieve details of a team using ownerId",
parameters: z.object({
ownerId: UserIdSchema,
}),
body: ProjectProjectsSchema,
responses: {
[StatusCode.SuccessOK]: {
description: "The team for this specific ownerId",
schema: ProjectProjectsSchema,
},
[StatusCode.ClientErrorConflict]: {
description: "No team found for this ownerId",
schema: NoTeamFoundErrorSchema,
},
},
}),
async (req, res) => {
const ownerId = req.params.ownerId;
const projectDetails = await Models.ProjectInfo.findOne({ ownerId });

// Return no project found if no details were found
if (!projectDetails) {
return res.status(StatusCode.ClientErrorConflict).send(NoTeamFoundError);
Bahl-Aryan marked this conversation as resolved.
Show resolved Hide resolved
}

return res.status(StatusCode.SuccessOK).send(projectDetails);
},
);

// GET details of user's team
projectRouter.get(
"/details",
specification({
method: "get",
path: "/project/details/",
tag: Tag.PROJECT,
role: null,
summary: "get details of user's team",
body: ProjectProjectsSchema,
responses: {
[StatusCode.SuccessOK]: {
description: "The user's team",
schema: ProjectProjectsSchema,
},
[StatusCode.ClientErrorConflict]: {
Bahl-Aryan marked this conversation as resolved.
Show resolved Hide resolved
description: "No team found for the user",
schema: NoTeamFoundErrorSchema,
},
},
}),
async (req, res) => {
const { id: userId } = getAuthenticatedUser(req);
const userMapping = await Models.ProjectMapping.findOne({ userId });
if (!userMapping) {
return res.status(StatusCode.ClientErrorNotFound).send(NoTeamFoundError);
}
const ownerId = userMapping?.teamOwnerId;

// find project associated with the ownerId
const projectDetails = await Models.ProjectInfo.findOne({ ownerId: ownerId });
Bahl-Aryan marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return a NoTeamFoundError if projectDetails is undefined. Even though that situation should never happen, we want to be comprehensive of all possible error cases.

return res.status(StatusCode.SuccessOK).send(projectDetails?.toObject());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ?

},
);

// POST create project/team
projectRouter.post(
"/create",
specification({
method: "post",
path: "/project/create/",
tag: Tag.PROJECT,
role: null,
summary: "create a new project/team",
body: CreateProjectRequestSchema,
responses: {
[StatusCode.SuccessOK]: {
description: "The new team",
schema: ProjectProjectsSchema,
},
[StatusCode.ClientErrorConflict]: {
description: "The user already has a team",
schema: UserAlreadyHasTeamErrorSchema,
},
},
}),
async (req, res) => {
const { id: userId } = getAuthenticatedUser(req);
const details = req.body;

const project: Project = {
ownerId: userId,
...details,
};

// ensure teamOwner hasn't already made a team
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check that the user isn't already mapped to a team in projectMappings.

const ownerExists = (await Models.ProjectInfo.findOne({ ownerId: userId })) ?? false;
if (ownerExists) {
return res.status(StatusCode.ClientErrorConflict).send(UserAlreadyHasTeamError);
}

const newProject = await Models.ProjectInfo.create(project);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd probably also have logic for adding a projectMappings of the user to themselves, since they should be part of their own group.

return res.status(StatusCode.SuccessCreated).send(newProject);
},
);

// POST join

// GET all projects (STAFF only)
projectRouter.get(
"/list/",
specification({
method: "get",
path: "/project/list/",
tag: Tag.PROJECT,
role: Role.STAFF, //staff only endpoint
summary: "get list of all teams",
body: ProjectsSchema,
responses: {
[StatusCode.SuccessOK]: {
description: "The projects",
schema: ProjectsSchema,
},
},
}),
async (_req, res) => {
const projects = await Models.ProjectInfo.find();
return res.status(StatusCode.SuccessOK).send({ projects: projects });
},
);

export default projectRouter;
113 changes: 113 additions & 0 deletions src/services/project/project-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { z } from "zod";
import { prop } from "@typegoose/typegoose";
import { UserIdSchema } from "../../common/schemas";
import { CreateErrorAndSchema } from "../../common/schemas";

// path enum
export enum PathType {
BEGINNER = "BEGINNER",
GENERAL = "GENERAL",
PRO = "PRO",
}
export const PathTypeSchema = z.nativeEnum(PathType);

// track enum
export enum TrackType {
SPONSOR_1 = "SPONSOR1",
SPONSOR_2 = "SPONSOR2",
SPONSOR_3 = "SPONSOR3",
}
export const TrackTypeSchema = z.nativeEnum(TrackType);

// general interface for a project
export class Project {
@prop({ required: true })
public projectName: string;

@prop({ required: true })
public ownerId: string;

@prop({
required: true,
type: () => String,
})
public teamMembers: string[];

@prop({
required: true,
enum: PathType,
})
public path: PathType;

@prop({
required: true,
enum: TrackType,
})
public track: TrackType;

@prop({ required: true })
public githubLink: string;

@prop({ required: true })
public videoLink: string;

@prop({ required: true })
public accessCode: string;

@prop({ required: false })
public description: string;
}

// interface for ownerId to userId mapping
export class ProjectMappings {
@prop({ required: true })
public teamOwnerId: string;

@prop({ required: true })
public userId: string;
}

export const ProjectProjectsSchema = z
.object({
projectName: z.string(),
ownerId: UserIdSchema,
teamMembers: z.array(UserIdSchema),
path: PathTypeSchema,
track: TrackTypeSchema,
githubLink: z.string(),
videoLink: z.string(),
accessCode: z.string(),
description: z.string(),
})
.openapi("ProjectSchema", {
description: "Information about a project",
});

export const CreateProjectRequestSchema = ProjectProjectsSchema.omit({ ownerId: true }).openapi("CreateProjectRequest"); // add example after

export const ProjectMappingsSchema = z
.object({
teamOwnerId: UserIdSchema,
userId: UserIdSchema,
})
.openapi("ProjectMappingSchema", {
description: "A user's team/teamOwnerId",
});

export const ProjectsSchema = z
.object({
projects: z.array(ProjectProjectsSchema),
})
.openapi("ProjectsSchema", {
description: "all projects",
});

export const [UserAlreadyHasTeamError, UserAlreadyHasTeamErrorSchema] = CreateErrorAndSchema({
error: "AlreadyHasTeam",
message: "This user already has a team!",
});

export const [NoTeamFoundError, NoTeamFoundErrorSchema] = CreateErrorAndSchema({
error: "NoTeamFound",
message: "No team was found for this user",
});
Loading