UNPKG

@ministryofjustice/probation-search-frontend

Version:

A shared UI component to search for probation cases from within your Express/Nunjucks application

118 lines 4.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const date_fns_1 = require("date-fns"); const url_1 = require("../utils/url"); const suggestions_1 = __importDefault(require("../utils/suggestions")); const pagination_1 = __importDefault(require("../utils/pagination")); const probationSearchClient_1 = __importDefault(require("../data/probationSearchClient")); const localData_1 = __importDefault(require("../data/localData")); const middleware_1 = __importDefault(require("../utils/middleware")); const highlighting_1 = __importDefault(require("../utils/highlighting")); class CaseSearchService { constructor(options) { const defaults = { resultPath: (crn) => `/case/${crn}`, extraColumns: [], allowEmptyQuery: false, maxQueryLength: 100, pageSize: 10, localData: localData_1.default, }; this.options = Object.assign(defaults, options); this.client = new probationSearchClient_1.default(this.options.hmppsAuthClient, this.options.environment === 'local' ? this.options.localData : this.options.environment); } post = (req, res) => { // Add the query to the session if (!req.session.probationSearch) req.session.probationSearch = {}; req.session.probationSearch.query = req.body['probation-search-input']; res.redirect((0, url_1.removeParameters)(req, 'page')); }; get = (0, middleware_1.default)(async (req, res, next) => { const { resultPath, extraColumns, allowEmptyQuery, maxQueryLength, pageSize } = this.options; // Render an empty search screen if no session if (!('probationSearch' in req.session) || !req.session.probationSearch) { return this.noSearch(res, next); } // Validate query string from session const { query, matchAllTerms, providers } = req.session.probationSearch; if (!query) { return allowEmptyQuery ? this.noSearch(res, next) : this.errorMessage(req, res, next, 'Please enter a search term'); } if (maxQueryLength && query.length > maxQueryLength) { return this.errorMessage(req, res, next, `Query must be ${maxQueryLength} characters or less.`, query); } // Load search results const request = { query, matchAllTerms: (matchAllTerms ?? 'true') === 'true', providersFilter: providers ?? [], asUsername: res.locals.user.username, pageNumber: req.query.page ? Number.parseInt(req.query.page, 10) : 1, pageSize, }; const response = await this.client.search(request); // Parse and render results res.locals.searchResults = { query, response, extraColumns, suggestions: (0, suggestions_1.default)(response, req), pagination: (0, pagination_1.default)(request.pageNumber, response.totalPages, response.totalElements, page => (0, url_1.addParameters)(req, { page: page.toString() }), pageSize), fn: { resultPath, formatDate: (date) => (date ? (0, date_fns_1.format)((0, date_fns_1.parseISO)(date), 'dd/MM/yyyy') : ''), highlight: (0, highlighting_1.default)(query), }, ...this.securityContext(res), }; res.locals.searchRequest = request; res.locals.searchResponse = response; return next(); }); /** * Render an empty search screen * @param res * @param next * @private */ noSearch(res, next) { res.locals.searchResults = this.securityContext(res); return next(); } /** * Render an error message * @param req * @param res * @param next * @param text * @param query * @param errorMessage * @private */ errorMessage(req, res, next, text, query = '', errorMessage = { text }) { delete req.session.probationSearch; res.locals.searchResults = { errorMessage, query, ...this.securityContext(res) }; return next(); } /** * These parameters are required in the Nunjucks component, we ensure they are * passed through from the Express router via the results param. * @param res * @private */ securityContext(res) { return { csrfToken: res.locals.csrfToken, cspNonce: res.locals.cspNonce, user: res.locals.user, }; } } exports.default = CaseSearchService; //# sourceMappingURL=caseSearchService.js.map