feat: refactor ordini and servizio controllers to use services, remove old controller files
This commit is contained in:
parent
2aa5ccea1d
commit
a8d29c6840
7 changed files with 208 additions and 199 deletions
|
|
@ -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[];
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -296,6 +296,7 @@ export const whIntentFailedHandler = async ({
|
|||
paymentstatus: PaymentStatusEnum.failed,
|
||||
})
|
||||
.returning("userid")
|
||||
.where("paymentstatus", "=", PaymentStatusEnum.processing)
|
||||
.where("intent_id", "=", pIntentId)
|
||||
.execute();
|
||||
|
||||
|
|
|
|||
183
apps/infoalloggi/src/server/services/servizio.service.ts
Normal file
183
apps/infoalloggi/src/server/services/servizio.service.ts
Normal 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}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue