infoalloggi-monorepo/apps/infoalloggi/src/server/controllers/ordini.controller.ts

146 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { TRPCError } from "@trpc/server";
2025-08-28 18:27:07 +02:00
import { jsonArrayFrom } from "kysely/helpers/postgres";
import type {
NewOrdini,
OrdiniOrdineId,
OrdiniUpdate,
} from "~/schemas/public/Ordini";
2025-08-28 18:27:07 +02:00
import type { UsersId } from "~/schemas/public/Users";
import { db, type Querier } from "~/server/db";
2025-08-04 17:45:44 +02:00
export type OrdiniWPagamenti = Awaited<ReturnType<typeof getOrdini>>[number];
export const getOrdini = async (userId: UsersId | undefined) => {
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")
.select((eb) => [
jsonArrayFrom(
eb
.selectFrom("payments")
.selectAll("payments")
.whereRef("payments.ordine_id", "=", "ordini.ordine_id")
.orderBy("payments.created_at", "desc"),
).as("pagamenti"),
]);
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")
.select((eb) => [
jsonArrayFrom(
eb
.selectFrom("payments")
.selectAll("payments")
.whereRef("payments.ordine_id", "=", "ordini.ordine_id")
.orderBy("payments.created_at", "desc"),
).as("pagamenti"),
])
.where("ordini.ordine_id", "=", ordineId)
.orderBy("created_at", "desc")
.executeTakeFirstOrThrow();
} 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: NewOrdini) => {
try {
return await db
.insertInto("ordini")
.values(data)
.returningAll()
.executeTakeFirstOrThrow();
} 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)
.executeTakeFirstOrThrow();
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();
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
};
export const cleanupUnusedOrders = async ({ db }: { db: Querier }) => {
2025-08-28 18:27:07 +02:00
try {
const orders = await db
.selectFrom("ordini")
.select("ordini.ordine_id")
.leftJoin("payments", "payments.ordine_id", "ordini.ordine_id")
.where("payments.paymentstatus", "is", null)
.where("ordini.isActive", "=", false)
.distinct()
.execute();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (orders.length === 0) {
return "ok";
}
const ordineIds = orders.map((o) => o.ordine_id);
await db.deleteFrom("ordini").where("ordine_id", "in", ordineIds).execute();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return "ok";
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error cleaning up old orders: ${(e as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};