import { TRPCError } from "@trpc/server"; import { z } from "zod"; import { protectedProcedure } from "./_core/trpc"; import { getDisputedInvoices, getInvoiceById, updateInvoice } from "./db"; /** Le litige ne modifie ni le score, ni la validation BAP, ni l’export. */ export const disputeInput = z.object({ id: z.number().int().positive(), isDisputed: z.boolean(), comment: z.string().max(5000, "Le commentaire est limité à 5 000 caractères.").nullable().optional(), }); export const listDisputes = protectedProcedure.query(({ ctx }) => getDisputedInvoices(ctx.user.role === "admin" ? undefined : ctx.user.id), ); export const setDispute = protectedProcedure.input(disputeInput).mutation(async ({ ctx, input }) => { if (String(ctx.user.role) === "readonly") { throw new TRPCError({ code: "FORBIDDEN", message: "Votre profil permet uniquement la consultation." }); } const invoice = await getInvoiceById(input.id); if (!invoice || (ctx.user.role !== "admin" && invoice.userId !== ctx.user.id)) { throw new TRPCError({ code: "NOT_FOUND", message: "Facture introuvable." }); } await updateInvoice(input.id, { isDisputed: input.isDisputed ? 1 : 0, // L’absence du champ conserve le commentaire ; null ou une chaîne vide l’efface. ...(input.comment !== undefined ? { disputeComment: input.comment?.trim() || null } : {}), }); return { success: true } as const; });