infoalloggi-monorepo/apps/infoalloggi/src/server/services/ordini.service.ts

124 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { TRPCError } from "@trpc/server";
import type { Insertable, Selectable, Updateable } from "kysely";
import type OrdiniTable from "~/schemas/public/Ordini";
import type {
Ordini,
OrdiniOrdineId,
OrdiniUpdate,
} from "~/schemas/public/Ordini";
import type { Users, UsersId } from "~/schemas/public/Users";
import { db } from "~/server/db";
2025-08-04 17:45:44 +02:00
export type ServizioOrdiniTable = Omit<OrdiniTable, "rinnovo_id">;
export type ServizioOrdini = Selectable<ServizioOrdiniTable>;
export type NewServizioOrdini = Insertable<ServizioOrdiniTable>;
export type ServizioOrdiniUpdate = Updateable<ServizioOrdiniTable>;
export type RinnovoOrdiniTable = Omit<OrdiniTable, "servizio_id">;
export type RinnovoOrdini = Selectable<RinnovoOrdiniTable>;
export type NewRinnovoOrdini = Insertable<RinnovoOrdiniTable>;
export type RinnovoOrdiniUpdate = Updateable<RinnovoOrdiniTable>;
export type Ordini_w_Utente = Ordini & Pick<Users, "username">;
export const getOrdini = async (
userId: UsersId | undefined,
): Promise<Ordini_w_Utente[]> => {
2025-08-28 18:27:07 +02:00
try {
let qry = db
.selectFrom("ordini")
.selectAll("ordini")
.innerJoin("users", "users.id", "ordini.userid")
.select("users.username");
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (userId) {
qry = qry.where("ordini.userid", "=", userId);
}
return await qry.orderBy("created_at", "desc").execute();
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error fetching ordini by user ID: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const getOrdineById = async (ordineId: OrdiniOrdineId) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.selectFrom("ordini")
.selectAll("ordini")
.innerJoin("users", "users.id", "ordini.userid")
.select("users.username")
.where("ordini.ordine_id", "=", ordineId)
.orderBy("created_at", "desc")
.executeTakeFirstOrThrow(
() => new Error(`Ordine not found - ordineId: ${ordineId}`),
);
2025-08-28 18:27:07 +02:00
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error fetching ordini by user ID: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const createOrdine = async (
data: NewServizioOrdini | NewRinnovoOrdini,
) => {
try {
return await db
.insertInto("ordini")
.values(data)
.returningAll()
.executeTakeFirstOrThrow(() => new Error("Failed to create ordine"));
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error creating ordine: ${(e as Error).message}`,
});
}
};
2025-08-04 17:45:44 +02:00
export const removeOrder = async (ordineId: OrdiniOrdineId) => {
2025-08-28 18:27:07 +02:00
try {
await db.deleteFrom("ordini").where("ordine_id", "=", ordineId).execute();
2025-08-28 18:27:07 +02:00
return true;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error removing order: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const updateOrder = async ({
2025-08-28 18:27:07 +02:00
ordineId,
data,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
ordineId: OrdiniOrdineId;
data: OrdiniUpdate;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
const updated = await db
.updateTable("ordini")
.set(data)
.where("ordine_id", "=", ordineId)
.returningAll()
.executeTakeFirstOrThrow(
() => new Error(`Failed to update ordine - ordineId: ${ordineId}`),
);
2025-08-28 18:27:07 +02:00
return updated;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error updating order: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};