More cleanup
This commit is contained in:
+9
-11
@@ -2,7 +2,6 @@ import {
|
|||||||
ActivityPerformanceSchema,
|
ActivityPerformanceSchema,
|
||||||
ActivityVerificationsSchema,
|
ActivityVerificationsSchema,
|
||||||
PlayerSchema,
|
PlayerSchema,
|
||||||
Activity,
|
|
||||||
} from "@blade-and-brawn/domain"
|
} from "@blade-and-brawn/domain"
|
||||||
import { cors } from "@elysiajs/cors";
|
import { cors } from "@elysiajs/cors";
|
||||||
import { Elysia, NotFoundError, redirect, status, t } from "elysia";
|
import { Elysia, NotFoundError, redirect, status, t } from "elysia";
|
||||||
@@ -33,7 +32,6 @@ import { VerificationsService, VerificationStatusSchema } from "./services/verif
|
|||||||
const EVENT_QUEUE_MANAGE_DELAY_MS = 1000 // 1 sec
|
const EVENT_QUEUE_MANAGE_DELAY_MS = 1000 // 1 sec
|
||||||
const JWT_EXP = "1d";
|
const JWT_EXP = "1d";
|
||||||
const JWT_EXP_SECONDS = 60 * 60 * 24; // keep in sync with JWT_EXP; used for cookie maxAge
|
const JWT_EXP_SECONDS = 60 * 60 * 24; // keep in sync with JWT_EXP; used for cookie maxAge
|
||||||
const PG_UNIQUE_VIOLATION = "23505";
|
|
||||||
|
|
||||||
// SERVICES
|
// SERVICES
|
||||||
// -----------------------
|
// -----------------------
|
||||||
@@ -59,12 +57,6 @@ const r2 = {
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
const presignVerificationUploads = (assessmentId: string) =>
|
|
||||||
Object.values(Activity).map(() => r2.verificationMedia.presign(`${assessmentId}/${crypto.randomUUID()}`, {
|
|
||||||
method: "PUT",
|
|
||||||
expiresIn: 30 * 60,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// QUEUES
|
// QUEUES
|
||||||
// -----------------------
|
// -----------------------
|
||||||
const queues = [
|
const queues = [
|
||||||
@@ -122,7 +114,7 @@ export const app = new Elysia()
|
|||||||
.onError(({ code, error }) => {
|
.onError(({ code, error }) => {
|
||||||
// Database errors
|
// Database errors
|
||||||
if (error instanceof DatabaseError) {
|
if (error instanceof DatabaseError) {
|
||||||
if (error.code === PG_UNIQUE_VIOLATION)
|
if (error.code === "23505")
|
||||||
return status(409, { error: "Conflicts with an existing record" });
|
return status(409, { error: "Conflicts with an existing record" });
|
||||||
log.error({ err: error }, "Database error");
|
log.error({ err: error }, "Database error");
|
||||||
return status(500, { error: "Internal Server Error" });
|
return status(500, { error: "Internal Server Error" });
|
||||||
@@ -557,7 +549,10 @@ export const app = new Elysia()
|
|||||||
const created = await s.Verifications.create(assessmentId);
|
const created = await s.Verifications.create(assessmentId);
|
||||||
if (!created) throw new ConflictError("This assessment already has a verification");
|
if (!created) throw new ConflictError("This assessment already has a verification");
|
||||||
|
|
||||||
return { ...created, uploadUrls: presignVerificationUploads(assessmentId) };
|
return {
|
||||||
|
...created,
|
||||||
|
uploadUrls: VerificationsService.presignMediaUploads(r2.verificationMedia, assessmentId)
|
||||||
|
};
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
assessmentId: BigIntIdSchema
|
assessmentId: BigIntIdSchema
|
||||||
@@ -570,7 +565,10 @@ export const app = new Elysia()
|
|||||||
const reopened = await s.Verifications.reopen(id);
|
const reopened = await s.Verifications.reopen(id);
|
||||||
if (!reopened) throw new ConflictError(`Cannot reopen a ${verification.status} verification`);
|
if (!reopened) throw new ConflictError(`Cannot reopen a ${verification.status} verification`);
|
||||||
|
|
||||||
return { ...reopened, uploadUrls: presignVerificationUploads(verification.assessment_id) };
|
return {
|
||||||
|
...reopened,
|
||||||
|
uploadUrls: VerificationsService.presignMediaUploads(r2.verificationMedia, verification.assessment_id)
|
||||||
|
};
|
||||||
}, {
|
}, {
|
||||||
params: t.Object({ id: BigIntIdSchema })
|
params: t.Object({ id: BigIntIdSchema })
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type { Static } from "@sinclair/typebox";
|
|||||||
import { t } from "elysia";
|
import { t } from "elysia";
|
||||||
import { db } from "../database/db";
|
import { db } from "../database/db";
|
||||||
import { AccountsService } from "./accounts";
|
import { AccountsService } from "./accounts";
|
||||||
|
import { S3Client } from "bun";
|
||||||
|
|
||||||
export const VerificationStatusSchema = t.Union([
|
export const VerificationStatusSchema = t.Union([
|
||||||
t.Literal("draft"),
|
t.Literal("draft"),
|
||||||
@@ -21,6 +22,13 @@ const ALLOWED_FROM = {
|
|||||||
} satisfies Record<string, VerificationStatus[]>;
|
} satisfies Record<string, VerificationStatus[]>;
|
||||||
|
|
||||||
export class VerificationsService {
|
export class VerificationsService {
|
||||||
|
public static presignMediaUploads(s3Client: S3Client, assessmentId: string) {
|
||||||
|
return Object.values(Activity).map(() => s3Client.presign(`${assessmentId}/${crypto.randomUUID()}`, {
|
||||||
|
method: "PUT",
|
||||||
|
expiresIn: 30 * 60,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
async create(assessmentId: string) {
|
async create(assessmentId: string) {
|
||||||
return await db.insertInto("verifications")
|
return await db.insertInto("verifications")
|
||||||
.values({ assessment_id: assessmentId, status: "draft" satisfies VerificationStatus })
|
.values({ assessment_id: assessmentId, status: "draft" satisfies VerificationStatus })
|
||||||
@@ -29,15 +37,6 @@ export class VerificationsService {
|
|||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
async reopen(id: string) {
|
|
||||||
return await db.updateTable("verifications")
|
|
||||||
.where("id", "=", id)
|
|
||||||
.where("status", "in", ALLOWED_FROM.reopen)
|
|
||||||
.set({ status: "draft" satisfies VerificationStatus, submitted_at: null })
|
|
||||||
.returning(["id", "status"])
|
|
||||||
.executeTakeFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
async get(id: string, accountId?: string) {
|
async get(id: string, accountId?: string) {
|
||||||
return await VerificationsService.baseQuery()
|
return await VerificationsService.baseQuery()
|
||||||
.where("verifications.id", "=", id)
|
.where("verifications.id", "=", id)
|
||||||
@@ -110,6 +109,15 @@ export class VerificationsService {
|
|||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async reopen(id: string) {
|
||||||
|
return await db.updateTable("verifications")
|
||||||
|
.where("id", "=", id)
|
||||||
|
.where("status", "in", ALLOWED_FROM.reopen)
|
||||||
|
.set({ status: "draft" satisfies VerificationStatus, submitted_at: null })
|
||||||
|
.returning(["id", "status"])
|
||||||
|
.executeTakeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
private static accountIdQuery(accountId: string) {
|
private static accountIdQuery(accountId: string) {
|
||||||
return db.selectFrom("accounts")
|
return db.selectFrom("accounts")
|
||||||
.select("id")
|
.select("id")
|
||||||
|
|||||||
Reference in New Issue
Block a user