60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import { notFound, redirect } from "next/navigation";
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import { maskCpf } from "@/lib/admin-service";
|
|
import { decryptCpf } from "@/lib/crypto";
|
|
|
|
export async function getBriefingPdfData(briefingId: string) {
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
redirect("/login");
|
|
}
|
|
|
|
const briefing = await db.briefing.findUnique({
|
|
where: { id: briefingId },
|
|
include: {
|
|
cliente: {
|
|
include: { user: true }
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!briefing) {
|
|
notFound();
|
|
}
|
|
|
|
const isAdmin = session.user.papel === "ADMIN";
|
|
const isOwner = session.user.papel === "CLIENTE" && briefing.cliente.userId === session.user.id;
|
|
|
|
if (!isAdmin && !isOwner) {
|
|
notFound();
|
|
}
|
|
|
|
const secoes = await db.secao.findMany({
|
|
where: { ativo: true },
|
|
orderBy: { ordem: "asc" },
|
|
include: {
|
|
perguntas: {
|
|
where: { ativo: true },
|
|
orderBy: { ordem: "asc" },
|
|
include: {
|
|
respostas: {
|
|
where: { briefingId }
|
|
},
|
|
anexos: {
|
|
where: { briefingId },
|
|
orderBy: { criadoEm: "asc" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return {
|
|
briefing,
|
|
secoes,
|
|
cpfMascarado: maskCpf(decryptCpf(briefing.cliente.cpfCifrado))
|
|
};
|
|
}
|