feat: enhance error handling with custom messages for database operations

This commit is contained in:
Marco Pedone 2026-03-05 10:29:08 +01:00
parent 728ba71c84
commit 8d89be35d5
11 changed files with 180 additions and 78 deletions

View file

@ -123,7 +123,9 @@ export const comunicazioniRouter = createTRPCRouter({
.selectFrom("users") .selectFrom("users")
.select("email") .select("email")
.where("id", "=", input.userId) .where("id", "=", input.userId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(`User not found - userId: ${input.userId}`);
});
const annuncio = await db const annuncio = await db
.selectFrom("annunci") .selectFrom("annunci")
.select([ .select([
@ -137,7 +139,10 @@ export const comunicazioniRouter = createTRPCRouter({
"numero", "numero",
]) ])
.where("id", "=", input.annuncioId) .where("id", "=", input.annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(`Annuncio not found - annuncioId: ${input.annuncioId}`),
);
await NewMail({ await NewMail({
template: { template: {
mailType: "emailContattiConScheda", mailType: "emailContattiConScheda",

View file

@ -39,7 +39,9 @@ export const messagesSSERouter = createTRPCRouter({
time: new Date(), time: new Date(),
}) })
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error("Failed to insert message into database"),
);
const userInfos = await db const userInfos = await db
.selectFrom("users") .selectFrom("users")
@ -193,7 +195,9 @@ export const messagesSSERouter = createTRPCRouter({
.selectFrom("messages") .selectFrom("messages")
.selectAll() .selectAll()
.where("messageid", "=", lastEventId) .where("messageid", "=", lastEventId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(`Message not found - messageId: ${lastEventId}`);
});
return messageById?.time ?? null; return messageById?.time ?? null;
})(); })();

View file

@ -440,7 +440,9 @@ export const editAnnuncioHandler = async ({
}) })
.where("id", "=", annuncioId) .where("id", "=", annuncioId)
.returning("codice") .returning("codice")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(`Annuncio not found - annuncioId: ${annuncioId}`);
});
await revalidate(`/annuncio/${annuncio.codice}`); await revalidate(`/annuncio/${annuncio.codice}`);
return true; return true;
} catch (e) { } catch (e) {
@ -485,7 +487,9 @@ export const getProprietarioDataHandler = async ({
"locatore", "locatore",
]) ])
.where("id", "=", annuncioId) .where("id", "=", annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(`Annuncio not found - annuncioId: ${annuncioId}`);
});
return { opened_at: opened_at.open_contatti_at, propData }; return { opened_at: opened_at.open_contatti_at, propData };
} catch (e) { } catch (e) {

View file

@ -167,7 +167,9 @@ export const getUserByChatHandler = async ({
.selectFrom("chats") .selectFrom("chats")
.select(["chatid", "created_at", "user_ref", getUserInfo()]) .select(["chatid", "created_at", "user_ref", getUserInfo()])
.where("chats.chatid", "=", chatId) .where("chats.chatid", "=", chatId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Chat not found - chatId: ${chatId}`),
);
if (!chat.userinfo) { if (!chat.userinfo) {
throw new TRPCError({ code: "NOT_FOUND" }); throw new TRPCError({ code: "NOT_FOUND" });

View file

@ -57,7 +57,9 @@ export const getOrdineById = async (ordineId: OrdiniOrdineId) => {
]) ])
.where("ordini.ordine_id", "=", ordineId) .where("ordini.ordine_id", "=", ordineId)
.orderBy("created_at", "desc") .orderBy("created_at", "desc")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Ordine not found - ordineId: ${ordineId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -73,13 +75,15 @@ export const createOrdine = async (data: NewOrdini) => {
.selectFrom("prezziario") .selectFrom("prezziario")
.select(["prezzo_cent", "nome_it"]) .select(["prezzo_cent", "nome_it"])
.where("prezziario.idprezziario", "=", data.packid) .where("prezziario.idprezziario", "=", data.packid)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Prezziario not found - packid: ${data.packid}`),
);
const order = await tx const order = await tx
.insertInto("ordini") .insertInto("ordini")
.values(data) .values(data)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => new Error("Failed to create ordine"));
await tx await tx
.insertInto("payments") .insertInto("payments")
.values({ .values({
@ -108,10 +112,7 @@ export const createOrdine = async (data: NewOrdini) => {
export const removeOrder = async (ordineId: OrdiniOrdineId) => { export const removeOrder = async (ordineId: OrdiniOrdineId) => {
try { try {
await db await db.deleteFrom("ordini").where("ordine_id", "=", ordineId).execute();
.deleteFrom("ordini")
.where("ordine_id", "=", ordineId)
.executeTakeFirstOrThrow();
return true; return true;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -134,7 +135,9 @@ export const updateOrder = async ({
.set(data) .set(data)
.where("ordine_id", "=", ordineId) .where("ordine_id", "=", ordineId)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Failed to update ordine - ordineId: ${ordineId}`),
);
return updated; return updated;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({

View file

@ -61,7 +61,9 @@ export const addServizio = async (data: NewServizio) => {
.insertInto("servizio") .insertInto("servizio")
.values(data) .values(data)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error("Failed to insert new servizio into database"),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -76,7 +78,9 @@ export const getServizioById = async (servizioId: ServizioServizioId) => {
.selectFrom("servizio") .selectFrom("servizio")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Servizio not found - servizioId: ${servizioId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
@ -98,7 +102,10 @@ export const updateServizio = async ({
.set(data) .set(data)
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(`Failed to update servizio - servizioId: ${servizioId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -112,7 +119,7 @@ export const deleteServizio = async (servizioId: ServizioServizioId) => {
await db await db
.deleteFrom("servizio") .deleteFrom("servizio")
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(); .execute();
return true; return true;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
@ -172,7 +179,9 @@ export const getPreOnboardData = async (servizioId: ServizioServizioId) => {
"users.telefono", "users.telefono",
]) ])
.where("id", "=", servizio.user_id) .where("id", "=", servizio.user_id)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`User not found - userId: ${servizio.user_id}`),
);
if (!userData) { if (!userData) {
throw new TRPCError({ throw new TRPCError({
@ -217,7 +226,9 @@ export const getDataPerAcquisto = async (servizioId: ServizioServizioId) => {
"users.telefono", "users.telefono",
]) ])
.where("id", "=", servizio.user_id) .where("id", "=", servizio.user_id)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`User not found - userId: ${servizio.user_id}`),
);
if (!userData) { if (!userData) {
throw new TRPCError({ throw new TRPCError({
@ -263,7 +274,7 @@ export const postAcquistoDataHandler = async ({
.updateTable("users") .updateTable("users")
.set(user) .set(user)
.where("id", "=", userId) .where("id", "=", userId)
.executeTakeFirstOrThrow(); .execute();
const { idanagrafica, ...rest } = anagrafica; const { idanagrafica, ...rest } = anagrafica;
if (idanagrafica) { if (idanagrafica) {
@ -275,7 +286,12 @@ export const postAcquistoDataHandler = async ({
}) })
.where("idanagrafica", "=", idanagrafica) .where("idanagrafica", "=", idanagrafica)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`Failed to update users_anagrafica - idanagrafica: ${idanagrafica}`,
),
);
} else { } else {
await tx await tx
.insertInto("users_anagrafica") .insertInto("users_anagrafica")
@ -284,7 +300,9 @@ export const postAcquistoDataHandler = async ({
userid: userId, userid: userId,
}) })
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error("Failed to insert new users_anagrafica"),
);
} }
// Update servizio data // Update servizio data
return await tx return await tx
@ -292,7 +310,10 @@ export const postAcquistoDataHandler = async ({
.set(servizio) .set(servizio)
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(`Failed to update servizio - servizioId: ${servizioId}`),
);
}); });
if (servizioData.skipPayment) { if (servizioData.skipPayment) {
@ -374,7 +395,7 @@ export const postAcquistoDataHandler = async ({
userid: userId, userid: userId,
}) })
.returning("ordine_id") .returning("ordine_id")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => new Error("Failed to create ordine"));
return { ordineIdForPayment: order.ordine_id }; return { ordineIdForPayment: order.ordine_id };
} catch (e) { } catch (e) {
@ -409,7 +430,9 @@ export const setupSaldoServizio = async ({
.selectFrom("servizio") .selectFrom("servizio")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Servizio not found - servizioId: ${servizioId}`),
);
if ( if (
!servizio.isOkAcconto || !servizio.isOkAcconto ||
@ -428,7 +451,12 @@ export const setupSaldoServizio = async ({
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("annunci_id", "=", annuncioId) .where("annunci_id", "=", annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`Annuncio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`,
),
);
if ( if (
annuncio.user_confirmed_at == null || annuncio.user_confirmed_at == null ||
annuncio.accettato_conferma_at == null annuncio.accettato_conferma_at == null
@ -498,7 +526,7 @@ export const setupSaldoServizio = async ({
userid: servizio.user_id, userid: servizio.user_id,
}) })
.returning("ordine_id") .returning("ordine_id")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => new Error("Failed to create ordine"));
return order.ordine_id; return order.ordine_id;
}); });
@ -633,7 +661,9 @@ export const setupSaldoConsulenzaServizio = async ({
.selectFrom("servizio") .selectFrom("servizio")
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Servizio not found - servizioId: ${servizioId}`),
);
if ( if (
!servizio.isOkAcconto || !servizio.isOkAcconto ||
@ -653,7 +683,12 @@ export const setupSaldoConsulenzaServizio = async ({
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("annunci_id", "=", annuncioId) .where("annunci_id", "=", annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`Annuncio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`,
),
);
if ( if (
annuncio.user_confirmed_at == null || annuncio.user_confirmed_at == null ||
annuncio.accettato_conferma_at == null || annuncio.accettato_conferma_at == null ||
@ -706,7 +741,7 @@ export const setupSaldoConsulenzaServizio = async ({
userid: servizio.user_id, userid: servizio.user_id,
}) })
.returning("ordine_id") .returning("ordine_id")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => new Error("Failed to create ordine"));
return order.ordine_id; return order.ordine_id;
}); });
@ -1133,7 +1168,12 @@ export const getServizioAnnuncio = async ({
.selectAll() .selectAll()
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("annunci_id", "=", annuncioId) .where("annunci_id", "=", annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`Servizio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`,
),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
@ -1422,7 +1462,7 @@ export const interruzioneServizio = async (servizioId: ServizioServizioId) => {
isInterrotto: true, isInterrotto: true,
}) })
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.executeTakeFirstOrThrow(); .execute();
// NOTIFICHE EMAIL INTERRUZIONE SERVIZIO // NOTIFICHE EMAIL INTERRUZIONE SERVIZIO
const utente = await getUser({ db, userId: servizio.user_id }); const utente = await getUser({ db, userId: servizio.user_id });
@ -1549,7 +1589,7 @@ export const userSendConfermaIntent = async ({
}) })
.where("servizio_id", "=", servizioId) .where("servizio_id", "=", servizioId)
.where("annunci_id", "=", annuncioId) .where("annunci_id", "=", annuncioId)
.executeTakeFirstOrThrow(); .execute();
// NOTIFICHE EMAIL CONFERMA IMMOBILE // NOTIFICHE EMAIL CONFERMA IMMOBILE
@ -1559,7 +1599,12 @@ export const userSendConfermaIntent = async ({
.selectFrom("annunci") .selectFrom("annunci")
.select("codice") .select("codice")
.where("id", "=", annuncioId) .where("id", "=", annuncioId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`Annuncio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`,
),
);
await NewMail({ await NewMail({
template: { template: {
@ -1751,13 +1796,17 @@ export const getServizioPagamentoData = async (ordineId: OrdiniOrdineId) => {
.selectFrom("ordini") .selectFrom("ordini")
.selectAll() .selectAll()
.where("ordine_id", "=", ordineId) .where("ordine_id", "=", ordineId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Ordine not found - ordineId: ${ordineId}`),
);
const pack = await db const pack = await db
.selectFrom("prezziario") .selectFrom("prezziario")
.selectAll() .selectAll()
.where("idprezziario", "=", ordine.packid) .where("idprezziario", "=", ordine.packid)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Prezziario not found - packid: ${ordine.packid}`),
);
const payment = await db const payment = await db
.insertInto("payments") .insertInto("payments")
@ -1769,13 +1818,18 @@ export const getServizioPagamentoData = async (ordineId: OrdiniOrdineId) => {
userid: ordine.userid, userid: ordine.userid,
}) })
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Failed to create payment - ordineId: ${ordineId}`),
);
const servizio = await db const servizio = await db
.selectFrom("servizio") .selectFrom("servizio")
.selectAll() .selectAll()
.where("servizio.servizio_id", "=", payment.servizio_id) .where("servizio.servizio_id", "=", payment.servizio_id)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(`Servizio not found - servizioId: ${payment.servizio_id}`),
);
if (!servizio) { if (!servizio) {
throw new TRPCError({ throw new TRPCError({
@ -1808,7 +1862,9 @@ export const getPagamentoDataForCheckout = async (paymentId: PaymentsId) => {
.innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid") .innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid")
.select(["prezziario.nome_it", "prezziario.desc_it"]) .select(["prezziario.nome_it", "prezziario.desc_it"])
.where("payments.id", "=", paymentId) .where("payments.id", "=", paymentId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Payment not found - paymentId: ${paymentId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
@ -1848,7 +1904,9 @@ export const updatePagamento = async ({
.set(data) .set(data)
.where("id", "=", paymentId) .where("id", "=", paymentId)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Failed to update pagamento - paymentId: ${paymentId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -2065,7 +2123,9 @@ export const getOrdineRicevutaData = async ({
.where("payments.ordine_id", "=", ordineId) .where("payments.ordine_id", "=", ordineId)
.where("payments.paymentstatus", "=", PaymentStatusEnum.success) .where("payments.paymentstatus", "=", PaymentStatusEnum.success)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Ordine not found - ordineId: ${ordineId}`),
);
if (!data || !data.user || !data.anagrafica) { if (!data || !data.user || !data.anagrafica) {
throw new TRPCError({ throw new TRPCError({

View file

@ -96,7 +96,12 @@ export const getStorageIdFromUserStorageId = async ({
.selectFrom("users_storage") .selectFrom("users_storage")
.select("storageId") .select("storageId")
.where("user_storage_id", "=", userStorageId) .where("user_storage_id", "=", userStorageId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() =>
new Error(
`User storage entry not found - userStorageId: ${userStorageId}`,
),
);
return storage.storageId; return storage.storageId;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({

View file

@ -9,10 +9,6 @@ import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum";
import { db } from "~/server/db"; import { db } from "~/server/db";
import { NewMail } from "~/server/services/mailer"; import { NewMail } from "~/server/services/mailer";
import { getUser } from "~/server/services/user.service"; import { getUser } from "~/server/services/user.service";
import {
genPdfCondizioniBase64,
genPdfRicevutaCortesiaBase64,
} from "../services/puppeteer.service";
import { addEventToQueue } from "./event_queue.controller"; import { addEventToQueue } from "./event_queue.controller";
export const stripeHealthCheck = async () => { export const stripeHealthCheck = async () => {
@ -50,7 +46,9 @@ export const createIntentHandler = async ({
.innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid") .innerJoin("prezziario", "prezziario.idprezziario", "ordini.packid")
.select(["prezziario.nome_it", "prezziario.desc_it"]) .select(["prezziario.nome_it", "prezziario.desc_it"])
.where("payments.id", "=", paymentId) .where("payments.id", "=", paymentId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Payment not found - paymentId: ${paymentId}`),
);
const paymentIntent = await stripe.paymentIntents.create({ const paymentIntent = await stripe.paymentIntents.create({
amount: payment.amount_cent, amount: payment.amount_cent,
@ -130,13 +128,19 @@ export const whIntentSucceededHandler = async ({
.where("intent_id", "=", pIntentId) .where("intent_id", "=", pIntentId)
.where("payments.id", "=", paymentId) .where("payments.id", "=", paymentId)
.returning(["servizio_id", "ordine_id"]) .returning(["servizio_id", "ordine_id"])
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(
`Payment not found - intent_id: ${pIntentId}, paymentId: ${paymentId}`,
);
});
const ordine = await trx const ordine = await trx
.selectFrom("ordini") .selectFrom("ordini")
.selectAll("ordini") .selectAll("ordini")
.where("ordine_id", "=", payment.ordine_id) .where("ordine_id", "=", payment.ordine_id)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(`Ordine not found - ordine_id: ${payment.ordine_id}`);
});
await trx await trx
.updateTable("ordini") .updateTable("ordini")
@ -150,12 +154,17 @@ export const whIntentSucceededHandler = async ({
.selectFrom("servizio") .selectFrom("servizio")
.select("servizio.tipologia") .select("servizio.tipologia")
.where("servizio_id", "=", payment.servizio_id) .where("servizio_id", "=", payment.servizio_id)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(() => {
throw new Error(
`Servizio not found - servizio_id: ${payment.servizio_id}`,
);
});
const utente = await getUser({ const utente = await getUser({
db: trx, db: trx,
userId: ordine.userid, userId: ordine.userid,
}); });
const lock_expires = new Date(Date.now() + 60 * 1000); // Lock for 1 minute
switch (ordine.type) { switch (ordine.type) {
case OrderTypeEnum.Acconto: case OrderTypeEnum.Acconto:
@ -168,7 +177,6 @@ export const whIntentSucceededHandler = async ({
}) })
.execute(); .execute();
//TODO inserire gli allegati solo se si ha il pdf generato
await addEventToQueue({ await addEventToQueue({
data: { data: {
tipo: "email", tipo: "email",
@ -183,9 +191,7 @@ export const whIntentSucceededHandler = async ({
{ {
filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`, filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`,
//ATTENTION: hardcoded condizioni key //ATTENTION: hardcoded condizioni key
// content: await genPdfCondizioniBase64(
// "CONDIZIONI_CERCHI" as TestiEStringheStingaId,
// ),
generate: { generate: {
type: "condizioni", type: "condizioni",
stringId: "CONDIZIONI_CERCHI" as TestiEStringheStingaId, stringId: "CONDIZIONI_CERCHI" as TestiEStringheStingaId,
@ -195,9 +201,7 @@ export const whIntentSucceededHandler = async ({
}, },
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
// content: await genPdfRicevutaCortesiaBase64(
// ordine.ordine_id,
// ),
generate: { generate: {
type: "ricevuta_cortesia", type: "ricevuta_cortesia",
ordineId: ordine.ordine_id, ordineId: ordine.ordine_id,
@ -210,7 +214,7 @@ export const whIntentSucceededHandler = async ({
userId: utente.id, userId: utente.id,
}, },
}, },
lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute lock_expires,
}); });
//SERVIZIO ATTIVATO //SERVIZIO ATTIVATO
@ -243,9 +247,7 @@ export const whIntentSucceededHandler = async ({
type: "ricevuta_cortesia", type: "ricevuta_cortesia",
ordineId: ordine.ordine_id, ordineId: ordine.ordine_id,
}, },
// content: await genPdfRicevutaCortesiaBase64(
// ordine.ordine_id,
// ),
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },
@ -254,7 +256,7 @@ export const whIntentSucceededHandler = async ({
userId: utente.id, userId: utente.id,
}, },
}, },
lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute lock_expires,
}); });
//PAGAMENTO SALDO CONFERMATO //PAGAMENTO SALDO CONFERMATO
break; break;
@ -279,9 +281,7 @@ export const whIntentSucceededHandler = async ({
attachments: [ attachments: [
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
// content: await genPdfRicevutaCortesiaBase64(
// ordine.ordine_id,
// ),
generate: { generate: {
type: "ricevuta_cortesia", type: "ricevuta_cortesia",
ordineId: ordine.ordine_id, ordineId: ordine.ordine_id,
@ -294,7 +294,7 @@ export const whIntentSucceededHandler = async ({
userId: utente.id, userId: utente.id,
}, },
}, },
lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute lock_expires,
}); });
//CONSULENZA ATTIVATA //CONSULENZA ATTIVATA
break; break;
@ -313,9 +313,7 @@ export const whIntentSucceededHandler = async ({
attachments: [ attachments: [
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
// content: await genPdfRicevutaCortesiaBase64(
// ordine.ordine_id,
// ),
generate: { generate: {
type: "ricevuta_cortesia", type: "ricevuta_cortesia",
ordineId: ordine.ordine_id, ordineId: ordine.ordine_id,
@ -328,16 +326,25 @@ export const whIntentSucceededHandler = async ({
userId: utente.id, userId: utente.id,
}, },
}, },
lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute lock_expires,
}); });
break; break;
} }
}); });
return true; return true;
} catch (error) { } catch (error) {
// Enhanced error logging
console.error("whIntentSucceededHandler error:", {
pIntentId,
paymentId,
pm_id,
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
message: `Error updating payment intent: ${(error as Error).message}`, message: `Error updating payment intent: ${(error as Error).message}`,
cause: error,
}); });
} }
}; };

View file

@ -21,7 +21,9 @@ export const getRaw = async ({
.selectFrom("chats") .selectFrom("chats")
.selectAll() .selectAll()
.where("chats.chatid", "=", chatId) .where("chats.chatid", "=", chatId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Chat not found - chatId: ${chatId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
@ -39,7 +41,9 @@ export const add = async ({ db, userId }: { db: Querier; userId: UsersId }) => {
user_ref: userId, user_ref: userId,
}) })
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Error while creating chat for userId: ${userId}`),
);
return newChat; return newChat;
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({

View file

@ -56,7 +56,9 @@ export const addPrezziario = async ({
.insertInto("prezziario") .insertInto("prezziario")
.values(input) .values(input)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Failed to add prezziario with name: ${input.nome_it}`),
);
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -79,7 +81,9 @@ export const updatePrezziario = async ({
.set(input) .set(input)
.where("prezziario.idprezziario", "=", prezziarioId) .where("prezziario.idprezziario", "=", prezziarioId)
.returningAll() .returningAll()
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`Failed to update prezziario with id: ${prezziarioId}`),
);
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",

View file

@ -71,7 +71,9 @@ export const getUser = async ({
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("id", "=", userId) .where("id", "=", userId)
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`User not found - userId: ${userId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
@ -160,7 +162,9 @@ export const getClientProfilo = async ({
), ),
).as("doc_personale_retro"), ).as("doc_personale_retro"),
]) ])
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow(
() => new Error(`User not found - userId: ${userId}`),
);
} catch (e) { } catch (e) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",