83 lines
3.9 KiB
TypeScript
83 lines
3.9 KiB
TypeScript
import Elysia, { NotFoundError, t } from "elysia";
|
|
import { ActivityVerificationsSchema } from "@blade-and-brawn/domain";
|
|
import { authPlugin } from "../plugins/auth";
|
|
import { s } from "../services";
|
|
import { VerificationStatusSchema } from "../services/verifications";
|
|
import { ConflictError, ForbiddenError } from "../util/error";
|
|
import { AccountIdSchema, BigIntIdSchema } from "../util/models";
|
|
import { r2 } from "../util/r2";
|
|
|
|
const VerificationsController = new Elysia({ prefix: "/verifications" })
|
|
.use(authPlugin)
|
|
.get("/", async ({ query }) => {
|
|
return await s.Verifications.list({
|
|
filter: { status: query.status },
|
|
limit: query.limit,
|
|
offset: query.offset,
|
|
});
|
|
}, {
|
|
authAdmin: true,
|
|
query: t.Object({
|
|
status: t.Optional(VerificationStatusSchema),
|
|
limit: t.Optional(t.Numeric()),
|
|
offset: t.Optional(t.Numeric()),
|
|
})
|
|
})
|
|
.group("/:id", (app) => app
|
|
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth
|
|
.get("/", async ({ params: { id } }) => {
|
|
const verification = await s.Verifications.get(id);
|
|
if (!verification) throw new NotFoundError("Verification not found");
|
|
|
|
const { contents = [] } = await r2.verificationMedia.list({ prefix: `${verification.assessment_id}/` });
|
|
const media = contents.map(({ key, size, lastModified }) => ({
|
|
key,
|
|
size,
|
|
lastModified,
|
|
url: r2.verificationMedia.presign(key, { expiresIn: 60 * 60 }),
|
|
}));
|
|
|
|
return { ...verification, media };
|
|
}, {
|
|
params: t.Object({ id: BigIntIdSchema })
|
|
})
|
|
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth
|
|
.post("/request-action", async ({ params: { id }, body: { reviewerNotes, activityVerifications } }) => {
|
|
const updated = await s.Verifications.requestAction(id, reviewerNotes ?? null, activityVerifications);
|
|
if (updated) return updated;
|
|
|
|
const verification = await s.Verifications.get(id);
|
|
if (!verification) throw new NotFoundError("Verification not found");
|
|
throw new ConflictError(`Cannot request action on a ${verification.status} verification`);
|
|
}, {
|
|
params: t.Object({ id: BigIntIdSchema }),
|
|
body: t.Object({
|
|
reviewerNotes: t.Optional(t.String()),
|
|
activityVerifications: ActivityVerificationsSchema,
|
|
})
|
|
})
|
|
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth and drop completedBy from the body
|
|
.post("/complete", async ({ params: { id }, body: { activityVerifications, completedBy }, jwt, cookie: { auth } }) => {
|
|
const portalSession = auth.value && await jwt.verify(auth.value);
|
|
const completerId = completedBy ?? (portalSession ? portalSession.accountId?.toString() : undefined);
|
|
const completer = completerId && await s.Accounts.get(completerId);
|
|
if (!completer || completer.role !== "admin") throw new ForbiddenError("Only a classmaster can complete a verification");
|
|
|
|
const updated = await s.Verifications.complete(id, completer.id, activityVerifications);
|
|
if (updated) return updated;
|
|
|
|
const verification = await s.Verifications.get(id);
|
|
if (!verification) throw new NotFoundError("Verification not found");
|
|
throw new ConflictError(`Cannot complete a ${verification.status} verification`);
|
|
}, {
|
|
params: t.Object({ id: BigIntIdSchema }),
|
|
body: t.Object({
|
|
activityVerifications: ActivityVerificationsSchema,
|
|
completedBy: t.Optional(AccountIdSchema),
|
|
}),
|
|
cookie: t.Cookie({ auth: t.Optional(t.String()) })
|
|
})
|
|
);
|
|
|
|
export default VerificationsController;
|