Skip to content

Commit

Permalink
feat: change filename on exports to date logged (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
afwilcox authored Jan 25, 2025
1 parent 3653dc1 commit d5e42ec
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { COMPLAINT_TYPE } from "./complaint-type";
export interface ExportComplaintParameters {
id: string;
type: COMPLAINT_TYPE;
fileName: string;
tz: string;
attachments: any;
}
6 changes: 2 additions & 4 deletions backend/src/v1/document/document.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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}`);
Expand All @@ -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,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const ComplaintHeader: FC<ComplaintHeaderProps> = ({
};

const exportComplaintToPdf = () => {
dispatch(exportComplaint(complaintType, id));
dispatch(exportComplaint(complaintType, id, new Date(loggedDate)));
};

return (
Expand Down
18 changes: 11 additions & 7 deletions frontend/src/app/store/reducers/documents-thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<string | undefined>, RootState, unknown, Action<string>> =>
(
type: string,
id: string,
dateLogged: Date,
): ThunkAction<Promise<string | undefined>, RootState, unknown, Action<string>> =>
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:
Expand All @@ -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;
Expand All @@ -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`;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AttachmentsState } from "../state/attachments-state";
export interface ExportComplaintInput {
id: string;
type: string;
fileName: string;
tz: string;
attachments: AttachmentsState;
}

0 comments on commit d5e42ec

Please sign in to comment.