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

212 lines
5 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
import type { EmailsIdEmail } from "~/schemas/public/Emails";
import {
adminProcedure,
createTRPCRouter,
protectedProcedure,
} from "~/server/api/trpc";
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({
// genEmail: publicProcedure
// .input(
// z.object({
// mail: z.custom<MailsTemplates>(),
// }),
// )
// .query(async ({ input }) => {
// return await genMail({
// ...input.mail,
// });
// }),
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,
});
}),
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();
const annuncio = await db
.selectFrom("annunci")
.select([
"codice",
"locatore",
"indirizzo",
"civico",
"comune",
"cap",
"provincia",
"numero",
])
.where("id", "=", input.annuncioId)
.executeTakeFirstOrThrow();
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}`,
);
}
}),
sendAnnunciEmail: adminProcedure
.input(
z.object({
annunci: zAnnuncioId.array(),
to: z.email(),
}),
)
.mutation(async ({ input }) => {
try {
const annunci = await db
.selectFrom("annunci")
.select([
"annunci.titolo_it as titolo",
"annunci.codice",
"annunci.prezzo",
])
.innerJoin("images_refs", "images_refs.codice", "annunci.codice")
.select("images_refs.img as immagine")
.where("images_refs.ordine", "=", 0)
.where("annunci.id", "in", input.annunci)
.execute();
await NewMail({
template: {
mailType: "shareAnnunci",
props: {
nome: "Admin",
annunci,
},
},
mail: {
subject: `Scheda Annuncio - Infoalloggi.it`,
to: input.to,
},
});
} catch (e) {
throw new Error(
`Errore nell'invio dell'email: ${(e as Error).message}`,
);
}
}),
2025-08-04 17:45:44 +02:00
});