From 95b7c669172b3f19293c018081a4ede257a7b7ef Mon Sep 17 00:00:00 2001 From: Quentin Bellanger Date: Thu, 24 Oct 2024 11:15:12 +0200 Subject: [PATCH] sort returned audits on account dashboard by desc creation date --- .../src/audits/audit.service.ts | 132 +++++++++--------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/confiture-rest-api/src/audits/audit.service.ts b/confiture-rest-api/src/audits/audit.service.ts index 861b73bf8..0d1901fc0 100644 --- a/confiture-rest-api/src/audits/audit.service.ts +++ b/confiture-rest-api/src/audits/audit.service.ts @@ -9,7 +9,7 @@ import { } from "@prisma/client"; import { nanoid } from "nanoid"; import sharp from "sharp"; -import { omit, pick, setWith, uniqBy } from "lodash"; +import { omit, orderBy, pick, setWith, uniqBy } from "lodash"; import { PrismaService } from "../prisma.service"; import * as RGAA from "../rgaa.json"; @@ -1363,76 +1363,80 @@ export class AuditService { } }); - return audits.map((a) => { - const results = [ - ...a.transverseElementsPage.results, - ...a.pages.flatMap((p) => p.results) - ]; - - const progress = - results.filter((r) => r.status !== CriterionResultStatus.NOT_TESTED) - .length / - (CRITERIA_BY_AUDIT_TYPE[a.auditType].length * a.pages.length); - - let complianceLevel = null; - - if (progress >= 1) { - const resultsGroupedById = results.reduce< - Record - >((acc, c) => { - const key = `${c.topic}.${c.criterium}`; - if (acc[key]) { - acc[key].push(c); - } else { - acc[key] = [c]; - } - return acc; - }, {}); - - const results2 = CRITERIA_BY_AUDIT_TYPE[a.auditType].map( - (c) => resultsGroupedById[`${c.topic}.${c.criterium}`] ?? null - ); + return orderBy( + audits.map((a) => { + const results = [ + ...a.transverseElementsPage.results, + ...a.pages.flatMap((p) => p.results) + ]; - const applicableCriteria = results2.filter((criteria) => - criteria.some((c) => isCompliant(c) || isNotCompliant(c)) - ); + const progress = + results.filter((r) => r.status !== CriterionResultStatus.NOT_TESTED) + .length / + (CRITERIA_BY_AUDIT_TYPE[a.auditType].length * a.pages.length); + + let complianceLevel = null; + + if (progress >= 1) { + const resultsGroupedById = results.reduce< + Record + >((acc, c) => { + const key = `${c.topic}.${c.criterium}`; + if (acc[key]) { + acc[key].push(c); + } else { + acc[key] = [c]; + } + return acc; + }, {}); - const compliantCriteria = applicableCriteria.filter((criteria) => { - // remove untested transverse criterion - const withoutUntestedTrans = criteria.filter( - (c) => - !(isTransverse(c, a.transverseElementsPageId) && isNotTested(c)) + const results2 = CRITERIA_BY_AUDIT_TYPE[a.auditType].map( + (c) => resultsGroupedById[`${c.topic}.${c.criterium}`] ?? null ); - return ( - withoutUntestedTrans.some((c) => isCompliant(c)) && - withoutUntestedTrans.every( - (c) => isCompliant(c) || isNotApplicable(c) - ) + const applicableCriteria = results2.filter((criteria) => + criteria.some((c) => isCompliant(c) || isNotCompliant(c)) ); - }); - complianceLevel = Math.round( - (compliantCriteria.length / applicableCriteria.length) * 100 - ); - } + const compliantCriteria = applicableCriteria.filter((criteria) => { + // remove untested transverse criterion + const withoutUntestedTrans = criteria.filter( + (c) => + !(isTransverse(c, a.transverseElementsPageId) && isNotTested(c)) + ); + + return ( + withoutUntestedTrans.some((c) => isCompliant(c)) && + withoutUntestedTrans.every( + (c) => isCompliant(c) || isNotApplicable(c) + ) + ); + }); + + complianceLevel = Math.round( + (compliantCriteria.length / applicableCriteria.length) * 100 + ); + } - const statementIsPublished = !!a.initiator; + const statementIsPublished = !!a.initiator; - return { - ...pick( - a, - "procedureName", - "editUniqueId", - "consultUniqueId", - "creationDate", - "auditType" - ), - complianceLevel, - status: progress < 1 ? "IN_PROGRESS" : "COMPLETED", - estimatedCsvSize: 502 + a.pages.length * 318, - statementIsPublished - }; - }); + return { + ...pick( + a, + "procedureName", + "editUniqueId", + "consultUniqueId", + "creationDate", + "auditType" + ), + complianceLevel, + status: progress < 1 ? "IN_PROGRESS" : "COMPLETED", + estimatedCsvSize: 502 + a.pages.length * 318, + statementIsPublished + }; + }), + (a) => a.creationDate, + ["desc"] + ); } }