infoalloggi-monorepo/apps/infoalloggi/src/server/services/email.service.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { TRPCError } from "@trpc/server";
2025-08-04 17:45:44 +02:00
import type { UsersId } from "~/schemas/public/Users";
import { db } from "~/server/db";
import { genMail, type MailsTemplates } from "~/server/services/mailer";
export const StoreEmail = async ({
2025-08-28 18:27:07 +02:00
userId,
data,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
userId: UsersId;
data: MailsTemplates;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.insertInto("emails")
.values({
user_id: userId,
data,
})
.returning("id_email")
.execute();
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Errore durante il salvataggio dell'email: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const GetEmails = async ({ userId }: { userId: UsersId }) => {
2025-08-28 18:27:07 +02:00
try {
const mails = await db
.selectFrom("emails")
.selectAll()
.where("user_id", "=", userId)
.orderBy("created_at", "desc")
.execute();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const gen = await Promise.all(
mails.map(async (mail) => {
return {
...(await genMail(mail.data as MailsTemplates)),
created_at: mail.created_at,
};
}),
);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return gen;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Errore durante il recupero delle email: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};