From c1fc16e826641e48a0c00f6ee959f71d97abd592 Mon Sep 17 00:00:00 2001 From: dmitri-korin-bcps <108112696+dmitri-korin-bcps@users.noreply.github.com> Date: Tue, 24 Dec 2024 11:39:22 -0800 Subject: [PATCH] fix: CE-1330-Attachment-Sort-Order-on-Export (#838) --- backend/src/v1/complaint/complaint.service.ts | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/backend/src/v1/complaint/complaint.service.ts b/backend/src/v1/complaint/complaint.service.ts index fbfaee64d..7039a8b06 100644 --- a/backend/src/v1/complaint/complaint.service.ts +++ b/backend/src/v1/complaint/complaint.service.ts @@ -2073,6 +2073,27 @@ export class ComplaintService { return outcomeData.getCaseFileByLeadId; }; + const _multiFieldCompare = (first: any, second: any, compareInfo: { field: string; sort: string }[]): number => { + for (const item of compareInfo) { + if (item.sort === "asc") { + if (first[item.field] < second[item.field]) { + return -1; + } + if (first[item.field] > second[item.field]) { + return 1; + } + } else if (item.sort === "desc") { + if (first[item.field] > second[item.field]) { + return -1; + } + if (first[item.field] < second[item.field]) { + return 1; + } + } + } + return 0; + }; + try { if (complaintType) { builder = this._generateQueryBuilder(complaintType); @@ -2196,10 +2217,16 @@ export class ComplaintService { .map((item) => { return { name: item.name, - date: _applyTimezone(item.date, tz, "datetime"), + date: item.date, fileType: getFileType(item.name), }; - }); + }) + .sort((first, second) => + _multiFieldCompare(first, second, [ + { field: "fileType", sort: "asc" }, + { field: "date", sort: "asc" }, + ]), + ); data.hasComplaintAttachments = data.cAtts?.length > 0; data.oAtts = attachments @@ -2207,10 +2234,16 @@ export class ComplaintService { .map((item) => { return { name: item.name, - date: _applyTimezone(item.date, tz, "datetime"), + date: item.date, fileType: getFileType(item.name), }; - }); + }) + .sort((first, second) => + _multiFieldCompare(first, second, [ + { field: "fileType", sort: "asc" }, + { field: "date", sort: "asc" }, + ]), + ); data.hasOutcomeAttachments = data.oAtts?.length > 0;