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.

72 lines (71 loc) 3.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const reportStoreService_1 = __importDefault(require("./reportStoreService")); const UserReports_1 = require("../types/UserReports"); const logger_1 = __importDefault(require("../utils/logger")); class RecentlyViewedStoreService extends reportStoreService_1.default { constructor(userDataStore) { super(userDataStore); logger_1.default.info('Service created: RecentlyViewedStoreService'); } async getAllReports(userId) { const userConfig = await this.getState(userId); return userConfig.recentlyViewedReports; } async getAllReportsById(id, userId) { const userConfig = await this.getState(userId); return userConfig.recentlyViewedReports.filter((requested) => { return (requested.id && requested.id === id) || (requested.variantId && requested.variantId === id); }); } async setRecentlyViewed(reportData, userId) { const userConfig = await this.getState(userId); const index = this.findIndexByExecutionId(reportData.executionId, userConfig.recentlyViewedReports); 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); await this.saveExpiredState(userConfig, index, userId); } async asyncSetToExpiredByTableId(id, userId) { const userConfig = await this.getState(userId); const index = this.findIndexByTableId(id, userConfig.recentlyViewedReports); await this.saveExpiredState(userConfig, index, userId); return userConfig.recentlyViewedReports[index].url.request.fullUrl; } async saveExpiredState(userConfig, index, userId) { let report = userConfig.recentlyViewedReports[index]; if (report) { report = { ...report, status: UserReports_1.RequestStatus.EXPIRED, timestamp: { ...report.timestamp, expired: new Date(), }, }; } // eslint-disable-next-line no-param-reassign userConfig.recentlyViewedReports[index] = report; await this.saveState(userId, userConfig); } async removeReport(id, userId) { const userConfig = await this.getState(userId); const index = this.findIndexByExecutionId(id, userConfig.recentlyViewedReports); userConfig.recentlyViewedReports.splice(index, 1); await this.saveState(userId, userConfig); } } exports.default = RecentlyViewedStoreService;