perf: suppression des $queryRaw redondants et cache sur getTemplates
Some checks failed
Deploy with Docker Compose / deploy (push) Failing after 3s
Some checks failed
Deploy with Docker Compose / deploy (push) Failing after 3s
- getEvaluation/getTemplates: retire les $queryRaw qui dupliquaient les données déjà chargées via Prisma include (2 requêtes DB → 1) - getTemplates: wrappé avec cache() React pour dédupliquer les appels dans le même render tree - Supprime l'import Prisma devenu inutile Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Prisma } from "@prisma/client";
|
import { cache } from "react";
|
||||||
import { auth } from "@/auth";
|
import { auth } from "@/auth";
|
||||||
import { prisma } from "@/lib/db";
|
import { prisma } from "@/lib/db";
|
||||||
import { canAccessEvaluation } from "@/lib/evaluation-access";
|
import { canAccessEvaluation } from "@/lib/evaluation-access";
|
||||||
@@ -62,60 +62,20 @@ export async function getEvaluation(id: string) {
|
|||||||
);
|
);
|
||||||
if (!hasAccess) return null;
|
if (!hasAccess) return null;
|
||||||
|
|
||||||
const templateId = evaluation.templateId;
|
|
||||||
const dimsRaw = evaluation.template
|
|
||||||
? ((await prisma.$queryRaw(
|
|
||||||
Prisma.sql`SELECT id, slug, title, rubric, "orderIndex", "suggestedQuestions" FROM "TemplateDimension" WHERE "templateId" = ${templateId} ORDER BY "orderIndex" ASC`
|
|
||||||
)) as { id: string; slug: string; title: string; rubric: string; orderIndex: number; suggestedQuestions: string | null }[])
|
|
||||||
: [];
|
|
||||||
|
|
||||||
const dimMap = new Map(dimsRaw.map((d) => [d.id, d]));
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...evaluation,
|
...evaluation,
|
||||||
evaluationDate: evaluation.evaluationDate.toISOString(),
|
evaluationDate: evaluation.evaluationDate.toISOString(),
|
||||||
template: evaluation.template
|
|
||||||
? {
|
|
||||||
...evaluation.template,
|
|
||||||
dimensions: evaluation.template.dimensions.map((d) => {
|
|
||||||
const raw = dimMap.get(d.id);
|
|
||||||
return {
|
|
||||||
...d,
|
|
||||||
suggestedQuestions: raw?.suggestedQuestions ?? d.suggestedQuestions,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
dimensionScores: evaluation.dimensionScores.map((ds) => ({
|
|
||||||
...ds,
|
|
||||||
dimension: ds.dimension
|
|
||||||
? {
|
|
||||||
...ds.dimension,
|
|
||||||
suggestedQuestions: dimMap.get(ds.dimension.id)?.suggestedQuestions ?? ds.dimension.suggestedQuestions,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
})),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTemplates() {
|
export const getTemplates = cache(async () => {
|
||||||
const templates = await prisma.template.findMany({
|
const templates = await prisma.template.findMany({
|
||||||
include: {
|
include: {
|
||||||
dimensions: { orderBy: { orderIndex: "asc" } },
|
dimensions: { orderBy: { orderIndex: "asc" } },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const dimsRaw = (await prisma.$queryRaw(
|
return templates;
|
||||||
Prisma.sql`SELECT id, "templateId", slug, title, rubric, "orderIndex", "suggestedQuestions" FROM "TemplateDimension" ORDER BY "templateId", "orderIndex"`
|
});
|
||||||
)) as { id: string; templateId: string; slug: string; title: string; rubric: string; orderIndex: number; suggestedQuestions: string | null }[];
|
|
||||||
const dimMap = new Map(dimsRaw.map((d) => [d.id, d]));
|
|
||||||
return templates.map((t) => ({
|
|
||||||
...t,
|
|
||||||
dimensions: t.dimensions.map((d) => ({
|
|
||||||
...d,
|
|
||||||
suggestedQuestions: dimMap.get(d.id)?.suggestedQuestions ?? d.suggestedQuestions,
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getUsers() {
|
export async function getUsers() {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|||||||
Reference in New Issue
Block a user