Skip to content

Commit

Permalink
sort returned audits on account dashboard by desc creation date
Browse files Browse the repository at this point in the history
  • Loading branch information
bellangerq committed Oct 24, 2024
1 parent 12fe8d0 commit 95b7c66
Showing 1 changed file with 68 additions and 64 deletions.
132 changes: 68 additions & 64 deletions confiture-rest-api/src/audits/audit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, CriterionResult[]>
>((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<string, CriterionResult[]>
>((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"]
);
}
}

0 comments on commit 95b7c66

Please sign in to comment.