Initial SeConectai briefing system
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"use server";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
import { getBriefingDoCliente } from "@/lib/briefing-service";
|
||||
import { db } from "@/lib/db";
|
||||
import { notificarBriefingEnviado } from "@/lib/notifications";
|
||||
|
||||
export type EnvioBriefingState = {
|
||||
ok: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export async function enviarBriefing(
|
||||
briefingId: string,
|
||||
_state: EnvioBriefingState,
|
||||
formData: FormData
|
||||
): Promise<EnvioBriefingState> {
|
||||
const briefing = await getBriefingDoCliente(briefingId);
|
||||
|
||||
if (briefing.status !== "RASCUNHO") {
|
||||
return { ok: false, message: "Este briefing ja foi enviado." };
|
||||
}
|
||||
|
||||
if (formData.get("confirmacaoCliente") !== "on") {
|
||||
return { ok: false, message: "Confirme a declaracao do cliente para enviar." };
|
||||
}
|
||||
|
||||
const totalPerguntas = await db.pergunta.count({
|
||||
where: { ativo: true }
|
||||
});
|
||||
const respostas = await db.resposta.findMany({
|
||||
where: { briefingId: briefing.id },
|
||||
select: {
|
||||
texto: true,
|
||||
complemento: true,
|
||||
selecionadas: true
|
||||
}
|
||||
});
|
||||
const respondidas = respostas.filter((resposta) => {
|
||||
const selecionadas = Array.isArray(resposta.selecionadas) ? resposta.selecionadas : [];
|
||||
return Boolean(resposta.texto?.trim() || resposta.complemento?.trim() || selecionadas.length > 0);
|
||||
}).length;
|
||||
|
||||
if (respondidas < totalPerguntas && formData.get("confirmarBrancos") !== "on") {
|
||||
return {
|
||||
ok: false,
|
||||
message: "Confirme que deseja enviar mesmo com perguntas em branco."
|
||||
};
|
||||
}
|
||||
|
||||
await db.briefing.update({
|
||||
where: { id: briefing.id },
|
||||
data: {
|
||||
status: "ENVIADO",
|
||||
confirmadoEm: new Date(),
|
||||
enviadoEm: new Date()
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await notificarBriefingEnviado(briefing.id);
|
||||
} catch {
|
||||
// Falha de notificacao nao pode impedir o envio do briefing.
|
||||
}
|
||||
|
||||
redirect(`/briefings/${briefing.id}/revisao`);
|
||||
}
|
||||
Reference in New Issue
Block a user