99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
import { Activity, Gender, inToCm, lbToKg, minToMs, secToMs } from "@blade-and-brawn/domain";
|
|
import { db } from "../database/db";
|
|
import { env, log } from "../util";
|
|
import type { AccountRole } from "../services/accounts";
|
|
import { VerificationsService } from "../services/verifications";
|
|
|
|
const TEST_DISCORD_ID = "test-verification-seed";
|
|
const TEST_ASSESSMENT_NAME = "Test Verification Player";
|
|
|
|
async function seedTestAccount(): Promise<string> {
|
|
const existing = await db.selectFrom("accounts")
|
|
.select("id")
|
|
.where("discord_id", "=", TEST_DISCORD_ID)
|
|
.executeTakeFirst();
|
|
if (existing) {
|
|
log.info({ id: existing.id }, "test account already seeded, reusing");
|
|
return existing.id;
|
|
}
|
|
|
|
const account = await db.insertInto("accounts")
|
|
.values({
|
|
discord_id: TEST_DISCORD_ID,
|
|
name: "Test Player",
|
|
role: "user" satisfies AccountRole,
|
|
})
|
|
.returning("id")
|
|
.executeTakeFirstOrThrow();
|
|
log.info({ id: account.id }, "seeded test account");
|
|
return account.id;
|
|
}
|
|
|
|
async function seedTestAssessment(accountId: string): Promise<string> {
|
|
const existing = await db.selectFrom("assessments")
|
|
.select("id")
|
|
.where("account_id", "=", accountId)
|
|
.where("name", "=", TEST_ASSESSMENT_NAME)
|
|
.executeTakeFirst();
|
|
if (existing) {
|
|
log.info({ id: existing.id }, "test assessment already seeded, reusing");
|
|
return existing.id;
|
|
}
|
|
|
|
const result = await db.insertInto("assessments")
|
|
.values({
|
|
account_id: accountId,
|
|
name: TEST_ASSESSMENT_NAME,
|
|
gender: Gender.Male,
|
|
age: 22,
|
|
weight: lbToKg(180),
|
|
perf_back_squat: lbToKg(225),
|
|
perf_deadlift: lbToKg(315),
|
|
perf_bench_press: lbToKg(185),
|
|
perf_broad_jump: inToCm(96),
|
|
perf_run: minToMs(7) + secToMs(30),
|
|
perf_cone_drill: secToMs(7),
|
|
})
|
|
.returning("id")
|
|
.executeTakeFirstOrThrow();
|
|
log.info({ id: result.id }, "seeded test assessment");
|
|
return result.id;
|
|
}
|
|
|
|
(async () => {
|
|
const answer = prompt(`Seed a test verification request in the database (${env.DATABASE_URL})? (y/N)`);
|
|
if (answer?.trim().toLowerCase() !== "y") {
|
|
log.info("Aborted");
|
|
await db.destroy();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const accountId = await seedTestAccount();
|
|
const assessmentId = await seedTestAssessment(accountId);
|
|
|
|
const verifications = new VerificationsService();
|
|
const verification = await verifications.create(assessmentId, {
|
|
[Activity.BackSquat]: `verifications/${assessmentId}/back-squat.mp4`,
|
|
[Activity.Deadlift]: `verifications/${assessmentId}/deadlift.mp4`,
|
|
[Activity.BenchPress]: `verifications/${assessmentId}/bench-press.mp4`,
|
|
[Activity.Run]: `verifications/${assessmentId}/run.png`,
|
|
[Activity.BroadJump]: `verifications/${assessmentId}/broad-jump.mp4`,
|
|
[Activity.ConeDrill]: `verifications/${assessmentId}/cone-drill.mp4`,
|
|
});
|
|
if (!verification) throw new Error("Test assessment already has a verification; clear the verifications table first");
|
|
|
|
const submitted = await verifications.submit(verification.id);
|
|
if (!submitted) throw new Error("Failed to submit test verification");
|
|
|
|
log.info({ id: submitted.id, status: submitted.status, assessmentId, accountId }, "seeded test verification request");
|
|
}
|
|
catch (err) {
|
|
log.error({ err }, "seed failed");
|
|
process.exit(1);
|
|
}
|
|
finally {
|
|
await db.destroy();
|
|
}
|
|
})();
|