2022-04-22 19:50:28 +00:00
|
|
|
import { Question } from "@prisma/client";
|
2022-03-16 21:02:34 +00:00
|
|
|
|
2022-04-22 19:50:28 +00:00
|
|
|
import { measureTime } from "./utils/measureTime";
|
2022-03-16 21:02:34 +00:00
|
|
|
|
2022-04-22 19:50:28 +00:00
|
|
|
export async function getFrontpage(): Promise<Question[]> {
|
|
|
|
const questions = await prisma.question.findMany({
|
|
|
|
where: {
|
|
|
|
onFrontpage: {
|
|
|
|
isNot: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
console.log(questions.length);
|
|
|
|
return questions;
|
2022-03-16 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:51:11 +00:00
|
|
|
export async function rebuildFrontpage() {
|
2022-04-22 19:50:28 +00:00
|
|
|
await measureTime(async () => {
|
|
|
|
const rows = await prisma.$queryRaw<{ id: string }[]>`
|
|
|
|
SELECT id FROM questions
|
|
|
|
WHERE
|
|
|
|
(qualityindicators->>'stars')::int >= 3
|
|
|
|
AND description != ''
|
|
|
|
AND JSONB_ARRAY_LENGTH(options) > 0
|
|
|
|
ORDER BY RANDOM() LIMIT 50
|
|
|
|
`;
|
2022-03-25 23:51:11 +00:00
|
|
|
|
2022-04-22 19:50:28 +00:00
|
|
|
await prisma.$transaction([
|
|
|
|
prisma.frontpageId.deleteMany({}),
|
|
|
|
prisma.frontpageId.createMany({
|
|
|
|
data: rows,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
2022-03-25 23:51:11 +00:00
|
|
|
}
|