UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

109 lines (104 loc) 4.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var reportStoreService = require('../../../../services/reportStoreService.js'); var UserReports = require('../../../../types/UserReports.js'); var logger = require('../../../../utils/logger.js'); class RecentlyViewedStoreService extends reportStoreService.ReportStoreService { constructor(userDataStore) { super(userDataStore); } async getAllReports(userId) { const userConfig = await this.getState(userId); return userConfig.recentlyViewedReports; } async getReportByExecutionId(id, userId) { const userConfig = await this.getState(userId); return userConfig.recentlyViewedReports.find(report => report.executionId === id); } async getReportByTableId(id, userId) { const userConfig = await this.getState(userId); return userConfig.recentlyViewedReports.find(report => report.tableId === id); } async setRecentlyViewed(reportData, userId) { const userConfig = await this.getState(userId); const { executionId } = reportData; const index = executionId ? this.findIndexByExecutionId(executionId, userConfig.recentlyViewedReports) : await this.findIndexByReportId(reportData.id, userConfig.recentlyViewedReports); // Remove the old entry before inserting the new at index 0 if (index > -1) { userConfig.recentlyViewedReports.splice(index, 1); await this.saveState(userId, userConfig); } await this.addReport(reportData, userId, userConfig); } async addReport(reportData, userId, userConfig) { userConfig.recentlyViewedReports.unshift(reportData); await this.saveState(userId, userConfig); } async setToExpired(id, userId) { const userConfig = await this.getState(userId); const index = this.findIndexByExecutionId(id, userConfig.recentlyViewedReports); if (index === -1) { return; } await this.saveExpiredState(userConfig, index, userId); } async setToExpiredByTableId(id, userId) { const userConfig = await this.getState(userId); const index = this.findIndexByTableId(id, userConfig.recentlyViewedReports); if (index !== -1) { await this.saveExpiredState(userConfig, index, userId); } else { logger.default.info(`Unable to expire viewed report - ${id} not found in state`); } } async saveExpiredState(userConfig, index, userId) { const report = userConfig.recentlyViewedReports[index]; if (!report) return; const updatedReport = { ...report, status: UserReports.RequestStatus.EXPIRED, timestamp: { ...report.timestamp, expired: new Date(), }, }; const updatedUserConfig = { ...userConfig, recentlyViewedReports: userConfig.recentlyViewedReports.map((r, i) => (i === index ? updatedReport : r)), }; await this.saveState(userId, updatedUserConfig); } async removeReport(userId, reportId, id, tableId) { const userConfig = await this.getState(userId); const { recentlyViewedReports } = userConfig; let index = -1; if (tableId) { // is an ASYNC report so can find it with the table ID index = this.findIndexByTableId(tableId, recentlyViewedReports); } else if (id && reportId) { // is a SYNC report - so only accessible via an ID and report ID index = this.findIndexByReportAndVariantId(id, reportId, recentlyViewedReports); } if (index >= 0) { userConfig.recentlyViewedReports.splice(index, 1); await this.saveState(userId, userConfig); } } async removeSupersededViewedReports(removedExecutionIds, userId) { Promise.all(removedExecutionIds.map(async (executionId) => { const report = await this.getReportByExecutionId(executionId, userId); if (report) { const { reportId, id, tableId } = report; await this.removeReport(userId, reportId, id, tableId); } })).then(() => undefined); } } exports.RecentlyViewedStoreService = RecentlyViewedStoreService; exports.default = RecentlyViewedStoreService; //# sourceMappingURL=service.js.map