Skip to content

Commit

Permalink
added more routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan-Bahl committed Nov 3, 2024
1 parent 0d1049f commit 38e73e0
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/common/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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 } from "../services/project/project-schema";
import { Project, ProjectMapping } from "../services/project/project-schema";
import { PuzzleItem } from "../services/puzzle/puzzle-schemas";

// Groups for collections
Expand Down Expand Up @@ -88,7 +88,8 @@ enum UserCollection {
}

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

export function generateConfig(collection: string): IModelOptions {
Expand Down Expand Up @@ -148,6 +149,7 @@ export default class Models {

// Projects
static ProjectInfo: Model<Project> = getModel(Project, Group.PROJECT, ProjectCollection.INFO);
static ProjectMapping: Model<ProjectMapping> = getModel(ProjectMapping, Group.PROJECT, ProjectCollection.MAPPINGS);

// Puzzle
static PuzzleItem: Model<PuzzleItem> = getModel(PuzzleItem, Group.PUZZLE, PuzzleCollection.RUNES_AND_RIDDLES);
Expand Down
127 changes: 123 additions & 4 deletions src/services/project/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,146 @@ import { Role } from "../auth/auth-schemas";
import specification, { Tag } from "../../middleware/specification";

import {
//ProjectSchema,
NoTeamFoundError,
NoTeamFoundErrorSchema,
Project,
ProjectSchema,
ProjectsSchema,
UserAlreadyHasTeamError,
UserAlreadyHasTeamErrorSchema,
//ProjectMappingSchema,
//PathTypeSchema,
//TrackTypeSchema
} from './project-schema'

//import { EventIdSchema, SuccessResponseSchema } from "../../common/schemas";
//import { z } from "zod";
import { z } from "zod";
import Models from "../../common/models";
//import { tryGetAuthenticatedUser } from "../../common/auth";
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
}),
responses: {
[StatusCode.SuccessOK]: {
description: "The team for this specific ownerId",
schema: ProjectSchema,
},
[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);
}

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",
responses: {
[StatusCode.SuccessOK]: {
description: "The user's team",
schema: ProjectSchema,
},
[StatusCode.ClientErrorConflict]: {
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}) as Project;

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

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

const project: Project = {
...details,
}

// ensure teamOwner hasn't already made a team
const ownerExists = (await Models.ProjectInfo.findOne({ ownerId: details.ownerId })) ?? false
if (ownerExists) {
return res.status(StatusCode.ClientErrorConflict).send(UserAlreadyHasTeamError);
}

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

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

// POST join

// GET all projects (STAFF only)
projectRouter.get(
"/list/",
specification({
method: "get",
path: "/projects/list/",
path: "/project/list/",
tag: Tag.PROJECT,
role: Role.STAFF, //staff only endpoint
summary: "get list of all teams",
Expand Down
13 changes: 12 additions & 1 deletion src/services/project/project-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from "zod";
import { prop } from "@typegoose/typegoose";
import { UserIdSchema } from "../../common/schemas";
import { CreateErrorAndSchema } from "../../common/schemas";

// path enum
export enum PathType {
Expand Down Expand Up @@ -98,4 +99,14 @@ export const ProjectsSchema = z
})
.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",
});

0 comments on commit 38e73e0

Please sign in to comment.