From e9bf38333e663207c6938193b0ae7f4cf3bdb93e Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Tue, 31 Dec 2024 17:34:29 +0000 Subject: [PATCH] Create submit service --- .../return-logs-edit.controller.js | 9 ++- .../submit-edit-return-log.service.js | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 app/services/return-logs-edit/submit-edit-return-log.service.js diff --git a/app/controllers/return-logs-edit.controller.js b/app/controllers/return-logs-edit.controller.js index 6a9020d82c..4087f70f87 100644 --- a/app/controllers/return-logs-edit.controller.js +++ b/app/controllers/return-logs-edit.controller.js @@ -7,6 +7,7 @@ const EditReturnLogService = require('../services/return-logs-edit/edit-return-log.service.js') const InitiateSessionService = require('../services/return-logs-edit/initiate-session.service.js') +const SubmitEditReturnLogService = require('../services/return-logs-edit/submit-edit-return-log.service.js') const basePath = '/system/return-log-edit' @@ -26,19 +27,17 @@ async function setup(request, h) { async function submitEdit(request, h) { const { sessionId } = request.params - const { howToEdit } = request.payload - const pageData = await EditReturnLogService.go(sessionId) + const pageData = await SubmitEditReturnLogService.go(sessionId, request.payload) - if (!howToEdit) { + if (pageData.error) { return h.view('return-logs-edit/how-to-edit.njk', { activeNavBar: 'search', - error: { text: 'Select how would you like to edit this return' }, ...pageData }) } - return h.redirect(`${basePath}/${sessionId}/${howToEdit}`) + return h.redirect(`${basePath}/${sessionId}/${pageData.howToEdit}`) } module.exports = { diff --git a/app/services/return-logs-edit/submit-edit-return-log.service.js b/app/services/return-logs-edit/submit-edit-return-log.service.js new file mode 100644 index 0000000000..2a46208811 --- /dev/null +++ b/app/services/return-logs-edit/submit-edit-return-log.service.js @@ -0,0 +1,57 @@ +'use strict' + +/** + * Handles the user submission for the `/return-log-edit/{sessionId}/how-to-edit` page + * @module SubmitEditReturnLogService + */ + +const SessionModel = require('../../models/session.model.js') +const EditReturnLogPresenter = require('../../presenters/return-logs-edit/edit-return-log.presenter.js') + +/** + * Handles the user submission for the `/return-log-edit/{sessionId}/how-to-edit` page + * + * @param {string} sessionId - The UUID for setup bill run session record + * @param {object} payload - The submitted form data + * + * @returns {Promise} An object with a `howToEdit:` property if there are no errors else the page data for + * the abstraction return page including the validation error details + */ +async function go(sessionId, payload) { + const session = await SessionModel.query().findById(sessionId) + const { howToEdit } = payload + const validationResult = _validate(howToEdit) + + if (!validationResult) { + await _save(session, howToEdit) + + return { howToEdit } + } + + const formattedData = EditReturnLogPresenter.go(session) + + return { + error: validationResult, + ...formattedData + } +} + +async function _save(session, howToEdit) { + const currentData = session + + currentData.howToEdit = howToEdit + + return session.$query().patch({ data: currentData }) +} + +function _validate(howToEdit) { + if (!howToEdit) { + return { text: 'Select how would you like to edit this return' } + } + + return null +} + +module.exports = { + go +}