Skip to content

Commit

Permalink
Start quest routes
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobWrenn committed Oct 20, 2024
1 parent c3bc49c commit 39af987
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Team = Prisma.TeamGetPayload<{ select: undefined }> & {
}
export type User = Prisma.UserGetPayload<{ select: undefined }>
export type TokenSet = Prisma.TokenSetGetPayload<{ select: undefined }>
export { QRCodes_category } from "@prisma/client"
export { QRCodes_category, Quest_dependency_mode } from "@prisma/client"

const basePrisma = new PrismaClient()
export const prisma = basePrisma.$extends({
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/qr-codes/qr-codes-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert/strict"
import { ClientError, HttpStatus, ServerError } from "@otterhttp/errors"
import { ClientError, HttpStatus } from "@otterhttp/errors"
import { v7 as uuid } from "uuid"
import { z } from "zod"

Expand Down
58 changes: 54 additions & 4 deletions server/src/routes/quests/quests-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import { HttpStatus, ServerError } from "@otterhttp/errors"
import { z } from "zod"

import { requireUserHasOne, requireUserIsAdmin } from "@server/common/decorators"
import { UserRole } from "@server/common/model-enums"
import type { Middleware, Request, Response } from "@server/types"
import { prisma, Quest_dependency_mode } from "@server/database"

class QuestsHandlers {
@requireUserIsAdmin()
getQuestListAdmin(): Middleware {
return (request: Request, response: Response) => {
throw new ServerError("", { statusCode: HttpStatus.NotImplemented, expected: true })
return async (_request: Request, response: Response): Promise<void> => {
// todo: this needs to be paginated
const result = await prisma.quest.findMany({
include: { challenges: { select: { challengeId: true, name: true } } },
orderBy: { createdAt: "desc" },
})

const payload = result.map((quest) => ({
id: quest.questId,
name: quest.name,
description: quest.description,
dependency_mode: quest.dependencyMode,
value: quest.points,
challenges: quest.challenges.map(challenge => ({
id: challenge.challengeId,
name: challenge.name
}))
}))

response.status(200)
response.json({
status: 200,
message: "OK",
quests: payload,
})
}
}

Expand All @@ -19,16 +44,41 @@ class QuestsHandlers {
}
}

static createQuestPayloadSchema = z.object({
name: z.string(),
description: z.string().optional(),
dependencyMode: z.nativeEnum(Quest_dependency_mode),
points: z.number().nonnegative().optional(),
challenges: z.array(z.number()),
})

@requireUserIsAdmin()
createQuest(): Middleware {
return (request: Request, response: Response) => {
throw new ServerError("", { statusCode: HttpStatus.NotImplemented, expected: true })
return async (request: Request, response: Response) => {
const create_attributes = QuestsHandlers.createQuestPayloadSchema.parse(request.body)

const new_instance = await prisma.quest.create({
data: {
...create_attributes,
challenges: { connect: create_attributes.challenges.map((id) => ({ challengeId: id })) },
},
});

response.status(200)
response.json({
status: response.statusCode,
message: "OK",
data: { ...new_instance },
})
}
}

@requireUserIsAdmin()
patchQuestDetails(): Middleware {
return (request: Request, response: Response) => {
const { quest_id } = response.locals
if (typeof quest_id !== "number") throw new Error("Parsed `quest_id` not found.")

throw new ServerError("", { statusCode: HttpStatus.NotImplemented, expected: true })
}
}
Expand Down

0 comments on commit 39af987

Please sign in to comment.