itpdp/api/src/party/numeric-question-generator.ts
Daniel Bulant 14ccaee48c
improved
2026-05-04 11:06:01 +02:00

47 lines
1.2 KiB
TypeScript

import type { db } from "../db";
import type { Question } from "../party-types";
import type { PartyAnalytics } from "./question-utils";
import { buildQuestionWindow, getQuestionRange } from "./question-utils";
type NumericQuestion = Omit<Extract<Question, { type: "numeric" }>, "startTimestamp" | "endTimestamp">;
type BuildNumericQuestionInput = {
db: typeof db;
analytics: PartyAnalytics;
index: number;
};
async function getAlbumReleaseYear({
db,
analytics,
index,
}: BuildNumericQuestionInput): Promise<NumericQuestion> {
const trackName = analytics?.storyClusters?.[0]?.tracks?.[0]?.name;
const track = trackName
? await db.query.track.findFirst({
where: {
name: trackName,
},
with: {
album: true,
},
})
: null;
const correct =
track?.album?.release_date?.getFullYear() ??
new Date().getFullYear() - 1 - index;
const subject = track?.album?.name ?? track?.name ?? "unknown album";
return {
type: "numeric",
text: `What's the release year of ${subject}?`,
correct,
range: getQuestionRange(correct, 5),
points: 10,
};
}
export async function buildNumericQuestion(
input: BuildNumericQuestionInput,
): Promise<Question> {
return buildQuestionWindow(await getAlbumReleaseYear(input));
}