|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Orchestrates validating the data for `/return-logs/setup/{sessionId}/period-used` page |
| 5 | + * @module SubmitPeriodUsedService |
| 6 | + */ |
| 7 | + |
| 8 | +const PeriodUsedPresenter = require('../../../presenters/return-logs/setup/period-used.presenter.js') |
| 9 | +const PeriodUsedValidator = require('../../../validators/return-logs/setup/period-used.validator.js') |
| 10 | +const SessionModel = require('../../../models/session.model.js') |
| 11 | + |
| 12 | +/** |
| 13 | + * Orchestrates validating the data for `/return-logs/setup/{sessionId}/period-used` page |
| 14 | + * |
| 15 | + * It first retrieves the session instance for the return log setup session in progress. The session has details about |
| 16 | + * the return log that are needed to validate that the chosen date is valid. |
| 17 | + * |
| 18 | + * The validation result is then combined with the output of the presenter to generate the page data needed by the view. |
| 19 | + * If there was a validation error the controller will re-render the page so needs this information. If all is well the |
| 20 | + * controller will redirect to the next page in the journey. |
| 21 | + * |
| 22 | + * @param {string} sessionId - The UUID of the current session |
| 23 | + * @param {object} payload - The submitted form data |
| 24 | + * |
| 25 | + * @returns {Promise<object>} If no errors the page data for the period-used page else the validation error details |
| 26 | + */ |
| 27 | +async function go(sessionId, payload) { |
| 28 | + const session = await SessionModel.query().findById(sessionId) |
| 29 | + |
| 30 | + const validationResult = _validate(payload, session) |
| 31 | + |
| 32 | + if (!validationResult) { |
| 33 | + await _save(session, payload) |
| 34 | + |
| 35 | + return {} |
| 36 | + } |
| 37 | + |
| 38 | + const formattedData = _submittedSessionData(session, payload) |
| 39 | + |
| 40 | + return { |
| 41 | + activeNavBar: 'search', |
| 42 | + error: validationResult, |
| 43 | + ...formattedData |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function _save(session, payload) { |
| 48 | + session.periodDateUsedOptions = payload.periodDateUsedOptions |
| 49 | + session.fromFullDate = payload.fromFullDate |
| 50 | + session.toFullDate = payload.toFullDate |
| 51 | + session.periodUsedFromDay = payload['period-used-from-day'] |
| 52 | + session.periodUsedFromMonth = payload['period-used-from-month'] |
| 53 | + session.periodUsedFromYear = payload['period-used-from-year'] |
| 54 | + session.periodUsedToDay = payload['period-used-to-day'] |
| 55 | + session.periodUsedToMonth = payload['period-used-to-month'] |
| 56 | + session.periodUsedToYear = payload['period-used-to-year'] |
| 57 | + |
| 58 | + return session.$update() |
| 59 | +} |
| 60 | + |
| 61 | +function _submittedSessionData(session, payload) { |
| 62 | + session.periodDateUsedOptions = payload.periodDateUsedOptions ?? null |
| 63 | + session.periodUsedFromDay = payload['period-used-from-day'] ?? null |
| 64 | + session.periodUsedFromMonth = payload['period-used-from-month'] ?? null |
| 65 | + session.periodUsedFromYear = payload['period-used-from-year'] ?? null |
| 66 | + session.periodUsedToDay = payload['period-used-to-day'] ?? null |
| 67 | + session.periodUsedToMonth = payload['period-used-to-month'] ?? null |
| 68 | + session.periodUsedToYear = payload['period-used-to-year'] ?? null |
| 69 | + |
| 70 | + return PeriodUsedPresenter.go(session) |
| 71 | +} |
| 72 | + |
| 73 | +function _validate(payload, session) { |
| 74 | + const { startDate, endDate } = session |
| 75 | + |
| 76 | + const validation = PeriodUsedValidator.go(payload, startDate, endDate) |
| 77 | + |
| 78 | + if (!validation.error) { |
| 79 | + return null |
| 80 | + } |
| 81 | + |
| 82 | + const result = { |
| 83 | + errorList: [] |
| 84 | + } |
| 85 | + |
| 86 | + validation.error.details.forEach((detail) => { |
| 87 | + let href |
| 88 | + |
| 89 | + if (detail.context.key === 'fromFullDate') { |
| 90 | + href = '#from-full-date' |
| 91 | + } else if (detail.context.key === 'toFullDate') { |
| 92 | + href = '#to-full-date' |
| 93 | + } else { |
| 94 | + href = '#period-date-used-options' |
| 95 | + } |
| 96 | + |
| 97 | + result.errorList.push({ |
| 98 | + href, |
| 99 | + text: detail.message |
| 100 | + }) |
| 101 | + |
| 102 | + result[detail.context.key] = { message: detail.message } |
| 103 | + }) |
| 104 | + |
| 105 | + return result |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { |
| 109 | + go |
| 110 | +} |
0 commit comments