UNPKG

@gov-cy/govcy-express-services

Version:

An Express-based system that dynamically renders services using @gov-cy/govcy-frontend-renderer and posts data to a submission API.

144 lines (129 loc) 6.76 kB
import * as govcyResources from "../resources/govcyResources.mjs"; import * as dataLayer from "../utils/govcyDataLayer.mjs"; import { logger } from "../utils/govcyLogger.mjs"; import { preparePrintFriendlyData, generateReviewSummary } from "../utils/govcySubmitData.mjs"; import { whatsIsMyEnvironment } from '../utils/govcyEnvVariables.mjs'; import { buildMultipleThingsValidationSummary } from "../utils/govcyMultipleThingsValidation.mjs"; /** * Middleware to handle the review page for the service. * This middleware processes the review page, populates form data, and shows validation errors. */ export function govcyReviewPageHandler() { return (req, res, next) => { try { const { siteId } = req.params; // Create a deep copy of the service to avoid modifying the original let serviceCopy = req.serviceData; // Deep copy renderer pageData from let pageData = JSON.parse(JSON.stringify(govcyResources.staticResources.rendererPageData)); // Handle isTesting pageData.site.isTesting = (whatsIsMyEnvironment() === "staging"); // Base page template structure let pageTemplate = { sections: [ // { // name: "beforeMain", // elements: [govcyResources.staticResources.elements.backLink] // } ] }; // Construct page title const pageH1 = { element: "textElement", params: { type: "h1", // if serviceCopy has site.reviewPageHeader use it otherwise use the static resource. it should test if serviceCopy.site.reviewPageHeader[req.globalLang] exists text: ( serviceCopy?.site?.reviewPageHeader?.[req.globalLang] ? serviceCopy.site.reviewPageHeader : govcyResources.staticResources.text.checkYourAnswersTitle ) } }; // Construct submit button const submitButton = { element: "form", params: { action: govcyResources.constructPageUrl(siteId, "review"), method: "POST", elements: [ { element: "button", params: { type: "submit", variant: "success", text: govcyResources.staticResources.text.submit } }, govcyResources.csrfTokenInput(req.csrfToken()) ] } } // Generate the summary list using the utility function let printFriendlyData = preparePrintFriendlyData(req, siteId, serviceCopy); let summaryList = generateReviewSummary(printFriendlyData, req, siteId); let mainElements = []; //--------- Handle Validation Errors --------- // Check if validation errors exist in the session const validationErrors = dataLayer.getSiteSubmissionErrors(req.session, siteId); let summaryItems = []; if (validationErrors && validationErrors.errors) { for (const [pageUrl, err] of Object.entries(validationErrors.errors)) { // Handle multipleThings hub errors if (err.type === "multipleThings") { const mtErrors = err.hub?.errors || {}; // Build summary items for multipleThings errors summaryItems.push(...buildMultipleThingsValidationSummary( serviceCopy, mtErrors, siteId, pageUrl, req, "review", false)); } else { // Normal pages: loop through field errors for (const [fieldKey, fieldErr] of Object.entries(err)) { // Skip type field if (fieldKey === "type") continue; // Push each field error to summary items summaryItems.push({ link: (err.type === "custom" ? // Custom pages `/${fieldErr.pageUrl}?route=review` : govcyResources.constructPageUrl(siteId, fieldErr.pageUrl, "review") ), text: fieldErr.message }); } } } } if (summaryItems.length > 0) { mainElements.unshift(govcyResources.errorSummary(summaryItems)); } // const validationErrors = dataLayer.getSiteSubmissionErrors(req.session, siteId); // let mainElements = []; // if (validationErrors ) { // for (const error in validationErrors.errors) { // validationErrors.errorSummary.push({ // link: govcyResources.constructPageUrl(siteId, validationErrors.errors[error].pageUrl, "review"), //`/${siteId}/${error.pageUrl}`, // text: validationErrors.errors[error].message // }); // } // mainElements.push(govcyResources.errorSummary(validationErrors.errorSummary)); // } //--------- End Handle Validation Errors --------- // Add elements to the main section, the H1, summary list, the submit button and the JS mainElements.push(pageH1, summaryList, submitButton ); // Append generated summary list to the page template pageTemplate.sections.push({ name: "main", elements: mainElements }); //prepare pageData pageData.site = serviceCopy.site; pageData.pageData.title = govcyResources.staticResources.text.checkYourAnswersTitle; // Attach processed page data to the request req.processedPage = { pageData: pageData, pageTemplate: pageTemplate }; logger.debug("Processed review page data:", req.processedPage, req); next(); } catch (error) { return next(error); // Pass error to govcyHttpErrorHandler } }; }