@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
41 lines (40 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDuplicateRequestIds = exports.removeDuplicates = void 0;
const removeDuplicates = async ({ storeService, userId, id, search, }) => {
const reports = await storeService.getAllReportsById(id, userId);
const duplicateIds = (0, exports.getDuplicateRequestIds)(search, reports);
if (duplicateIds.length) {
await Promise.all(duplicateIds.map(async (duplicateId) => {
await await storeService.removeReport(duplicateId, userId);
}));
}
};
exports.removeDuplicates = removeDuplicates;
/**
* Gets the execution IDs of duplicate requests
* - Checks whether the request query are the same
*
* @param {string} newReportSearchParams
* @param {UserReportData[]} existingReports
* @return {string[]} ids of the duplicate requests
*/
const getDuplicateRequestIds = (newReportSearchParams, existingReports) => {
const duplicates = [];
const newQueryParams = new URLSearchParams(newReportSearchParams);
existingReports.forEach((existingReportData) => {
const matches = [];
const existingQueryParams = new URLSearchParams(existingReportData.url.request.search);
if (existingQueryParams.entries.length === newQueryParams.entries.length) {
newQueryParams.forEach((newValue, newKey) => {
const match = existingQueryParams.has(newKey, newValue);
matches.push(match);
});
}
if (matches.every(Boolean)) {
duplicates.push(existingReportData.executionId);
}
});
return duplicates;
};
exports.getDuplicateRequestIds = getDuplicateRequestIds;