Skip to content

Commit

Permalink
reports scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed Sep 27, 2024
1 parent 9e3fe37 commit 1926be2
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 6 deletions.
107 changes: 107 additions & 0 deletions app/client/src/features/reports/ReportDairyTrailerInspection.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React, { useEffect } from "react";
import { useForm } from "react-hook-form";
import { useSelector, useDispatch } from "react-redux";
import { Row, Col, Form, Button } from "react-bootstrap";

import { startOfToday, add } from "date-fns";

import CustomDatePicker from "../../components/CustomDatePicker";
import DocGenDownloadBar from "../../components/DocGenDownloadBar";

import {
startDairyTrailerInspectionJob,
generateReport,
fetchReportJob,
selectReportsJob,
completeReportJob,
} from "./reportsSlice";

import { REPORTS } from "../../utilities/constants";

export default function ReportDairyTrailerInspection() {
const dispatch = useDispatch();

const job = useSelector(selectReportsJob);
const { pendingDocuments } = job;

const form = useForm({
reValidateMode: "onBlur",
});
const { setValue, watch } = form;

const startDate = startOfToday();
const endDate = add(startOfToday(), { days: 15 });
const watchStartDate = watch("startDate", startDate);
const watchEndDate = watch("endDate", endDate);

useEffect(() => {
setValue("startDate", startDate);
setValue("endDate", endDate);
}, [dispatch]);

useEffect(() => {
if (job.id && job.type === REPORTS.DAIRY_TRAILER_INSPECTION) {
dispatch(fetchReportJob());

if (pendingDocuments?.length > 0) {
dispatch(generateReport(pendingDocuments[0].documentId));
} else {
dispatch(completeReportJob(job.id));
}
}
}, [pendingDocuments]); // eslint-disable-line react-hooks/exhaustive-deps

const handleFieldChange = (field) => {
return (value) => {
setValue(field, value);
};
};

const onGenerateReport = () => {
dispatch(
startDairyTrailerInspectionJob({
startDate: watchStartDate,
endDate: watchEndDate,
})
);
};

return (
<>
<Row>
<Col lg={3}>
<CustomDatePicker
id="startDate"
label="Start Date"
notifyOnChange={handleFieldChange("startDate")}
defaultValue={startDate}
/>
</Col>
<Col lg={3}>
<CustomDatePicker
id="endDate"
label="End Date"
notifyOnChange={handleFieldChange("endDate")}
defaultValue={endDate}
/>
</Col>
<Col sm={2}>
<Form.Label>&nbsp;</Form.Label>
<Button
variant="primary"
type="button"
onClick={() => onGenerateReport()}
block
>
Generate Report
</Button>
</Col>
</Row>
<div className="mt-3">
<DocGenDownloadBar job={job} />
</div>
</>
);
}

ReportDairyTrailerInspection.propTypes = {};
16 changes: 16 additions & 0 deletions app/client/src/features/reports/Reports.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ReportLicenceExpiry from "./ReportLicenceExpiry";

import { clearReportsJob } from "./reportsSlice";
import RenderOnRole from "../../components/RenderOnRole";
import ReportDairyTrailerInspection from "./ReportDairyTrailerInspection";

export default function Reports() {
const dispatch = useDispatch();
Expand Down Expand Up @@ -75,6 +76,8 @@ export default function Reports() {
case REPORTS.LICENCE_EXPIRY:
control = <ReportLicenceExpiry />;
break;
case REPORTS.DAIRY_TRAILER_INSPECTION:
control = <ReportDairyTrailerInspection />;
default:
break;
}
Expand Down Expand Up @@ -177,6 +180,19 @@ export default function Reports() {
</option>
</RenderOnRole>

<RenderOnRole
roles={[
SYSTEM_ROLES.READ_ONLY,
SYSTEM_ROLES.USER,
SYSTEM_ROLES.INSPECTOR,
SYSTEM_ROLES.SYSTEM_ADMIN,
]}
>
<option value={REPORTS.DAIRY_TRAILER_INSPECTION}>
Dairy Trailer Inspection
</option>
</RenderOnRole>

<RenderOnRole
roles={[
SYSTEM_ROLES.READ_ONLY,
Expand Down
18 changes: 18 additions & 0 deletions app/client/src/features/reports/reportsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ export const startApiaryHiveInspectionJob = createAsyncThunk(
}
);

export const startDairyTrailerInspectionJob = createAsyncThunk(
"reports/startDairyTrailerInspection",
async (payload, thunkApi) => {
try {
const response = await Api.post(
`documents/reports/startJob/dairyTrailerInspection`,
payload
);
return response.data;
} catch (error) {
if (error instanceof ApiError) {
return thunkApi.rejectWithValue(error.serialize());
}
return thunkApi.rejectWithValue({ code: -1, description: error.message });
}
}
);

export const startProducersAnalysisRegionJob = createAsyncThunk(
"reports/startProducersAnalysisRegionJob",
async (_, thunkApi) => {
Expand Down
1 change: 1 addition & 0 deletions app/client/src/utilities/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const REPORTS = {
DAIRY_TEST_THRESHOLD: "DAIRY_TEST_THRESHOLD",
LICENCE_LOCATION: "LICENCE_LOCATION",
LICENCE_EXPIRY: "LICENCE_EXPIRY",
DAIRY_TRAILER_INSPECTION: "DAIRY_TRAILER_INSPECTION",
};

export const SYSTEM_ROLES = {
Expand Down
53 changes: 47 additions & 6 deletions app/server/routes/v1/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ const certificateTemplateDir = path.join(
__dirname,
"../../static/templates/certificates"
);
const renewalsTemplateDir = path.join(__dirname, "../../static/templates/notices");
const renewalsTemplateDir = path.join(
__dirname,
"../../static/templates/notices"
);
const dairyNoticeTemplateDir = path.join(
__dirname,
"../../static/templates/notices/dairy"
);
const reportsTemplateDir = path.join(__dirname, "../../static/templates/reports");
const reportsTemplateDir = path.join(
__dirname,
"../../static/templates/reports"
);

// As templates are converted to base 64 for the first time they will be pushed to this for reuse
const templateBuffers = [];
Expand All @@ -61,7 +67,6 @@ cdogs.interceptors.request.use(
)
);


async function getPendingDocuments(jobId) {
const documents = await prisma.mal_print_job_output.findMany({
where: { document_binary: null, print_job_id: jobId },
Expand Down Expand Up @@ -253,11 +258,14 @@ async function startCertificateJob(licenceIds) {

async function generateCertificate(documentId) {
const document = await getDocument(documentId);
console.log("generateCertificate");
console.log(document);

const templateFileName = getCertificateTemplateName(
document.document_type,
document.licence_type
);
console.log("templateFileName: " + templateFileName);

if (templateFileName === undefined) {
return {
Expand Down Expand Up @@ -363,7 +371,7 @@ async function startRenewalJob(licenceIds) {
},
};

const [, , procedureResult,] = await prisma.$transaction([
const [, , procedureResult] = await prisma.$transaction([
// ensure selected licences have print_renewal set to true
prisma.mal_licence.updateMany({
where: licenceFilterCriteria,
Expand Down Expand Up @@ -463,7 +471,6 @@ async function generateRenewal(documentId) {
}

async function getQueuedDairyNotices(startDate, endDate) {

const andArray = [];
andArray.push({ recorded_date: { gte: new Date(startDate) } });
andArray.push({ recorded_date: { lte: new Date(endDate) } });
Expand Down Expand Up @@ -586,7 +593,6 @@ async function generateDairyNotice(documentId) {
return result;
}


async function getQueuedDairyTankNotices() {
return prisma.mal_print_dairy_farm_tank_recheck_vw.findMany({
where: {
Expand Down Expand Up @@ -730,6 +736,19 @@ async function startApiaryHiveInspectionJob(startDate, endDate) {
return { jobId, documents };
}

async function startDairyTrailerInspectionJob(startDate, endDate) {
// waiting for implemenation of the stored procedure
const [procedureResult] = await prisma.$transaction([
prisma.$queryRawUnsafe(
`CALL mals_app.pr_generate_print_json_dairy_trailer_inspection('${startDate}', '${endDate}', NULL)`
),
]);

const jobId = procedureResult[0].iop_print_job_id;
const documents = await getPendingDocuments(jobId);
return { jobId, documents };
}

async function startProducersAnalysisRegionJob() {
const [procedureResult] = await prisma.$transaction([
prisma.$queryRawUnsafe(
Expand Down Expand Up @@ -862,6 +881,8 @@ async function startLicenceExpiryJob(startDate, endDate) {

async function generateReport(documentId) {
const document = await getDocument(documentId);
console.log("generateReport");
console.log(document);

const templateFileName = getReportsTemplateName(document.document_type);

Expand Down Expand Up @@ -1208,6 +1229,26 @@ router.post(
}
);

router.post(
"/reports/startJob/dairyTrailerInspection",
async (req, res, next) => {
const startDate = formatDate(new Date(req.body.startDate));
const endDate = formatDate(new Date(req.body.endDate));
console.log("startJob dairyTrailerInspection");

await startDairyTrailerInspectionJob(startDate, endDate)
.then(({ jobId, documents }) => {
return res.send({
jobId,
documents,
type: REPORTS.DAIRY_TRAILER_INSPECTION,
});
})
.catch(next)
.finally(async () => prisma.$disconnect());
}
);

router.post(
"/reports/startJob/producersAnalysisRegion",
async (req, res, next) => {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions app/server/utilities/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module.exports = Object.freeze({
DAIRY_FARM_QUALITY: "DAIRY_FARM_QUALITY",
DAIRY_FARM_TANK: "DAIRY_FARM_TANK",
DAIRY_TEST_THRESHOLD: "DAIRY_TEST_THRESHOLD",
DAIRY_TRAILER_INSPECTION: "DAIRY_TRAILER_INSPECTION",
LICENCE_LOCATION: "LICENCE_LOCATION",
LICENCE_EXPIRY: "LICENCE_EXPIRY",
},
Expand Down
6 changes: 6 additions & 0 deletions app/server/utilities/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function getCertificateTemplateName(documentType, licenceType) {
return "Veterinary-Drug-Outlet";
case "DISPENSER":
return "Veterinary-Drug-Dispenser";
case "DAIRY TANK TRUCK":
return "Dairy-Tank-Truck";
default:
return undefined;
}
Expand Down Expand Up @@ -110,6 +112,8 @@ function getRenewalTemplateName(documentType, licenceType) {
return "Renewal_VetDrugLicence_Template";
case "DISPENSER":
return "Renewal_VetDrugDispenser_Template";
case "DAIRY TANK TRUCK":
return "Renewal_DairyTankTruck_Template"; // not implemented
default:
return undefined;
}
Expand Down Expand Up @@ -192,6 +196,8 @@ function getReportsTemplateName(documentType) {
return "LicenceType_Location_Template";
case constants.REPORTS.LICENCE_EXPIRY:
return "Licence_Expiry_Species_NoSpecies_Template";
case constants.REPORTS.DAIRY_TRAILER_INSPECTION:
return "Dairy_Trailer_Inspection_Template";
default:
return null;
}
Expand Down

0 comments on commit 1926be2

Please sign in to comment.