-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/services/return-logs-edit/submit-edit-return-log.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
} | ||
|
||
return null | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |