From a0d5faf97275bbbfadc87344f5e8a6673c54d60c Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Mon, 29 Dec 2025 16:00:57 +0100 Subject: [PATCH] refactor: update email service functions and add delete all user emails functionality --- .../area-riservata/comunicazioni.tsx | 2 +- .../user-view/comunicazioni/[userId].tsx | 65 ++++++++++++++++--- .../src/server/api/routers/comunicazioni.ts | 30 ++++----- .../src/server/services/email.service.ts | 17 ++++- .../infoalloggi/src/server/services/mailer.ts | 4 +- 5 files changed, 87 insertions(+), 31 deletions(-) diff --git a/apps/infoalloggi/src/components/area-riservata/comunicazioni.tsx b/apps/infoalloggi/src/components/area-riservata/comunicazioni.tsx index 91145a0..e44fbfb 100644 --- a/apps/infoalloggi/src/components/area-riservata/comunicazioni.tsx +++ b/apps/infoalloggi/src/components/area-riservata/comunicazioni.tsx @@ -38,7 +38,7 @@ export const EmailAccordion = ({ const iframeHeight = mobile ? 600 : 500; return ( -
+
= ({ userId, }: ComunicazioniUserProps) => { const { user } = useEnforcedSession(); - return ( -
-
-
-

Comunicazioni

+ const utils = api.useUtils(); + const { mutate } = api.comunicazioni.deleteAllUserEmails.useMutation({ + onError: (error) => { + console.error(error); + toast.error("Errore durante l'eliminazione delle comunicazioni"); + }, + onSuccess: async () => { + toast.success("Comunicazioni eliminate"); + await utils.comunicazioni.getEmails.invalidate({ userId }); + }, + }); -
- -
-
+ return ( +
+
+

Comunicazioni

+ + + + + + { + mutate({ userId }); + }} + title="Elimina tutte le comunicazioni" + > + + + +
+ +
); }; diff --git a/apps/infoalloggi/src/server/api/routers/comunicazioni.ts b/apps/infoalloggi/src/server/api/routers/comunicazioni.ts index c57a1dc..066e358 100644 --- a/apps/infoalloggi/src/server/api/routers/comunicazioni.ts +++ b/apps/infoalloggi/src/server/api/routers/comunicazioni.ts @@ -5,7 +5,11 @@ import { createTRPCRouter, protectedProcedure, } from "~/server/api/trpc"; -import { DeleteEmail, GetEmails } from "~/server/services/email.service"; +import { + deleteAllUserEmails, + deleteEmail, + getEmails, +} from "~/server/services/email.service"; import { NewMail } from "~/server/services/mailer"; import { zUserId } from "~/server/utils/zod_types"; @@ -24,7 +28,7 @@ export const comunicazioniRouter = createTRPCRouter({ getEmails: protectedProcedure .input(z.object({ userId: zUserId })) .query(async ({ input }) => { - return await GetEmails({ + return await getEmails({ userId: input.userId, }); }), @@ -35,23 +39,17 @@ export const comunicazioniRouter = createTRPCRouter({ }), ) .mutation(async ({ input }) => { - return await DeleteEmail({ + return await deleteEmail({ id_email: input.id_email, }); }), - // storeEmail: publicProcedure - // .input( - // z.object({ - // data: z.custom(), - // userId: zUserId, - // }), - // ) - // .mutation(async ({ input }) => { - // return await StoreEmail({ - // data: input.data, - // userId: input.userId, - // }); - // }), + deleteAllUserEmails: adminProcedure + .input(z.object({ userId: zUserId })) + .mutation(async ({ input }) => { + return await deleteAllUserEmails({ + userId: input.userId, + }); + }), sendSchedaAnnuncioEmail: adminProcedure .input( z.object({ diff --git a/apps/infoalloggi/src/server/services/email.service.ts b/apps/infoalloggi/src/server/services/email.service.ts index 3245600..0f228e5 100644 --- a/apps/infoalloggi/src/server/services/email.service.ts +++ b/apps/infoalloggi/src/server/services/email.service.ts @@ -4,7 +4,7 @@ import type { UsersId } from "~/schemas/public/Users"; import { db } from "~/server/db"; import { genMail, type MailsTemplates } from "~/server/services/mailer"; -export const StoreEmail = async ({ +export const storeEmail = async ({ userId, data, }: { @@ -28,7 +28,7 @@ export const StoreEmail = async ({ } }; -export const GetEmails = async ({ userId }: { userId: UsersId }) => { +export const getEmails = async ({ userId }: { userId: UsersId }) => { try { const mails = await db .selectFrom("emails") @@ -56,7 +56,7 @@ export const GetEmails = async ({ userId }: { userId: UsersId }) => { } }; -export const DeleteEmail = async ({ +export const deleteEmail = async ({ id_email, }: { id_email: EmailsIdEmail; @@ -70,3 +70,14 @@ export const DeleteEmail = async ({ }); } }; + +export const deleteAllUserEmails = async ({ userId }: { userId: UsersId }) => { + try { + await db.deleteFrom("emails").where("user_id", "=", userId).execute(); + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Errore durante l'eliminazione delle email dell'utente: ${(e as Error).message}`, + }); + } +}; diff --git a/apps/infoalloggi/src/server/services/mailer.ts b/apps/infoalloggi/src/server/services/mailer.ts index ad89da7..4ab3371 100644 --- a/apps/infoalloggi/src/server/services/mailer.ts +++ b/apps/infoalloggi/src/server/services/mailer.ts @@ -41,7 +41,7 @@ import { env } from "~/env"; import type { UsersId } from "~/schemas/public/Users"; import { GetFlagValueHandler } from "~/server/controllers/flags.controller"; import { db } from "~/server/db"; -import { StoreEmail } from "~/server/services/email.service"; +import { storeEmail } from "~/server/services/email.service"; const mailer = async (options: Options) => { const smtpTransport = createTransport({ @@ -230,6 +230,6 @@ export const NewMail = async ({ template, userId, mail }: NewMailProps) => { if (userId && template) { // Store the email in the database if userId is provided - await StoreEmail({ data: template, userId }); + await storeEmail({ data: template, userId }); } };