import { notFound, redirect } from "next/navigation"; import { auth } from "@/auth"; import { db } from "@/lib/db"; async function getClienteAtual() { const session = await auth(); if (!session?.user?.id || session.user.papel !== "CLIENTE") { redirect("/login"); } const cliente = await db.cliente.findUnique({ where: { userId: session.user.id }, include: { user: true } }); if (!cliente) { notFound(); } return cliente; } function isRespondida(resposta: { texto: string | null; selecionadas: unknown; complemento?: string | null; }) { const selecionadas = Array.isArray(resposta.selecionadas) ? resposta.selecionadas : []; return Boolean( resposta.texto?.trim() || resposta.complemento?.trim() || selecionadas.length > 0 ); } export async function listarBriefingsDoCliente() { const cliente = await getClienteAtual(); return db.briefing.findMany({ where: { clienteId: cliente.id }, orderBy: { atualizadoEm: "desc" }, include: { respostas: true } }); } export async function criarBriefingParaCliente() { const cliente = await getClienteAtual(); const titulo = cliente.razaoSocial ?? cliente.nomeCompleto; const briefing = await db.briefing.create({ data: { clienteId: cliente.id, titulo: `Briefing - ${titulo}` } }); return briefing; } export async function getBriefingDoCliente(briefingId: string) { const cliente = await getClienteAtual(); const briefing = await db.briefing.findFirst({ where: { id: briefingId, clienteId: cliente.id }, include: { respostas: true, cliente: { include: { user: true } } } }); if (!briefing) { notFound(); } return briefing; } export async function getAberturaBriefing(briefingId: string) { const briefing = await getBriefingDoCliente(briefingId); const [secoes, configuracoes] = await Promise.all([ db.secao.findMany({ where: { ativo: true }, orderBy: { ordem: "asc" }, include: { perguntas: { where: { ativo: true }, orderBy: { ordem: "asc" }, include: { anexos: { where: { briefingId: briefing.id }, orderBy: { criadoEm: "desc" } }, respostas: { where: { briefingId: briefing.id } } } } } }), db.configuracao.findMany({ where: { chave: { in: ["comoPreencher", "avisoSenhas"] } } }) ]); const progresso = secoes.map((secao) => { const total = secao.perguntas.length; const respondidas = secao.perguntas.filter((pergunta) => pergunta.respostas.some((resposta) => isRespondida(resposta)) ).length; return { id: secao.id, ordem: secao.ordem, titulo: secao.titulo, descricao: secao.descricao, total, respondidas }; }); return { briefing, progresso, comoPreencher: configuracoes.find((configuracao) => configuracao.chave === "comoPreencher")?.valor ?? [], avisoSenhas: configuracoes.find((configuracao) => configuracao.chave === "avisoSenhas")?.valor ?? null }; } export async function getSecaoBriefing(briefingId: string, ordemSecao: number) { const briefing = await getBriefingDoCliente(briefingId); const secoes = await db.secao.findMany({ where: { ativo: true }, orderBy: { ordem: "asc" }, select: { id: true, ordem: true, titulo: true, perguntas: { where: { ativo: true }, select: { id: true, respostas: { where: { briefingId: briefing.id }, select: { texto: true, selecionadas: true, complemento: true } } } } } }); const secao = await db.secao.findFirst({ where: { ordem: ordemSecao, ativo: true }, include: { perguntas: { where: { ativo: true }, orderBy: { ordem: "asc" }, include: { ajuda: true, anexos: { where: { briefingId: briefing.id }, orderBy: { criadoEm: "desc" } }, respostas: { where: { briefingId: briefing.id } } } } } }); if (!secao) { notFound(); } const progressoGeral = secoes.reduce( (acc, item) => { const respondidas = item.perguntas.filter((pergunta) => pergunta.respostas.some((resposta) => isRespondida(resposta)) ).length; return { total: acc.total + item.perguntas.length, respondidas: acc.respondidas + respondidas }; }, { total: 0, respondidas: 0 } ); return { briefing, secao, secoes: secoes.map((item) => ({ id: item.id, ordem: item.ordem, titulo: item.titulo, total: item.perguntas.length, respondidas: item.perguntas.filter((pergunta) => pergunta.respostas.some((resposta) => isRespondida(resposta)) ).length })), progressoGeral, anterior: secoes.find((item) => item.ordem === ordemSecao - 1) ?? null, proxima: secoes.find((item) => item.ordem === ordemSecao + 1) ?? null }; } export async function getRevisaoBriefing(briefingId: string) { const briefing = await getBriefingDoCliente(briefingId); const [secoes, confirmacao] = await Promise.all([ db.secao.findMany({ where: { ativo: true }, orderBy: { ordem: "asc" }, include: { perguntas: { where: { ativo: true }, orderBy: { ordem: "asc" }, include: { anexos: { where: { briefingId: briefing.id }, orderBy: { criadoEm: "desc" } }, respostas: { where: { briefingId: briefing.id } } } } } }), db.configuracao.findUnique({ where: { chave: "confirmacaoCliente" } }) ]); return { briefing, secoes, confirmacaoCliente: confirmacao?.valor ?? null }; } export async function reabrirBriefing(briefingId: string) { return db.briefing.update({ where: { id: briefingId }, data: { status: "RASCUNHO", confirmadoEm: null, enviadoEm: null } }); }