Skip to content

Commit

Permalink
Create submit service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozzey committed Dec 31, 2024
1 parent 781a196 commit e9bf383
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/controllers/return-logs-edit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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 = {
Expand Down
57 changes: 57 additions & 0 deletions app/services/return-logs-edit/submit-edit-return-log.service.js
Original file line number Diff line number Diff line change
@@ -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<object>} 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' }
}

Check failure on line 50 in app/services/return-logs-edit/submit-edit-return-log.service.js

View workflow job for this annotation

GitHub Actions / build

Delete `··`

return null
}

module.exports = {
go
}

0 comments on commit e9bf383

Please sign in to comment.