Implement .verify bot command
This commit is contained in:
@@ -8,7 +8,7 @@ import { env, log } from "../util";
|
||||
import type { AccountRole } from "../services/accounts";
|
||||
import { VerificationsService } from "../services/verifications";
|
||||
|
||||
const DEFAULT_DISCORD_ID = "test-verification-seed";
|
||||
const DEFAULT_DISCORD_ID = "1325552430942916731";
|
||||
const FIXTURES_DIR = path.join(import.meta.dir, "fixtures");
|
||||
const BUCKET = `verification-media${env.NODE_ENV === "production" ? "" : "-dev"}`;
|
||||
|
||||
|
||||
+41
-3
@@ -490,6 +490,21 @@ export const app = new Elysia()
|
||||
}, {
|
||||
params: t.Object({ id: AccountIdSchema })
|
||||
})
|
||||
// TODO: for added security, MUST enforce bot or admin only access
|
||||
.get("/:id/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()),
|
||||
})
|
||||
})
|
||||
.guard({ authAdmin: true }, (app) => app
|
||||
.get("/:id", async ({ params: { id } }) => {
|
||||
const account = await s.Accounts.get(id);
|
||||
@@ -523,6 +538,19 @@ export const app = new Elysia()
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.get("/verifications", 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("/verifications", async ({ body: { assessmentId }, accountId }) => {
|
||||
const assessment = await s.Assessments.get(assessmentId, accountId);
|
||||
if (!assessment) return status(404, { error: "Assessment not found" });
|
||||
@@ -599,7 +627,7 @@ export const app = new Elysia()
|
||||
)
|
||||
|
||||
// VERIFICATIONS
|
||||
.group("/verifications", { authAdmin: true }, (app) => app
|
||||
.group("/verifications", (app) => app
|
||||
.get("/", async ({ query }) => {
|
||||
return await s.Verifications.list({
|
||||
filter: { status: query.status },
|
||||
@@ -607,12 +635,14 @@ export const app = new Elysia()
|
||||
offset: query.offset,
|
||||
});
|
||||
}, {
|
||||
authAdmin: true,
|
||||
query: t.Object({
|
||||
status: t.Optional(VerificationStatusSchema),
|
||||
limit: t.Optional(t.Numeric()),
|
||||
offset: t.Optional(t.Numeric()),
|
||||
})
|
||||
})
|
||||
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth
|
||||
.get("/:id", async ({ params: { id } }) => {
|
||||
const verification = await s.Verifications.get(id);
|
||||
if (!verification) return status(404, { error: "Verification not found" });
|
||||
@@ -629,6 +659,7 @@ export const app = new Elysia()
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth
|
||||
.post("/:id/request-action", async ({ params: { id }, body: { reviewerNotes, activityVerifications } }) => {
|
||||
const updated = await s.Verifications.requestAction(id, reviewerNotes ?? null, activityVerifications);
|
||||
if (updated) return updated;
|
||||
@@ -643,8 +674,14 @@ export const app = new Elysia()
|
||||
activityVerifications: ActivityVerificationsSchema,
|
||||
})
|
||||
})
|
||||
.post("/:id/complete", async ({ params: { id }, body: { activityVerifications }, accountId }) => {
|
||||
const updated = await s.Verifications.complete(id, accountId, activityVerifications);
|
||||
// TODO: dev-only, unguarded so the bot can call it; restore admin/bot auth and drop completedBy from the body
|
||||
.post("/:id/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") return status(403, { error: "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);
|
||||
@@ -654,6 +691,7 @@ export const app = new Elysia()
|
||||
params: t.Object({ id: BigIntIdSchema }),
|
||||
body: t.Object({
|
||||
activityVerifications: ActivityVerificationsSchema,
|
||||
completedBy: t.Optional(AccountIdSchema),
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
@@ -9,7 +9,7 @@ export type AccountRole = "user" | "admin";
|
||||
export class AccountsService {
|
||||
constructor(private Calculator: CalculatorService) { }
|
||||
|
||||
private static idComparison(id: string): ["accounts.id" | "accounts.discord_id", "=", string] {
|
||||
static idComparison(id: string): ["accounts.id" | "accounts.discord_id", "=", string] {
|
||||
return id.startsWith("@")
|
||||
? ["accounts.discord_id", "=", id.slice(1)]
|
||||
: ["accounts.id", "=", id];
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Activity, type ActivityVerifications } from "@blade-and-brawn/domain";
|
||||
import type { Static } from "@sinclair/typebox";
|
||||
import { t } from "elysia";
|
||||
import { db } from "../database/db";
|
||||
import { AccountsService } from "./accounts";
|
||||
|
||||
export const VerificationStatusSchema = t.Union([
|
||||
t.Literal("draft"),
|
||||
@@ -40,12 +41,12 @@ export class VerificationsService {
|
||||
async get(id: string, accountId?: string) {
|
||||
return await VerificationsService.baseQuery()
|
||||
.where("verifications.id", "=", id)
|
||||
.$if(accountId !== undefined, (qb) => qb.where("assessments.account_id", "=", accountId!))
|
||||
.$if(accountId !== undefined, (qb) => qb.where("assessments.account_id", "=", VerificationsService.accountIdQuery(accountId!)))
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async list(opt: {
|
||||
filter?: { status?: VerificationStatus },
|
||||
filter?: { status?: VerificationStatus, accountId?: string },
|
||||
limit?: number,
|
||||
offset?: number,
|
||||
} = {}) {
|
||||
@@ -53,6 +54,9 @@ export class VerificationsService {
|
||||
.$if(opt.filter?.status !== undefined, (qb) => qb
|
||||
.where("verifications.status", "=", opt.filter!.status!)
|
||||
)
|
||||
.$if(opt.filter?.accountId !== undefined, (qb) => qb
|
||||
.where("assessments.account_id", "=", VerificationsService.accountIdQuery(opt.filter!.accountId!))
|
||||
)
|
||||
.orderBy("verifications.submitted_at", (ob) => ob.desc().nullsLast())
|
||||
.orderBy("verifications.created_at", "desc")
|
||||
.$if(opt.limit !== undefined, (qb) => qb.limit(opt.limit!))
|
||||
@@ -106,6 +110,12 @@ export class VerificationsService {
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
private static accountIdQuery(accountId: string) {
|
||||
return db.selectFrom("accounts")
|
||||
.select("id")
|
||||
.where(...AccountsService.idComparison(accountId));
|
||||
}
|
||||
|
||||
private static baseQuery() {
|
||||
return db.selectFrom("verifications")
|
||||
.innerJoin("assessments", "assessments.id", "verifications.assessment_id")
|
||||
|
||||
Reference in New Issue
Block a user