2025-08-07 14:50:40 +02:00
|
|
|
import { z } from "zod/v4";
|
2025-12-01 12:29:48 +01:00
|
|
|
import type { EmailsIdEmail } from "~/schemas/public/Emails";
|
2025-11-19 17:18:28 +01:00
|
|
|
import {
|
|
|
|
|
adminProcedure,
|
|
|
|
|
createTRPCRouter,
|
|
|
|
|
protectedProcedure,
|
|
|
|
|
} from "~/server/api/trpc";
|
2025-12-01 12:29:48 +01:00
|
|
|
import { DeleteEmail, GetEmails } from "~/server/services/email.service";
|
2025-11-19 17:18:28 +01:00
|
|
|
import { NewMail } from "~/server/services/mailer";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { zUserId } from "~/server/utils/zod_types";
|
2025-08-04 17:45:44 +02:00
|
|
|
|
|
|
|
|
export const comunicazioniRouter = createTRPCRouter({
|
2025-10-20 18:00:53 +02:00
|
|
|
// 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({
|
|
|
|
|
userId: input.userId,
|
|
|
|
|
});
|
|
|
|
|
}),
|
2025-12-01 12:29:48 +01:00
|
|
|
deleteEmail: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
id_email: z.custom<EmailsIdEmail>(),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ input }) => {
|
|
|
|
|
return await DeleteEmail({
|
|
|
|
|
id_email: input.id_email,
|
|
|
|
|
});
|
|
|
|
|
}),
|
2025-10-20 18:00:53 +02:00
|
|
|
// storeEmail: publicProcedure
|
|
|
|
|
// .input(
|
|
|
|
|
// z.object({
|
|
|
|
|
// data: z.custom<MailsTemplates>(),
|
|
|
|
|
// userId: zUserId,
|
|
|
|
|
// }),
|
|
|
|
|
// )
|
|
|
|
|
// .mutation(async ({ input }) => {
|
|
|
|
|
// return await StoreEmail({
|
|
|
|
|
// data: input.data,
|
|
|
|
|
// userId: input.userId,
|
|
|
|
|
// });
|
|
|
|
|
// }),
|
2025-11-19 17:18:28 +01:00
|
|
|
sendSchedaAnnuncioEmail: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
pdf: z.string(),
|
|
|
|
|
codice: z.string(),
|
2025-11-20 11:59:24 +01:00
|
|
|
numero: z.string(),
|
|
|
|
|
nominativo: z.string(),
|
|
|
|
|
indirizzo: z.string(),
|
2025-11-19 17:18:28 +01:00
|
|
|
to: z.email(),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ input }) => {
|
|
|
|
|
try {
|
|
|
|
|
const base64Content = input.pdf.includes("base64,")
|
|
|
|
|
? input.pdf.split("base64,")[1]
|
|
|
|
|
: input.pdf;
|
2025-11-20 11:59:24 +01:00
|
|
|
|
2025-11-19 17:18:28 +01:00
|
|
|
await NewMail({
|
|
|
|
|
template: {
|
2025-11-20 11:59:24 +01:00
|
|
|
mailType: "emailSchedaContatto",
|
2025-11-19 17:18:28 +01:00
|
|
|
props: {
|
2025-11-20 11:59:24 +01:00
|
|
|
nominativo: input.nominativo,
|
|
|
|
|
telefono: input.numero,
|
|
|
|
|
indirizzo: input.indirizzo,
|
2025-11-19 17:18:28 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
mail: {
|
|
|
|
|
subject: `Scheda Annuncio ${input.codice} - Infoalloggi.it`,
|
|
|
|
|
to: input.to,
|
|
|
|
|
attachments: [
|
|
|
|
|
{
|
2025-11-20 11:59:24 +01:00
|
|
|
filename: `scheda annuncio ${input.codice}.pdf`,
|
2025-11-19 17:18:28 +01:00
|
|
|
content: base64Content,
|
|
|
|
|
encoding: "base64",
|
|
|
|
|
contentType: "application/pdf",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Errore nell'invio dell'email: ${(e as Error).message}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}),
|
2025-08-04 17:45:44 +02:00
|
|
|
});
|