From a8d29c68403f2a23e2b76eabb2cce49e9e1c3d01 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Sun, 8 Mar 2026 14:34:21 +0100 Subject: [PATCH] feat: refactor ordini and servizio controllers to use services, remove old controller files --- .../src/components/tables/orders-table.tsx | 2 +- .../src/server/api/routers/servizio.ts | 30 +-- .../controllers/pagamenti.controller.ts | 4 +- .../server/controllers/servizio.controller.ts | 187 +----------------- .../server/controllers/stripe.controller.ts | 1 + .../ordini.service.ts} | 0 .../src/server/services/servizio.service.ts | 183 +++++++++++++++++ 7 files changed, 208 insertions(+), 199 deletions(-) rename apps/infoalloggi/src/server/{controllers/ordini.controller.ts => services/ordini.service.ts} (100%) create mode 100644 apps/infoalloggi/src/server/services/servizio.service.ts diff --git a/apps/infoalloggi/src/components/tables/orders-table.tsx b/apps/infoalloggi/src/components/tables/orders-table.tsx index 808cb7b..c33ca1a 100644 --- a/apps/infoalloggi/src/components/tables/orders-table.tsx +++ b/apps/infoalloggi/src/components/tables/orders-table.tsx @@ -18,7 +18,7 @@ import OrderTypeEnum from "~/schemas/public/OrderTypeEnum"; import type { OrdiniOrdineId } from "~/schemas/public/Ordini"; import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum"; import type { UsersId } from "~/schemas/public/Users"; -import type { Ordini_w_Utente } from "~/server/controllers/ordini.controller"; +import type { Ordini_w_Utente } from "~/server/services/ordini.service"; type PaymentsTableProps = { data: Ordini_w_Utente[]; diff --git a/apps/infoalloggi/src/server/api/routers/servizio.ts b/apps/infoalloggi/src/server/api/routers/servizio.ts index 9997a94..d510b51 100644 --- a/apps/infoalloggi/src/server/api/routers/servizio.ts +++ b/apps/infoalloggi/src/server/api/routers/servizio.ts @@ -10,39 +10,41 @@ import { overridableProcedure, protectedProcedure, } from "~/server/api/trpc"; -import { - createOrdine, - getOrdineById, - getOrdini, - removeOrder, - updateOrder, -} from "~/server/controllers/ordini.controller"; import { getPacksPerServizio } from "~/server/controllers/pagamenti.controller"; import { - addServizio, addServizioAnnunci, adminUpdateConfermaStatus, attivaServizio_SaltaPagamentoAcconto, - deleteServizio, - deleteServizioAnnuncio, getAllServizioAnnunci, getAnnunciAvailableToAdd, getCompatibileAnnunci, getDataPerAcquisto, getOrdineRicevutaData, getServiziByUserId, - getServizioAnnuncio, - getServizioById, getServizioDataById, InteractionLogicTipologia, interruzioneServizio, processServizioOnboard, SbloccaContatti, - updateServizio, - updateServizioAnnuncio, userAccettaConferma, userSendConfermaIntent, } from "~/server/controllers/servizio.controller"; +import { + createOrdine, + getOrdineById, + getOrdini, + removeOrder, + updateOrder, +} from "~/server/services/ordini.service"; +import { + addServizio, + deleteServizio, + deleteServizioAnnuncio, + getServizioAnnuncio, + getServizioById, + updateServizio, + updateServizioAnnuncio, +} from "~/server/services/servizio.service"; import { zAnnuncioId, zOrdineId, diff --git a/apps/infoalloggi/src/server/controllers/pagamenti.controller.ts b/apps/infoalloggi/src/server/controllers/pagamenti.controller.ts index 7177a37..8c13655 100644 --- a/apps/infoalloggi/src/server/controllers/pagamenti.controller.ts +++ b/apps/infoalloggi/src/server/controllers/pagamenti.controller.ts @@ -8,9 +8,9 @@ import type { import type { Servizio, ServizioServizioId } from "~/schemas/public/Servizio"; import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; import { db } from "../db"; +import { createOrdine } from "../services/ordini.service"; import { getPrezziarioByIdHandler } from "../services/prezziario.service"; -import { createOrdine } from "./ordini.controller"; -import { getServizioById } from "./servizio.controller"; +import { getServizioById } from "../services/servizio.service"; export const ACCONTO_TRANSITORIO = "ACCONTO_BD" as PrezziarioIdprezziario; export const ACCONTO_STABILE = "ACCONTO_CA" as PrezziarioIdprezziario; diff --git a/apps/infoalloggi/src/server/controllers/servizio.controller.ts b/apps/infoalloggi/src/server/controllers/servizio.controller.ts index 9a3521a..0456e9b 100644 --- a/apps/infoalloggi/src/server/controllers/servizio.controller.ts +++ b/apps/infoalloggi/src/server/controllers/servizio.controller.ts @@ -10,17 +10,13 @@ import type { Ordini, OrdiniOrdineId } from "~/schemas/public/Ordini"; import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum"; import type { Prezziario } from "~/schemas/public/Prezziario"; import type { - NewServizio, Servizio, ServizioServizioId, ServizioUpdate, } from "~/schemas/public/Servizio"; -import type { - ServizioAnnunci, - ServizioAnnunciUpdate, -} from "~/schemas/public/ServizioAnnunci"; +import type { ServizioAnnunci } from "~/schemas/public/ServizioAnnunci"; import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe"; -import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; +import type TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; import type { Users, UsersId } from "~/schemas/public/Users"; import type { UsersAnagraficaUpdate } from "~/schemas/public/UsersAnagrafica"; import type { UsersStorageUserStorageId } from "~/schemas/public/UsersStorage"; @@ -42,112 +38,11 @@ import { withVideos, } from "~/utils/kysely-helper"; import { GetUserInterests } from "../services/interests.service"; -import { getPrezziarioByIdHandler } from "../services/prezziario.service"; +import { createOrdine } from "../services/ordini.service"; import { genPdfCondizioniBase64 } from "../services/puppeteer.service"; +import { getServizioById } from "../services/servizio.service"; import type { AnnuncioRicerca } from "./annunci.controller"; -import { createOrdine } from "./ordini.controller"; -import { - ACCONTO_STABILE, - ACCONTO_TRANSITORIO, - SaldoSolver, -} from "./pagamenti.controller"; - -export const addServizio = async (data: NewServizio) => { - try { - return await db.transaction().execute(async (tx) => { - const servizio = await tx - .insertInto("servizio") - .values(data) - .returningAll() - .executeTakeFirstOrThrow( - () => new Error("Failed to insert new servizio into database"), - ); - - const acconto = await getPrezziarioByIdHandler( - data.tipologia === TipologiaPosizioneEnum.Transitorio - ? ACCONTO_TRANSITORIO - : ACCONTO_STABILE, - ); - if (!acconto) { - throw new Error("Acconto not found for new servizio"); - } - await tx - .insertInto("ordini") - .values({ - servizio_id: servizio.servizio_id, - userid: servizio.user_id, - type: OrderTypeEnum.Acconto, - amount_cent: acconto.prezzo_cent, - packid: acconto.idprezziario, - isActive: data.skipPayment || false, - }) - .execute(); - return servizio; - }); - } catch (e) { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: `Error adding new servizio: ${(e as Error).message}`, - }); - } -}; - -export const getServizioById = async (servizioId: ServizioServizioId) => { - try { - return await db - .selectFrom("servizio") - .selectAll() - .where("servizio_id", "=", servizioId) - .executeTakeFirstOrThrow( - () => new Error(`Servizio not found - servizioId: ${servizioId}`), - ); - } catch (e) { - throw new TRPCError({ - code: "NOT_FOUND", - message: `Servizio not found: ${(e as Error).message}`, - }); - } -}; - -export const updateServizio = async ({ - servizioId, - data, -}: { - servizioId: ServizioServizioId; - data: ServizioUpdate; -}) => { - try { - return await db - .updateTable("servizio") - .set(data) - .where("servizio_id", "=", servizioId) - .returningAll() - .executeTakeFirstOrThrow( - () => - new Error(`Failed to update servizio - servizioId: ${servizioId}`), - ); - } catch (e) { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: `Error updating servizio: ${(e as Error).message}`, - }); - } -}; - -export const deleteServizio = async (servizioId: ServizioServizioId) => { - try { - await db - .deleteFrom("servizio") - .where("servizio_id", "=", servizioId) - .execute(); - return true; - } catch (e) { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: `Error deleting servizio: ${(e as Error).message}`, - }); - } -}; +import { SaldoSolver } from "./pagamenti.controller"; export const getServiziByUserId = async (userId: UsersId) => { try { @@ -752,59 +647,6 @@ export const addServizioAnnunci = async ({ } }; -export const getServizioAnnuncio = async ({ - servizioId, - annuncioId, -}: { - servizioId: ServizioServizioId; - annuncioId: AnnunciId; -}) => { - try { - return await db - .selectFrom("servizio_annunci") - .selectAll() - .where("servizio_id", "=", servizioId) - .where("annunci_id", "=", annuncioId) - .executeTakeFirstOrThrow( - () => - new Error( - `Servizio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`, - ), - ); - } catch (e) { - throw new TRPCError({ - code: "NOT_FOUND", - message: `Servizio not found: ${(e as Error).message}`, - }); - } -}; - -export const updateServizioAnnuncio = async ({ - servizioId, - annuncioId, - data, -}: { - servizioId: ServizioServizioId; - annuncioId: AnnunciId; - data: ServizioAnnunciUpdate; -}) => { - try { - await db - .updateTable("servizio_annunci") - .set(data) - .where("servizio_id", "=", servizioId) - .where("annunci_id", "=", annuncioId) - .execute(); - - return true; - } catch (e) { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: `Error updating servizio from annuncio: ${(e as Error).message}`, - }); - } -}; - export const getAnnunciAvailableToAdd = async ( servizioId: ServizioServizioId, ) => { @@ -836,25 +678,6 @@ export const getAnnunciAvailableToAdd = async ( } }; -export const deleteServizioAnnuncio = async ( - servizioId: ServizioServizioId, - annuncioId: AnnunciId, -) => { - try { - await db - .deleteFrom("servizio_annunci") - .where("servizio_id", "=", servizioId) - .where("annunci_id", "=", annuncioId) - .execute(); - return true; - } catch (e) { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: `Error deleting servizio from annuncio: ${(e as Error).message}`, - }); - } -}; - export const SbloccaContatti = async ({ servizioId, annuncioId, diff --git a/apps/infoalloggi/src/server/controllers/stripe.controller.ts b/apps/infoalloggi/src/server/controllers/stripe.controller.ts index d8f2aa8..e02bcc1 100644 --- a/apps/infoalloggi/src/server/controllers/stripe.controller.ts +++ b/apps/infoalloggi/src/server/controllers/stripe.controller.ts @@ -296,6 +296,7 @@ export const whIntentFailedHandler = async ({ paymentstatus: PaymentStatusEnum.failed, }) .returning("userid") + .where("paymentstatus", "=", PaymentStatusEnum.processing) .where("intent_id", "=", pIntentId) .execute(); diff --git a/apps/infoalloggi/src/server/controllers/ordini.controller.ts b/apps/infoalloggi/src/server/services/ordini.service.ts similarity index 100% rename from apps/infoalloggi/src/server/controllers/ordini.controller.ts rename to apps/infoalloggi/src/server/services/ordini.service.ts diff --git a/apps/infoalloggi/src/server/services/servizio.service.ts b/apps/infoalloggi/src/server/services/servizio.service.ts new file mode 100644 index 0000000..cf9a2d1 --- /dev/null +++ b/apps/infoalloggi/src/server/services/servizio.service.ts @@ -0,0 +1,183 @@ +import { TRPCError } from "@trpc/server"; +import { db } from "../db"; +import type { NewServizio, ServizioServizioId, ServizioUpdate } from "~/schemas/public/Servizio"; +import OrderTypeEnum from "~/schemas/public/OrderTypeEnum"; +import { ACCONTO_STABILE, ACCONTO_TRANSITORIO } from "../controllers/pagamenti.controller"; +import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; +import { getPrezziarioByIdHandler } from "./prezziario.service"; +import type { AnnunciId } from "~/schemas/public/Annunci"; +import type { ServizioAnnunciUpdate } from "~/schemas/public/ServizioAnnunci"; + +export const addServizio = async (data: NewServizio) => { + try { + return await db.transaction().execute(async (tx) => { + const servizio = await tx + .insertInto("servizio") + .values(data) + .returningAll() + .executeTakeFirstOrThrow( + () => new Error("Failed to insert new servizio into database"), + ); + + const acconto = await getPrezziarioByIdHandler( + data.tipologia === TipologiaPosizioneEnum.Transitorio + ? ACCONTO_TRANSITORIO + : ACCONTO_STABILE, + ); + if (!acconto) { + throw new Error("Acconto not found for new servizio"); + } + await tx + .insertInto("ordini") + .values({ + servizio_id: servizio.servizio_id, + userid: servizio.user_id, + type: OrderTypeEnum.Acconto, + amount_cent: acconto.prezzo_cent, + packid: acconto.idprezziario, + isActive: data.skipPayment || false, + }) + .execute(); + return servizio; + }); + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error adding new servizio: ${(e as Error).message}`, + }); + } +}; + +export const getServizioById = async (servizioId: ServizioServizioId) => { + try { + return await db + .selectFrom("servizio") + .selectAll() + .where("servizio_id", "=", servizioId) + .executeTakeFirstOrThrow( + () => new Error(`Servizio not found - servizioId: ${servizioId}`), + ); + } catch (e) { + throw new TRPCError({ + code: "NOT_FOUND", + message: `Servizio not found: ${(e as Error).message}`, + }); + } +}; + +export const updateServizio = async ({ + servizioId, + data, +}: { + servizioId: ServizioServizioId; + data: ServizioUpdate; +}) => { + try { + return await db + .updateTable("servizio") + .set(data) + .where("servizio_id", "=", servizioId) + .returningAll() + .executeTakeFirstOrThrow( + () => + new Error(`Failed to update servizio - servizioId: ${servizioId}`), + ); + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error updating servizio: ${(e as Error).message}`, + }); + } +}; + +export const deleteServizio = async (servizioId: ServizioServizioId) => { + try { + await db + .deleteFrom("servizio") + .where("servizio_id", "=", servizioId) + .execute(); + return true; + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error deleting servizio: ${(e as Error).message}`, + }); + } +}; + + + + +////// SERVIZIO ANNUNCI + +export const getServizioAnnuncio = async ({ + servizioId, + annuncioId, +}: { + servizioId: ServizioServizioId; + annuncioId: AnnunciId; +}) => { + try { + return await db + .selectFrom("servizio_annunci") + .selectAll() + .where("servizio_id", "=", servizioId) + .where("annunci_id", "=", annuncioId) + .executeTakeFirstOrThrow( + () => + new Error( + `Servizio not found - servizioId: ${servizioId}, annunciId: ${annuncioId}`, + ), + ); + } catch (e) { + throw new TRPCError({ + code: "NOT_FOUND", + message: `Servizio not found: ${(e as Error).message}`, + }); + } +}; + +export const updateServizioAnnuncio = async ({ + servizioId, + annuncioId, + data, +}: { + servizioId: ServizioServizioId; + annuncioId: AnnunciId; + data: ServizioAnnunciUpdate; +}) => { + try { + await db + .updateTable("servizio_annunci") + .set(data) + .where("servizio_id", "=", servizioId) + .where("annunci_id", "=", annuncioId) + .execute(); + + return true; + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error updating servizio from annuncio: ${(e as Error).message}`, + }); + } +}; + +export const deleteServizioAnnuncio = async ( + servizioId: ServizioServizioId, + annuncioId: AnnunciId, +) => { + try { + await db + .deleteFrom("servizio_annunci") + .where("servizio_id", "=", servizioId) + .where("annunci_id", "=", annuncioId) + .execute(); + return true; + } catch (e) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error deleting servizio from annuncio: ${(e as Error).message}`, + }); + } +}; \ No newline at end of file