refactor: update TypeScript SDK path and improve error handling in servizio service
This commit is contained in:
parent
985a302a05
commit
41d743c97f
4 changed files with 146 additions and 151 deletions
3
apps/infoalloggi/.vscode/settings.json
vendored
3
apps/infoalloggi/.vscode/settings.json
vendored
|
|
@ -17,5 +17,6 @@
|
|||
"username": "postgres"
|
||||
}
|
||||
],
|
||||
"typescript.tsdk": "node_modules\\typescript\\lib"
|
||||
"js/ts.tsdk.path": "node_modules\\typescript\\lib",
|
||||
"biome.lsp.trace.server": "verbose",
|
||||
}
|
||||
|
|
@ -121,9 +121,7 @@ const RinnovoLinkCard = ({
|
|||
key={data.id}
|
||||
>
|
||||
<Card
|
||||
className={cn(
|
||||
"transition-shadow hover:bg-muted/15 hover:shadow-md",
|
||||
)}
|
||||
className={cn("transition-shadow hover:bg-muted/15 hover:shadow-md")}
|
||||
>
|
||||
<CardHeader>
|
||||
<CardTitle className="group-hover:underline">
|
||||
|
|
@ -134,7 +132,6 @@ const RinnovoLinkCard = ({
|
|||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col flex-wrap gap-4 sm:flex-row sm:justify-between">
|
||||
|
||||
<div className="flex items-center gap-2 rounded-md text-muted-foreground group-hover:text-foreground">
|
||||
<span>Vai al rinnovo</span>
|
||||
<ArrowRight />
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ import {
|
|||
FormEditServizio,
|
||||
} from "~/forms/FormEditServizioAdmin";
|
||||
import { FormNewOrder } from "~/forms/FormNewOrdine";
|
||||
import { FormNewServizio, type FormValues } from "~/forms/FormNewServizio";
|
||||
import { FormServizio, type FormValues } from "~/forms/FormServizio";
|
||||
import { cn, formatCurrency } from "~/lib/utils";
|
||||
import { useTranslation } from "~/providers/I18nProvider";
|
||||
import { useServizio } from "~/providers/ServizioProvider";
|
||||
|
|
@ -696,7 +696,7 @@ const EditParametri = () => {
|
|||
</CredenzaDescription>
|
||||
</CredenzaHeader>
|
||||
<CredenzaBody className="max-h-[80vh] w-full max-w-3xl overflow-y-auto pb-5 sm:max-w-5xl">
|
||||
<FormNewServizio
|
||||
<FormServizio
|
||||
initialData={servizio}
|
||||
isLimited={!isAdmin}
|
||||
onSubmit={onSubmit}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,24 @@
|
|||
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 OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
|
||||
import type {
|
||||
NewServizio,
|
||||
ServizioServizioId,
|
||||
ServizioUpdate,
|
||||
} from "~/schemas/public/Servizio";
|
||||
import type { ServizioAnnunciUpdate } from "~/schemas/public/ServizioAnnunci";
|
||||
import { db } from "../db";
|
||||
|
||||
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"),
|
||||
);
|
||||
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 db
|
||||
.selectFrom("prezziario")
|
||||
|
|
@ -29,160 +29,157 @@ export const addServizio = async (data: NewServizio) => {
|
|||
.limit(1)
|
||||
.executeTakeFirst();
|
||||
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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,
|
||||
data,
|
||||
}: {
|
||||
servizioId: ServizioServizioId;
|
||||
data: ServizioUpdate;
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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,
|
||||
annuncioId,
|
||||
}: {
|
||||
servizioId: ServizioServizioId;
|
||||
annuncioId: AnnunciId;
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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,
|
||||
annuncioId,
|
||||
data,
|
||||
}: {
|
||||
servizioId: ServizioServizioId;
|
||||
annuncioId: AnnunciId;
|
||||
data: ServizioAnnunciUpdate;
|
||||
servizioId: ServizioServizioId;
|
||||
annuncioId: AnnunciId;
|
||||
data: ServizioAnnunciUpdate;
|
||||
}) => {
|
||||
try {
|
||||
await db
|
||||
.updateTable("servizio_annunci")
|
||||
.set(data)
|
||||
.where("servizio_id", "=", servizioId)
|
||||
.where("annunci_id", "=", annuncioId)
|
||||
.execute();
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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,
|
||||
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}`,
|
||||
});
|
||||
}
|
||||
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