@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
87 lines (86 loc) • 4.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.populateRequestedReports = exports.populateDefinitions = exports.setRoutePrefix = void 0;
const definitionUtils_1 = __importDefault(require("../utils/definitionUtils"));
const urlHelper_1 = require("../utils/urlHelper");
const getQueryParamAsString = (query, name) => (query[name] ? query[name].toString() : null);
const getDefinitionsPath = (query) => getQueryParamAsString(query, 'dataProductDefinitionsPath');
const deriveDefinitionsPath = (query) => {
const definitionsPath = getDefinitionsPath(query);
if (definitionsPath) {
return definitionsPath;
}
return null;
};
exports.default = (services, config) => {
return async (req, res, next) => {
try {
await (0, exports.populateDefinitions)(services, req, res, config);
await (0, exports.populateRequestedReports)(services, res);
(0, exports.setRoutePrefix)(res, config);
return next();
}
catch (error) {
return next(error);
}
};
};
const setRoutePrefix = async (res, config) => {
res.locals.routePrefix = (0, urlHelper_1.getRoutePrefix)(config);
};
exports.setRoutePrefix = setRoutePrefix;
const populateDefinitions = async (services, req, res, config) => {
// Get the DPD path from the query
const dpdPathFromQuery = deriveDefinitionsPath(req.query);
const dpdPathFromBody = req.body.dataProductDefinitionsPath;
const definitionsPathFromQuery = dpdPathFromQuery || dpdPathFromBody;
if (definitionsPathFromQuery) {
res.locals.dpdPathFromQuery = true;
}
// Get the DPD path from the config
const dpdPathFromConfig = config === null || config === void 0 ? void 0 : config.dataProductDefinitionsPath;
if (dpdPathFromConfig) {
res.locals.dpdPathFromConfig = true;
}
// query takes presedence over config
res.locals.definitionsPath = definitionsPathFromQuery || dpdPathFromConfig;
res.locals.pathSuffix = `?dataProductDefinitionsPath=${res.locals.definitionsPath}`;
if (res.locals.user.token && services.reportingService) {
res.locals.definitions = await services.reportingService.getDefinitions(res.locals.user.token, res.locals.definitionsPath);
}
};
exports.populateDefinitions = populateDefinitions;
const populateRequestedReports = async (services, res) => {
if (res.locals.user) {
const { uuid: userId } = res.locals.user;
const { definitions, definitionsPath } = res.locals;
const requested = await services.requestedReportService.getAllReports(userId);
res.locals.requestedReports = !definitionsPath
? requested
: requested.filter((report) => {
return definitionUtils_1.default.getCurrentVariantDefinition(definitions, report.reportId, report.id);
});
const recent = await services.recentlyViewedService.getAllReports(userId);
res.locals.recentlyViewedReports = !definitionsPath
? recent
: recent.filter((report) => {
return definitionUtils_1.default.getCurrentVariantDefinition(definitions, report.reportId, report.id);
});
if (services.bookmarkService) {
res.locals.bookmarkingEnabled = true;
const bookmarks = await services.bookmarkService.getAllBookmarks(userId);
res.locals.bookmarks = !definitionsPath
? bookmarks
: bookmarks.filter((bookmark) => {
return definitionUtils_1.default.getCurrentVariantDefinition(definitions, bookmark.reportId, bookmark.id);
});
}
if (services.downloadPermissionService) {
res.locals.downloadingEnabled = true;
}
}
};
exports.populateRequestedReports = populateRequestedReports;