2025-08-28 18:27:07 +02:00
|
|
|
import { TRPCError } from "@trpc/server";
|
2025-12-01 12:29:48 +01:00
|
|
|
import type { EmailsIdEmail } from "~/schemas/public/Emails";
|
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";
|
|
|
|
|
|
2025-12-29 16:00:57 +01:00
|
|
|
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({
|
|
|
|
|
data,
|
2025-08-29 16:18:32 +02:00
|
|
|
user_id: userId,
|
2025-08-28 18:27:07 +02:00
|
|
|
})
|
|
|
|
|
.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
|
|
|
};
|
|
|
|
|
|
2025-12-29 16:00:57 +01: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-12-01 12:29:48 +01:00
|
|
|
id_email: mail.id_email,
|
2025-08-28 18:27:07 +02:00
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
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
|
|
|
};
|
2025-12-01 12:29:48 +01:00
|
|
|
|
2025-12-29 16:00:57 +01:00
|
|
|
export const deleteEmail = async ({
|
2025-12-01 12:29:48 +01:00
|
|
|
id_email,
|
|
|
|
|
}: {
|
|
|
|
|
id_email: EmailsIdEmail;
|
|
|
|
|
}) => {
|
|
|
|
|
try {
|
|
|
|
|
await db.deleteFrom("emails").where("id_email", "=", id_email).execute();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
|
|
|
message: `Errore durante l'eliminazione dell'email: ${(e as Error).message}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-12-29 16:00:57 +01:00
|
|
|
|
|
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|