infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/comunicazioni.ts

212 lines
5.1 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
import type { EmailsIdEmail } from "~/schemas/public/Emails";
import {
adminProcedure,
createTRPCRouter,
protectedProcedure,
publicProcedure,
} from "~/server/api/trpc";
import { SendContattoAnnuncioEmail } from "~/server/controllers/servizio.controller";
import { db } from "~/server/db";
import {
deleteAllUserEmails,
deleteEmail,
getEmails,
} from "~/server/services/email.service";
import { NewMail } from "~/server/services/mailer";
import { genPdfSchedaAnnuncioBase64 } from "~/server/services/puppeteer.service";
import { zAnnuncioId, zUserId } from "~/server/utils/zod_types";
2025-08-04 17:45:44 +02:00
export const comunicazioniRouter = createTRPCRouter({
getEmails: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(z.object({ userId: zUserId }))
.query(async ({ input }) => {
return await getEmails({
2025-08-28 18:27:07 +02:00
userId: input.userId,
});
}),
deleteEmail: adminProcedure
.input(
z.object({
id_email: z.custom<EmailsIdEmail>(),
}),
)
.mutation(async ({ input }) => {
return await deleteEmail({
id_email: input.id_email,
});
}),
deleteAllUserEmails: adminProcedure
.input(z.object({ userId: zUserId }))
.mutation(async ({ input }) => {
return await deleteAllUserEmails({
userId: input.userId,
});
}),
sendEmailContatto: publicProcedure
.input(
z.object({
cognome: z.string(),
email: z.email(),
messaggio: z.string(),
nome: z.string(),
telefono: z.string(),
}),
)
.mutation(async ({ input }) => {
await NewMail({
template: {
mailType: "contattoAdminEmail",
props: {
cognome: input.cognome,
email: input.email,
messaggio: input.messaggio,
nome: input.nome,
telefono: input.telefono,
},
},
mail: {
subject: `Richiesta di contatto da ${input.nome}`,
to: "web@infoalloggi.it",
},
});
return { success: true };
}),
sendSchedaAnnuncioEmail: adminProcedure
.input(
z.object({
id: zAnnuncioId,
codice: z.string(),
numero: z.string(),
nominativo: z.string(),
indirizzo: z.string(),
to: z.email(),
}),
)
.mutation(async ({ input }) => {
try {
const scheda = await genPdfSchedaAnnuncioBase64(input.id);
await NewMail({
template: {
mailType: "emailSchedaContatto",
props: {
nominativo: input.nominativo,
telefono: input.numero,
indirizzo: input.indirizzo,
},
},
mail: {
subject: `Scheda Annuncio ${input.codice} - Infoalloggi.it`,
to: input.to,
attachments: [
{
filename: `scheda annuncio ${input.codice}.pdf`,
content: scheda,
encoding: "base64",
contentType: "application/pdf",
},
],
},
});
await NewMail({
template: {
mailType: "generic",
props: {
title: `Ricevuta invio Scheda ${input.codice} a ${input.to}`,
testo: `La scheda dell'annuncio ${input.codice} è stata inviata a ${input.to} con successo.`,
},
},
mail: {
subject: `Ricevuta invio Scheda ${input.codice} a ${input.to}`,
to: "web@infoalloggi.it",
},
});
} catch (e) {
throw new Error(
`Errore nell'invio dell'email: ${(e as Error).message}`,
);
}
}),
sendSchedaAnnuncioServizio: adminProcedure
.input(
z.object({
userId: zUserId,
annuncioId: zAnnuncioId,
}),
)
.mutation(async ({ input }) => {
try {
const scheda = await genPdfSchedaAnnuncioBase64(input.annuncioId);
const user = await db
.selectFrom("users")
.select("email")
.where("id", "=", input.userId)
.executeTakeFirstOrThrow(() => {
throw new Error(`User not found - userId: ${input.userId}`);
});
const annuncio = await db
.selectFrom("annunci")
.select([
"codice",
"locatore",
"indirizzo",
"civico",
"comune",
"cap",
"provincia",
"numero",
])
.where("id", "=", input.annuncioId)
.executeTakeFirstOrThrow(
() =>
new Error(`Annuncio not found - annuncioId: ${input.annuncioId}`),
);
await NewMail({
template: {
mailType: "emailContattiConScheda",
props: {
codice: annuncio.codice,
nominativo: annuncio.locatore || "",
telefono: annuncio.numero || "",
indirizzo: `${annuncio.indirizzo || ""}, ${annuncio.civico || ""} ${annuncio.comune || ""} ${annuncio.cap || ""} (${annuncio.provincia || ""})`,
},
},
mail: {
subject: `Contatti Annuncio ${annuncio.codice} - Infoalloggi.it`,
to: user.email,
attachments: [
{
filename: `scheda annuncio ${annuncio.codice}.pdf`,
content: scheda,
encoding: "base64",
contentType: "application/pdf",
},
],
},
});
} catch (e) {
throw new Error(
`Errore nell'invio dell'email: ${(e as Error).message}`,
);
}
}),
sendContattoAnnuncioEmail: publicProcedure
.input(
z.object({
codice: z.string(),
email: z.string(),
messaggio: z.string(),
nome: z.string(),
telefono: z.string(),
}),
)
.mutation(async ({ input }) => {
return await SendContattoAnnuncioEmail({
...input,
});
}),
2025-08-04 17:45:44 +02:00
});