More cleanup
This commit is contained in:
+9
-11
@@ -2,7 +2,6 @@ import {
|
||||
ActivityPerformanceSchema,
|
||||
ActivityVerificationsSchema,
|
||||
PlayerSchema,
|
||||
Activity,
|
||||
} from "@blade-and-brawn/domain"
|
||||
import { cors } from "@elysiajs/cors";
|
||||
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 JWT_EXP = "1d";
|
||||
const JWT_EXP_SECONDS = 60 * 60 * 24; // keep in sync with JWT_EXP; used for cookie maxAge
|
||||
const PG_UNIQUE_VIOLATION = "23505";
|
||||
|
||||
// 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
|
||||
// -----------------------
|
||||
const queues = [
|
||||
@@ -122,7 +114,7 @@ export const app = new Elysia()
|
||||
.onError(({ code, error }) => {
|
||||
// Database errors
|
||||
if (error instanceof DatabaseError) {
|
||||
if (error.code === PG_UNIQUE_VIOLATION)
|
||||
if (error.code === "23505")
|
||||
return status(409, { error: "Conflicts with an existing record" });
|
||||
log.error({ err: error }, "Database error");
|
||||
return status(500, { error: "Internal Server Error" });
|
||||
@@ -557,7 +549,10 @@ export const app = new Elysia()
|
||||
const created = await s.Verifications.create(assessmentId);
|
||||
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({
|
||||
assessmentId: BigIntIdSchema
|
||||
@@ -570,7 +565,10 @@ export const app = new Elysia()
|
||||
const reopened = await s.Verifications.reopen(id);
|
||||
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 })
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Static } from "@sinclair/typebox";
|
||||
import { t } from "elysia";
|
||||
import { db } from "../database/db";
|
||||
import { AccountsService } from "./accounts";
|
||||
import { S3Client } from "bun";
|
||||
|
||||
export const VerificationStatusSchema = t.Union([
|
||||
t.Literal("draft"),
|
||||
@@ -21,6 +22,13 @@ const ALLOWED_FROM = {
|
||||
} satisfies Record<string, VerificationStatus[]>;
|
||||
|
||||
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) {
|
||||
return await db.insertInto("verifications")
|
||||
.values({ assessment_id: assessmentId, status: "draft" satisfies VerificationStatus })
|
||||
@@ -29,15 +37,6 @@ export class VerificationsService {
|
||||
.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) {
|
||||
return await VerificationsService.baseQuery()
|
||||
.where("verifications.id", "=", id)
|
||||
@@ -110,6 +109,15 @@ export class VerificationsService {
|
||||
.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) {
|
||||
return db.selectFrom("accounts")
|
||||
.select("id")
|
||||
|
||||
Reference in New Issue
Block a user