From d5e42ec65a7043791ae94065a1b5b0c076d517dc Mon Sep 17 00:00:00 2001 From: afwilcox Date: Fri, 24 Jan 2025 16:23:59 -0800 Subject: [PATCH] feat: change filename on exports to date logged (#901) --- .../complaints/export-complaint-parameters.ts | 1 + backend/src/v1/document/document.controller.ts | 6 ++---- .../complaints/details/complaint-header.tsx | 2 +- .../src/app/store/reducers/documents-thunks.ts | 18 +++++++++++------- .../types/complaints/export-complaint-input.ts | 1 + 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/backend/src/types/models/complaints/export-complaint-parameters.ts b/backend/src/types/models/complaints/export-complaint-parameters.ts index 1317369d6..51695523b 100644 --- a/backend/src/types/models/complaints/export-complaint-parameters.ts +++ b/backend/src/types/models/complaints/export-complaint-parameters.ts @@ -3,6 +3,7 @@ import { COMPLAINT_TYPE } from "./complaint-type"; export interface ExportComplaintParameters { id: string; type: COMPLAINT_TYPE; + fileName: string; tz: string; attachments: any; } diff --git a/backend/src/v1/document/document.controller.ts b/backend/src/v1/document/document.controller.ts index e10d239b1..dd85eb0b5 100644 --- a/backend/src/v1/document/document.controller.ts +++ b/backend/src/v1/document/document.controller.ts @@ -6,7 +6,6 @@ import { ApiTags } from "@nestjs/swagger"; import { Role } from "../../enum/role.enum"; import { Roles } from "../../auth/decorators/roles.decorator"; import { Token } from "../../auth/decorators/token.decorator"; -import { COMPLAINT_TYPE } from "../../types/models/complaints/complaint-type"; import { format } from "date-fns"; import { escape } from "escape-html"; import { ExportComplaintParameters } from "src/types/models/complaints/export-complaint-parameters"; @@ -50,8 +49,7 @@ export class DocumentController { ]; try { - const fileName = `Complaint-${id}-${model.type}-${format(new Date(), "yyyy-MM-dd")}.pdf`; - const response = await this.service.exportComplaint(id, model.type, fileName, model.tz, token, attachments); + const response = await this.service.exportComplaint(id, model.type, model.fileName, model.tz, token, attachments); if (!response || !response.data) { throw Error(`exception: unable to export document for complaint: ${id}`); @@ -61,7 +59,7 @@ export class DocumentController { res.set({ "Content-Type": "application/pdf", - "Content-Disposition": `attachment; filename=${fileName}`, + "Content-Disposition": `attachment; filename=${model.fileName}`, "Content-Length": buffer.length, }); diff --git a/frontend/src/app/components/containers/complaints/details/complaint-header.tsx b/frontend/src/app/components/containers/complaints/details/complaint-header.tsx index 2350d63ed..fb619dd7a 100644 --- a/frontend/src/app/components/containers/complaints/details/complaint-header.tsx +++ b/frontend/src/app/components/containers/complaints/details/complaint-header.tsx @@ -110,7 +110,7 @@ export const ComplaintHeader: FC = ({ }; const exportComplaintToPdf = () => { - dispatch(exportComplaint(complaintType, id)); + dispatch(exportComplaint(complaintType, id, new Date(loggedDate))); }; return ( diff --git a/frontend/src/app/store/reducers/documents-thunks.ts b/frontend/src/app/store/reducers/documents-thunks.ts index 87e5d8d92..062a9a9d7 100644 --- a/frontend/src/app/store/reducers/documents-thunks.ts +++ b/frontend/src/app/store/reducers/documents-thunks.ts @@ -11,16 +11,20 @@ import { ExportComplaintInput } from "@/app/types/complaints/export-complaint-in //-- exports a complaint as a pdf document //-- export const exportComplaint = - (type: string, id: string): ThunkAction, RootState, unknown, Action> => + ( + type: string, + id: string, + dateLogged: Date, + ): ThunkAction, RootState, unknown, Action> => async (dispatch, getState) => { const { attachments } = getState(); try { const agency = getUserAgency(); - let tailored_filename = ""; + let fileName = ""; if (agency != null) { switch (agency) { case AgencyType.CEEB: { - tailored_filename = `${format(new Date(), "yyyy-MM-dd")} Complaint ${id}.pdf`; + fileName = `${format(dateLogged, "yyyy-MM-dd")} Complaint ${id}.pdf`; break; } case AgencyType.COS: @@ -31,13 +35,13 @@ export const exportComplaint = } else if (type === "HWCR") { typeName = "HWC"; } - tailored_filename = `${typeName}_${id}_${format(new Date(), "yyMMdd")}.pdf`; + fileName = `${typeName}_${id}_${format(dateLogged, "yyMMdd")}.pdf`; break; } } } else { // Can't find any agency information - use previous standard - tailored_filename = `Complaint-${id}-${type}-${format(new Date(), "yyyy-MM-dd")}.pdf`; + fileName = `Complaint-${id}-${type}-${format(dateLogged, "yyyy-MM-dd")}.pdf`; } const tz: string = Intl.DateTimeFormat().resolvedOptions().timeZone; @@ -48,7 +52,7 @@ export const exportComplaint = axios.defaults.headers.common["Authorization"] = `Bearer ${localStorage.getItem(AUTH_TOKEN)}`; - const exportComplaintInput = { id, type, tz, attachments } as ExportComplaintInput; + const exportComplaintInput = { id, type, fileName, tz, attachments } as ExportComplaintInput; const url = `${config.API_BASE_URL}/v1/document/export-complaint`; @@ -61,7 +65,7 @@ export const exportComplaint = let link = document.createElement("a"); link.id = "hidden-details-screen-export-complaint"; link.href = fileURL; - link.download = tailored_filename; + link.download = fileName; document.body.appendChild(link); link.click(); diff --git a/frontend/src/app/types/complaints/export-complaint-input.ts b/frontend/src/app/types/complaints/export-complaint-input.ts index b9fa89539..fd35e6ab3 100644 --- a/frontend/src/app/types/complaints/export-complaint-input.ts +++ b/frontend/src/app/types/complaints/export-complaint-input.ts @@ -3,6 +3,7 @@ import { AttachmentsState } from "../state/attachments-state"; export interface ExportComplaintInput { id: string; type: string; + fileName: string; tz: string; attachments: AttachmentsState; }