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.

128 lines (127 loc) 6.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initUserDataStore = void 0; const logger_1 = __importDefault(require("./logger")); const EmbeddedReportUtils_1 = require("../types/EmbeddedReportUtils"); // DATA STORE const reportDataStore_1 = __importDefault(require("../data/reportDataStore")); // FEATURE: Download const download_1 = __importDefault(require("../routes/download")); const downloadPermissionService_1 = __importDefault(require("../services/downloadPermissionService")); const reportingClient_1 = __importDefault(require("../data/reportingClient")); const reportingService_1 = __importDefault(require("../services/reportingService")); const initUserDataStore = (features, services) => { logger_1.default.info('Embedded Reports: Setting up user data store.'); const { config: featuresConfig, list } = features; const { redisClient, userDataStore } = featuresConfig; let initialisedUserDataStore; if (redisClient) { logger_1.default.info('Embedded Reports: Redis Client found. Initialising User Data store.'); return new reportDataStore_1.default(redisClient); } if (userDataStore) { logger_1.default.info('Embedded Reports: User data store found. Using the provided user data store'); return userDataStore; } logger_1.default.info('No user data store config provided. Assuming pre-initialisation of feature services'); const errorMessages = []; if (!services.downloadPermissionService && list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.download)) { errorMessages.push('Missing downloadPermissionService in services config: Cant enable download feature'); } if (!services.bookmarkService && list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.bookmark)) { errorMessages.push('Missing bookmarkService in services config: Cant enable bookmark feature'); } if (!services.recentlyViewedService && list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.recentlyViewed)) { errorMessages.push('Missing recentlyViewedService in services config: Cant enable recently feature'); } if (errorMessages.length) { throw new Error(errorMessages.join(', ')); } return initialisedUserDataStore; }; exports.initUserDataStore = initUserDataStore; const initFeatures = ({ router, config, services, features }) => { let updatedServices = { ...services, }; const initialisedFeatures = {}; if (features !== undefined) { logger_1.default.info(`Embedded Reports: Features config found. Initialising features: ${features.list}`); const { list } = features; const { layoutPath } = config; const downloadFeatureEnabled = list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.download); const bookmarkFeatureEnabled = list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.bookmark); const recentlyViewedFeatureEnabled = list.includes(EmbeddedReportUtils_1.EmbeddedReportFeaturesList.recentlyViewed); const initialisedUserDataStore = (0, exports.initUserDataStore)(features, services); if (downloadFeatureEnabled) { logger_1.default.info(`Download Feature: Setup`); try { if (!services.downloadPermissionService && initialisedUserDataStore) { logger_1.default.info('Download Feature: DownloadPermissionService not provided. Initialising new service'); const downloadPermissionService = new downloadPermissionService_1.default(initialisedUserDataStore); updatedServices = { ...services, downloadPermissionService, }; logger_1.default.info('Download Feature: Service initialised and added to services config'); } else { logger_1.default.info('Download Feature: DownloadPermissionService found'); } initialisedFeatures.download = true; (0, download_1.default)({ router, services: updatedServices, layoutPath, prefix: '' }); } catch (error) { logger_1.default.info('Download Feature: Unable to init feature.'); throw error; } } if (recentlyViewedFeatureEnabled) { logger_1.default.info('Recently Viewed Feature: Not Available'); } if (bookmarkFeatureEnabled) { logger_1.default.info('Bookmark Feature: Not Available'); } } return { router, services: updatedServices, initialisedFeatures, }; }; const initReportingService = (config, services) => { let updatedServices = { ...services, }; if (config === null || config === void 0 ? void 0 : config.reportingClientArgs) { logger_1.default.info('Embedded Reports: Reporting config found'); if (services.reportingService) { logger_1.default.info('Embedded Reports: Reporting Service Found. Using service provided'); } else { logger_1.default.info('Embedded Reports: Initialising Reporting Client and Service'); const reportingClient = new reportingClient_1.default(config.reportingClientArgs); const reportingService = new reportingService_1.default(reportingClient); updatedServices = { ...services, reportingService, }; } } else if (services.reportingService) { logger_1.default.info('Embedded Reports: Reporting Service Found. Using service provided'); } else { logger_1.default.error('Embedded Reports: No Reporting service or Config Found. Please provide an initialiesed report service, or the correct config'); } return { services: updatedServices, }; }; exports.default = { initFeatures, initReportingService, };