@dwp/govuk-casa
Version:
A framework for building GOVUK Collect-And-Submit-Applications
57 lines • 2.67 kB
JavaScript
;
// Gather the field data from `req.body` into the current JourneyContext
// - Store in the current session
// - Update the user's journey context with the new data
// - Remove validation date of JourneyContext so it can re-evaluted
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const JourneyContext_js_1 = __importDefault(require("../lib/JourneyContext.js"));
const constants_js_1 = require("../lib/constants.js");
/**
* @typedef {import("../lib/field").PageField} PageField
* @access private
*/
/**
* Gather the field data from `req.body` into the current JourneyContext
*
* - Store in the current session
* - Update the user's journey context with the new data
* - Remove validation date of JourneyContext so it can re-evaluted
*
* @param {object} obj Options
* @param {string} obj.waypoint Waypoint
* @param {PageField[]} [obj.fields] Fields. Default is `[]`
* @returns {Array} Array of middleware
*/
exports.default = ({ waypoint, fields = [] }) => [
(req, res, next) => {
// Store a copy of the journey context before modifying it. This is useful
// for any comparison work that may be done in subsequent middleware.
req.casa.archivedJourneyContext = JourneyContext_js_1.default.fromContext(req.casa.journeyContext, req);
// Ignore data for any non-persistent fields
// ESLint disabled as `fields`, `i` and `name` are dev-controlled
/* eslint-disable security/detect-object-injection */
const persistentBody = Object.create(null);
for (let i = 0, l = fields.length; i < l; i++) {
if (fields[i].meta.persist &&
fields[i].getValue(req.body) !== undefined) {
persistentBody[fields[i].name] = fields[i].getValue(req.body);
}
}
/* eslint-enable security/detect-object-injection */
// Update data and validation context in the current request, and store.
// The validation state is removed here because we must assume the gathered
// data is invalid until proven otherwise when the validation step is run.
req.casa.journeyContext.setDataForPage(waypoint, persistentBody);
req.casa.journeyContext.removeValidationStateForPage(waypoint);
JourneyContext_js_1.default.putContext(req.session, req.casa.journeyContext, {
userInfo: {
casaRequestPhase: constants_js_1.REQUEST_PHASE_GATHER,
},
});
next();
},
];
//# sourceMappingURL=gather-fields.js.map