Organize and restructure controllers
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import Elysia, { NotFoundError, t } from "elysia";
|
||||
import { ActivityPerformanceSchema, PlayerSchema } from "@blade-and-brawn/domain";
|
||||
import { authPlugin } from "../plugins/auth";
|
||||
import { s } from "../services";
|
||||
import { VerificationsService, VerificationStatusSchema } from "../services/verifications";
|
||||
import { ConflictError } from "../util/error";
|
||||
import { AccountIdSchema, BigIntIdSchema } from "../util/models";
|
||||
import { r2 } from "../util/r2";
|
||||
|
||||
const AccountsController = new Elysia({ prefix: "/accounts" })
|
||||
.use(authPlugin)
|
||||
.group("/:id", (app) => app
|
||||
.get("/", async ({ params: { id } }) => {
|
||||
const account = await s.Accounts.get(id);
|
||||
if (!account) throw new NotFoundError("Account not found");
|
||||
return account;
|
||||
}, {
|
||||
authAdmin: true,
|
||||
params: t.Object({ id: AccountIdSchema })
|
||||
})
|
||||
// TODO: for added security, could enforce bot or admin only access
|
||||
.get("/stats", async ({ params: { id } }) => {
|
||||
const stats = await s.Accounts.stats(id);
|
||||
if (!stats) throw new NotFoundError("Account stats not found");
|
||||
return stats;
|
||||
}, {
|
||||
params: t.Object({ id: AccountIdSchema })
|
||||
})
|
||||
// TODO: for added security, MUST enforce bot or admin only access
|
||||
.get("/verifications", async ({ query, params: { id } }) => {
|
||||
return await s.Verifications.list({
|
||||
filter: { status: query.status, accountId: id },
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
});
|
||||
}, {
|
||||
params: t.Object({ id: AccountIdSchema }),
|
||||
query: t.Object({
|
||||
status: t.Optional(VerificationStatusSchema),
|
||||
limit: t.Optional(t.Numeric()),
|
||||
offset: t.Optional(t.Numeric()),
|
||||
})
|
||||
})
|
||||
)
|
||||
.group("/me", { auth: true }, (app) => app
|
||||
.get("/stats", async ({ accountId }) => {
|
||||
const stats = await s.Accounts.stats(accountId);
|
||||
if (!stats) throw new NotFoundError("Account stats not found");
|
||||
return stats;
|
||||
})
|
||||
.group("/assessments", (app) => app
|
||||
.post("/", async ({ body: { player, activityPerformances }, accountId }) => {
|
||||
return await s.Assessments.create(player, activityPerformances, accountId);
|
||||
}, {
|
||||
body: t.Object({
|
||||
player: PlayerSchema,
|
||||
activityPerformances: t.Array(ActivityPerformanceSchema)
|
||||
})
|
||||
})
|
||||
// TODO: handle @ formatted accountIds
|
||||
.get("/", async ({ accountId }) => {
|
||||
return await s.Assessments.list({ filter: { accountId } });
|
||||
})
|
||||
.delete("/:id", async ({ params: { id }, accountId }) => {
|
||||
const deleted = await s.Assessments.delete(id, accountId);
|
||||
if (!deleted) throw new NotFoundError("Assessment not found");
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
)
|
||||
.group("/verifications", (app) => app
|
||||
.get("/", async ({ query, accountId }) => {
|
||||
return await s.Verifications.list({
|
||||
filter: { status: query.status, accountId },
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
});
|
||||
}, {
|
||||
query: t.Object({
|
||||
status: t.Optional(VerificationStatusSchema),
|
||||
limit: t.Optional(t.Numeric()),
|
||||
offset: t.Optional(t.Numeric()),
|
||||
})
|
||||
})
|
||||
.post("/", async ({ body: { assessmentId }, accountId }) => {
|
||||
const assessment = await s.Assessments.get(assessmentId, accountId);
|
||||
if (!assessment) throw new NotFoundError("Assessment not found");
|
||||
|
||||
// TODO: only allow a single active non-completed verification at a time
|
||||
|
||||
const created = await s.Verifications.create(assessmentId);
|
||||
if (!created) throw new ConflictError("This assessment already has a verification");
|
||||
|
||||
return {
|
||||
...created,
|
||||
uploadUrls: VerificationsService.presignMediaUploads(r2.verificationMedia, assessmentId)
|
||||
};
|
||||
}, {
|
||||
body: t.Object({
|
||||
assessmentId: BigIntIdSchema
|
||||
})
|
||||
})
|
||||
.group("/:id", (app) => app
|
||||
.post("/reopen", async ({ params: { id }, accountId }) => {
|
||||
const verification = await s.Verifications.get(id, accountId);
|
||||
if (!verification) throw new NotFoundError("Verification not found");
|
||||
|
||||
const reopened = await s.Verifications.reopen(id);
|
||||
if (!reopened) throw new ConflictError(`Cannot reopen a ${verification.status} verification`);
|
||||
|
||||
return {
|
||||
...reopened,
|
||||
uploadUrls: VerificationsService.presignMediaUploads(r2.verificationMedia, verification.assessment_id)
|
||||
};
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.post("/submit", async ({ params: { id }, accountId }) => {
|
||||
const verification = await s.Verifications.get(id, accountId);
|
||||
if (!verification) throw new NotFoundError("Verification not found");
|
||||
|
||||
if (verification.status === "draft") {
|
||||
const { contents } = await r2.verificationMedia.list({ prefix: `${verification.assessment_id}/`, maxKeys: 1 });
|
||||
if (!contents?.length) throw new ConflictError("No media uploaded");
|
||||
}
|
||||
|
||||
const submitted = await s.Verifications.submit(id);
|
||||
if (submitted) {
|
||||
// TODO: trigger discord webhook here
|
||||
return submitted;
|
||||
}
|
||||
|
||||
const current = await s.Verifications.get(id, accountId);
|
||||
if (!current) throw new NotFoundError("Verification not found");
|
||||
if (current.status === "submitted") return { id: current.id, status: current.status };
|
||||
throw new ConflictError(`Cannot submit a ${current.status} verification`);
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
export default AccountsController;
|
||||
Reference in New Issue
Block a user