83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import { redirect, type ActionFunctionArgs } from "@remix-run/node";
|
|
import { Button, Container, Form, Row } from "react-bootstrap";
|
|
|
|
import { enhance } from "~/utils/db.server";
|
|
import { createPdf } from "~/utils/pdf/pdf.generate.server";
|
|
import { slugify } from "~/utils/slugs";
|
|
|
|
export const action = async ({
|
|
request,
|
|
}: ActionFunctionArgs) => {
|
|
const { dbe } = await enhance(request);
|
|
|
|
const form = await request.formData();
|
|
|
|
const name = String(form.get("name")) || "";
|
|
const dateStart = String(form.get("dateStart")) + ":00.000z" || Date.now().toString();
|
|
const dateEnd = String(form.get("dateEnd")) + ":00.000z" || Date.now().toString();
|
|
|
|
try{
|
|
|
|
const containersWithHistoryAndDrink = await dbe.container.findMany({
|
|
where: {checkouts: {some:{AND: [{checkoutAt: {gte: dateStart}}, {checkoutAt: {lte: dateEnd}}]}}},
|
|
include: {drink: true, checkouts: {where:{AND: [{checkoutAt: {gte: dateStart}}, {checkoutAt: {lte: dateEnd}}]}}}
|
|
});
|
|
|
|
const report = await dbe.report.create({ data: {
|
|
name: name,
|
|
dateStart: dateStart,
|
|
dateEnd: dateEnd
|
|
}
|
|
});
|
|
|
|
const reportId = String(report.id).padStart(8, "0");
|
|
|
|
const filepath = await createPdf(slugify(name), reportId, dateStart, dateEnd, containersWithHistoryAndDrink);
|
|
|
|
await dbe.report.update({ data: {
|
|
file: filepath
|
|
},
|
|
where: {id: report.id}
|
|
});
|
|
|
|
}
|
|
catch(e){
|
|
console.log(e);
|
|
}
|
|
|
|
return redirect("/admin/reports");
|
|
};
|
|
|
|
export default function NewReportRoute() {
|
|
|
|
const startDate = new Date((Date.now() - (1000*60*60*24*30))).toISOString().slice(0,-8);
|
|
const endDate = new Date(Date.now()).toISOString().slice(0,-8);
|
|
|
|
return (
|
|
<Container>
|
|
<Row>
|
|
<h1>Create Report </h1>
|
|
<Form method="POST">
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Name</Form.Label>
|
|
<Form.Control type="text" name="name" required/>
|
|
</Form.Group>
|
|
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Date range start</Form.Label>
|
|
<Form.Control type="datetime-local" defaultValue={startDate} name="dateStart" required/>
|
|
</Form.Group>
|
|
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Date range end</Form.Label>
|
|
<Form.Control type="datetime-local" defaultValue={endDate} name="dateEnd" required/>
|
|
</Form.Group>
|
|
|
|
<Button variant="primary" type="submit">
|
|
Submit
|
|
</Button>
|
|
</Form>
|
|
</Row>
|
|
</Container>
|
|
);
|
|
} |