import { TRPCError } from "@trpc/server"; import { z } from "zod/v4"; import type { AnnunciUpdate } from "~/schemas/public/Annunci"; import { adminProcedure, apiProcedure, createTRPCRouter, protectedProcedure, publicProcedure, } from "~/server/api/trpc"; import { editAnnuncioHandler, get_AnnunciPositionsHandler, getAnnunciByCod, getAnnunciById_rawImgUrls, getAnnunciListHandler, getAnnunciMetaByCod, getAnnunciRicerca, getCodici_AnnunciHandler, getOnline_AnnuncioIdCodice_AnnunciHandler, getOptions_AnnunciHandler, getProprietarioDataHandler, type ImgToRemove, removeAnnuncioMedia, type VideoToRemove, } from "~/server/controllers/annunci.controller"; import { db } from "~/server/db"; import { NewMail } from "~/server/services/mailer"; import { genPdfSchedaAnnuncio } from "~/server/services/puppeteer.service"; import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types"; import { getKeydbClient } from "~/utils/keydb"; export const annunciRouter = createTRPCRouter({ editAnnuncio: adminProcedure .input( z.object({ annuncioId: zAnnuncioId, data: z.custom() }), ) .mutation(async ({ input }) => { const keybd = getKeydbClient(); if (keybd) { await keybd.del(`scheda-annuncio-${input.annuncioId}`); } return await editAnnuncioHandler({ ...input, }); }), getAnnunciList: publicProcedure.query(async () => { return await getAnnunciListHandler(); }), getAnnunciOptions: publicProcedure.query(async () => { return await getOptions_AnnunciHandler(); }), getOnline_AnnuncioIdCodice: publicProcedure.query(async () => { return await getOnline_AnnuncioIdCodice_AnnunciHandler(); }), getAnnuncio: publicProcedure .input( z.object({ cod: z.string(), }), ) .query(async ({ input }) => { return await getAnnunciByCod({ ...input }); }), getAnnuncioById_rawImgUrls: publicProcedure .input( z.object({ id: zAnnuncioId, }), ) .query(async ({ input }) => { return await getAnnunciById_rawImgUrls({ ...input }); }), getAnnuncioMeta: publicProcedure .input( z.object({ cod: z.string(), }), ) .query(async ({ input }) => { return await getAnnunciMetaByCod({ ...input }); }), getCodici: publicProcedure.query(async () => { return await getCodici_AnnunciHandler(); }), getMapping: publicProcedure .input( z.object({ comune: z.string().optional(), consegna: z.number().array().optional(), tipologia: z.string().optional(), minBudget: z.number().optional(), maxBudget: z.number().optional(), }), ) .query(async ({ input }) => { return await get_AnnunciPositionsHandler({ ...input, }); }), getProprietarioData: protectedProcedure .input(z.object({ annuncioId: zAnnuncioId, servizioId: zServizioId })) .query(async ({ input }) => { return await getProprietarioDataHandler({ annuncioId: input.annuncioId, servizioId: input.servizioId, }); }), annunciRicerca: publicProcedure .input( z.object({ comune: z.string().optional(), consegna: z.number().array().optional(), sort: z.enum(["Recenti", "Prezzo", "Consegna", "Mq"]).optional(), tipologia: z.string().optional(), minBudget: z.number().optional(), maxBudget: z.number().optional(), }), ) .query(async ({ input }) => { return await getAnnunciRicerca({ ...input, }); }), checkAnnunci: apiProcedure.query(async () => { const hasAnnunci = await db .selectFrom("annunci") .select("id") .where("stato", "=", "Attivo") .where("web", "is", true) .execute(); if (hasAnnunci.length === 0) { await NewMail({ template: { mailType: "generic", props: { title: "Attenzione: Nessun annuncio attivo!", testo: "Nel sistema non รจ presente nessun annuncio con stato Attivo e visibile sul web.", }, }, mail: { subject: "Attenzione: Nessun annuncio attivo!", to: "m.pedone98@gmail.com", }, }); return { status: "no_annunci", timestamp: Date.now() }; } return { status: "ok", timestamp: Date.now() }; }), removeAnnuncioMedia: adminProcedure .input(z.custom()) .mutation(async ({ input }) => { return await removeAnnuncioMedia({ ...input, }); }), getSchedaAnnuncio: adminProcedure .input( z.object({ id: zAnnuncioId, }), ) .mutation(async ({ input }) => { try { return await genPdfSchedaAnnuncio(input.id); } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Errore nella generazione della scheda: ${(e as Error).message}`, }); } }), });