feat: refactor ordini and servizio controllers to use services, remove old controller files

This commit is contained in:
Marco Pedone 2026-03-08 14:34:21 +01:00
parent 2aa5ccea1d
commit a8d29c6840
7 changed files with 208 additions and 199 deletions

View file

@ -18,7 +18,7 @@ import OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
import type { OrdiniOrdineId } from "~/schemas/public/Ordini"; import type { OrdiniOrdineId } from "~/schemas/public/Ordini";
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum"; import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
import type { UsersId } from "~/schemas/public/Users"; 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 = { type PaymentsTableProps = {
data: Ordini_w_Utente[]; data: Ordini_w_Utente[];

View file

@ -10,39 +10,41 @@ import {
overridableProcedure, overridableProcedure,
protectedProcedure, protectedProcedure,
} from "~/server/api/trpc"; } from "~/server/api/trpc";
import {
createOrdine,
getOrdineById,
getOrdini,
removeOrder,
updateOrder,
} from "~/server/controllers/ordini.controller";
import { getPacksPerServizio } from "~/server/controllers/pagamenti.controller"; import { getPacksPerServizio } from "~/server/controllers/pagamenti.controller";
import { import {
addServizio,
addServizioAnnunci, addServizioAnnunci,
adminUpdateConfermaStatus, adminUpdateConfermaStatus,
attivaServizio_SaltaPagamentoAcconto, attivaServizio_SaltaPagamentoAcconto,
deleteServizio,
deleteServizioAnnuncio,
getAllServizioAnnunci, getAllServizioAnnunci,
getAnnunciAvailableToAdd, getAnnunciAvailableToAdd,
getCompatibileAnnunci, getCompatibileAnnunci,
getDataPerAcquisto, getDataPerAcquisto,
getOrdineRicevutaData, getOrdineRicevutaData,
getServiziByUserId, getServiziByUserId,
getServizioAnnuncio,
getServizioById,
getServizioDataById, getServizioDataById,
InteractionLogicTipologia, InteractionLogicTipologia,
interruzioneServizio, interruzioneServizio,
processServizioOnboard, processServizioOnboard,
SbloccaContatti, SbloccaContatti,
updateServizio,
updateServizioAnnuncio,
userAccettaConferma, userAccettaConferma,
userSendConfermaIntent, userSendConfermaIntent,
} from "~/server/controllers/servizio.controller"; } 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 { import {
zAnnuncioId, zAnnuncioId,
zOrdineId, zOrdineId,

View file

@ -8,9 +8,9 @@ import type {
import type { Servizio, ServizioServizioId } from "~/schemas/public/Servizio"; import type { Servizio, ServizioServizioId } from "~/schemas/public/Servizio";
import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum";
import { db } from "../db"; import { db } from "../db";
import { createOrdine } from "../services/ordini.service";
import { getPrezziarioByIdHandler } from "../services/prezziario.service"; import { getPrezziarioByIdHandler } from "../services/prezziario.service";
import { createOrdine } from "./ordini.controller"; import { getServizioById } from "../services/servizio.service";
import { getServizioById } from "./servizio.controller";
export const ACCONTO_TRANSITORIO = "ACCONTO_BD" as PrezziarioIdprezziario; export const ACCONTO_TRANSITORIO = "ACCONTO_BD" as PrezziarioIdprezziario;
export const ACCONTO_STABILE = "ACCONTO_CA" as PrezziarioIdprezziario; export const ACCONTO_STABILE = "ACCONTO_CA" as PrezziarioIdprezziario;

View file

@ -10,17 +10,13 @@ import type { Ordini, OrdiniOrdineId } from "~/schemas/public/Ordini";
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum"; import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
import type { Prezziario } from "~/schemas/public/Prezziario"; import type { Prezziario } from "~/schemas/public/Prezziario";
import type { import type {
NewServizio,
Servizio, Servizio,
ServizioServizioId, ServizioServizioId,
ServizioUpdate, ServizioUpdate,
} from "~/schemas/public/Servizio"; } from "~/schemas/public/Servizio";
import type { import type { ServizioAnnunci } from "~/schemas/public/ServizioAnnunci";
ServizioAnnunci,
ServizioAnnunciUpdate,
} from "~/schemas/public/ServizioAnnunci";
import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe"; 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 { Users, UsersId } from "~/schemas/public/Users";
import type { UsersAnagraficaUpdate } from "~/schemas/public/UsersAnagrafica"; import type { UsersAnagraficaUpdate } from "~/schemas/public/UsersAnagrafica";
import type { UsersStorageUserStorageId } from "~/schemas/public/UsersStorage"; import type { UsersStorageUserStorageId } from "~/schemas/public/UsersStorage";
@ -42,112 +38,11 @@ import {
withVideos, withVideos,
} from "~/utils/kysely-helper"; } from "~/utils/kysely-helper";
import { GetUserInterests } from "../services/interests.service"; 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 { genPdfCondizioniBase64 } from "../services/puppeteer.service";
import { getServizioById } from "../services/servizio.service";
import type { AnnuncioRicerca } from "./annunci.controller"; import type { AnnuncioRicerca } from "./annunci.controller";
import { createOrdine } from "./ordini.controller"; import { SaldoSolver } from "./pagamenti.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}`,
});
}
};
export const getServiziByUserId = async (userId: UsersId) => { export const getServiziByUserId = async (userId: UsersId) => {
try { 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 ( export const getAnnunciAvailableToAdd = async (
servizioId: ServizioServizioId, 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 ({ export const SbloccaContatti = async ({
servizioId, servizioId,
annuncioId, annuncioId,

View file

@ -296,6 +296,7 @@ export const whIntentFailedHandler = async ({
paymentstatus: PaymentStatusEnum.failed, paymentstatus: PaymentStatusEnum.failed,
}) })
.returning("userid") .returning("userid")
.where("paymentstatus", "=", PaymentStatusEnum.processing)
.where("intent_id", "=", pIntentId) .where("intent_id", "=", pIntentId)
.execute(); .execute();

View file

@ -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}`,
});
}
};