111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import "dotenv/config";
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
import { dane, punkty } from "@/src/db/schema";
|
|
import { sql, eq, and, or, isNotNull } from "drizzle-orm";
|
|
import { AdvancedQuestion } from "~/types";
|
|
import arrayShuffle from "array-shuffle";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
async function getFromDb(points: number | string) {
|
|
return await db
|
|
.select({
|
|
id: dane.id,
|
|
nr_pytania: dane.nr_pytania,
|
|
pytanie: dane.pytanie,
|
|
poprawna_odp: dane.poprawna_odp,
|
|
media: dane.media,
|
|
kategorie: dane.kategorie,
|
|
nazwa_media_pjm_tresc_pyt: dane.nazwa_media_pjm_tresc_pyt,
|
|
pytanie_eng: dane.pytanie_eng,
|
|
pytanie_de: dane.pytanie_de,
|
|
pytanie_ua: dane.pytanie_ua,
|
|
liczba_pkt: punkty.liczba_pkt,
|
|
|
|
odp_a: dane.odp_a,
|
|
odp_b: dane.odp_b,
|
|
odp_c: dane.odp_c,
|
|
nazwa_media_pjm_tresc_odp_a: dane.nazwa_media_pjm_tresc_odp_a,
|
|
nazwa_media_pjm_tresc_odp_b: dane.nazwa_media_pjm_tresc_odp_b,
|
|
nazwa_media_pjm_tresc_odp_c: dane.nazwa_media_pjm_tresc_odp_c,
|
|
odp_a_eng: dane.odp_a_eng,
|
|
odp_b_eng: dane.odp_b_eng,
|
|
odp_c_eng: dane.odp_c_eng,
|
|
odp_a_de: dane.odp_a_de,
|
|
odp_b_de: dane.odp_b_de,
|
|
odp_c_de: dane.odp_c_de,
|
|
odp_a_ua: dane.odp_a_ua,
|
|
odp_b_ua: dane.odp_b_ua,
|
|
odp_c_ua: dane.odp_c_ua,
|
|
})
|
|
.from(dane)
|
|
.innerJoin(punkty, eq(dane.nr_pytania, punkty.nr_pytania))
|
|
.where(
|
|
and(
|
|
isNotNull(dane.odp_a),
|
|
isNotNull(dane.odp_b),
|
|
isNotNull(dane.odp_c),
|
|
isNotNull(dane.odp_a_eng),
|
|
isNotNull(dane.odp_b_eng),
|
|
isNotNull(dane.odp_c_eng),
|
|
isNotNull(dane.odp_a_de),
|
|
isNotNull(dane.odp_b_de),
|
|
isNotNull(dane.odp_c_de),
|
|
eq(punkty.liczba_pkt, +points)
|
|
)
|
|
);
|
|
}
|
|
const query = getQuery(event);
|
|
const category = query.category;
|
|
const categories = [
|
|
"A",
|
|
"B",
|
|
"C",
|
|
"D",
|
|
"T",
|
|
"AM",
|
|
"A1",
|
|
"A2",
|
|
"B1",
|
|
"C1",
|
|
"D1",
|
|
"PT",
|
|
];
|
|
if (category == null || category == "") {
|
|
throw new Error(
|
|
"category argument has to be string (or not to be defined at all)"
|
|
);
|
|
}
|
|
if (!categories.includes(`${category}`)) {
|
|
throw new Error(`category argument has to be equal to: ${categories}`);
|
|
}
|
|
const db = drizzle(process.env.DATABASE_URL!);
|
|
|
|
const randomizedQuestions: AdvancedQuestion[] = [];
|
|
|
|
for (let [key, value] of Object.entries({ 1: 2, 2: 4, 3: 6 })) {
|
|
const questionsKeyPoints: AdvancedQuestion[] = await getFromDb(key);
|
|
const chosenRandomQuestions: AdvancedQuestion[] = [];
|
|
|
|
const randoms: Array<number> = [];
|
|
for (let j = 0; j < value; j++) {
|
|
let randomized =
|
|
Math.floor(Math.random() * (questionsKeyPoints.length - 1 + 1)) + 0;
|
|
while (randoms.includes(randomized)) {
|
|
randomized =
|
|
Math.floor(Math.random() * (questionsKeyPoints.length - 1 + 1)) + 0;
|
|
}
|
|
randoms.push(randomized);
|
|
if (
|
|
questionsKeyPoints[randomized].kategorie
|
|
.split(",")
|
|
.includes(`${category}`)
|
|
) {
|
|
chosenRandomQuestions.push(questionsKeyPoints[randomized]);
|
|
} else {
|
|
j--;
|
|
}
|
|
}
|
|
chosenRandomQuestions.forEach((q) => randomizedQuestions.push(q));
|
|
}
|
|
return arrayShuffle(randomizedQuestions);
|
|
});
|