import { Activity, type ActivityPerformance, type Player } from "@blade-and-brawn/domain"; import { db } from "../database/db"; export class AssessmentsService { async create(player: Player, activityPerformances: ActivityPerformance[], accountId?: string) { const performanceFor = (activity: Activity) => activityPerformances.find((p) => p.activity === activity)?.performance ?? null; return await db.insertInto("assessments") .values({ account_id: accountId ?? null, name: player.name ?? "Anonymous", age: player.metrics.age, weight: player.metrics.weight, gender: player.metrics.gender, perf_back_squat: performanceFor(Activity.BackSquat), perf_deadlift: performanceFor(Activity.Deadlift), perf_bench_press: performanceFor(Activity.BenchPress), perf_broad_jump: performanceFor(Activity.BroadJump), perf_run: performanceFor(Activity.Run), perf_cone_drill: performanceFor(Activity.ConeDrill), }) .returning("id") .executeTakeFirstOrThrow(); } async list(opt: { filter?: { accountId?: string }, limit?: number, offset?: number, } = {}) { return await db.selectFrom("assessments") .selectAll() .$if(opt.filter?.accountId !== undefined, (qb) => qb .where("account_id", "=", opt.filter!.accountId!) ) .orderBy("created_at", "desc") .$if(opt.limit !== undefined, (qb) => qb.limit(opt.limit!)) .$if(opt.offset !== undefined, (qb) => qb.offset(opt.offset!)) .execute(); } }