45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import type { LoaderFunctionArgs } from "@remix-run/node";
|
|
import { redirect } from "@remix-run/node";
|
|
import { enhance } from "~/utils/db.server";
|
|
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const { dbe, session } = await enhance(request);
|
|
|
|
const user = session.get("user");
|
|
if (!user || user.type !== "Admin") {
|
|
return redirect("/");
|
|
}
|
|
|
|
const containerId = Number(params.id);
|
|
|
|
if (!containerId) {
|
|
return redirect("/");
|
|
}
|
|
|
|
const container = await dbe.container.findUnique({
|
|
select: { id: true, inventory: true, drink: true },
|
|
where: { id: containerId },
|
|
});
|
|
|
|
if (!container){
|
|
return redirect("/");
|
|
} else if(container.inventory <= 0) {
|
|
return redirect("/inventory/drink/" + container.drink.id);
|
|
}
|
|
|
|
const newInventory = Math.max(container.inventory - 1, 0);
|
|
await dbe.container.update({
|
|
where: { id: container.id },
|
|
data: { inventory: newInventory },
|
|
});
|
|
|
|
await dbe.history.create({
|
|
data: {
|
|
container: { connect: { id: container.id } },
|
|
inventoryAfter: newInventory,
|
|
},
|
|
});
|
|
|
|
return redirect("/inventory/drink/" + container.drink.id);
|
|
} |