Skip to content

Commit

Permalink
Merge branch 'main' into add-param1label-and-param2label-columns-to-l…
Browse files Browse the repository at this point in the history
…icence-version-purpose-condition-types-view
  • Loading branch information
rvsiyad authored Dec 18, 2024
2 parents dc81ef3 + e48b4ce commit cc98e45
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 435 deletions.
15 changes: 11 additions & 4 deletions app/controllers/notifications-setup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ const InitiateSessionService = require('../services/notifications/setup/initiate

const basePath = 'notifications/setup'

async function viewReturnsPeriod(_request, h) {
const pageData = ReturnsPeriodService.go()
async function viewReturnsPeriod(request, h) {
const {
params: { sessionId }
} = request

const pageData = await ReturnsPeriodService.go(sessionId)

return h.view(`${basePath}/view-returns-period.njk`, {
...pageData
Expand All @@ -26,9 +30,12 @@ async function setup(_request, h) {
}

async function submitReturnsPeriod(request, h) {
const { payload } = request
const {
payload,
params: { sessionId }
} = request

const pageData = await SubmitReturnsPeriodService.go(payload)
const pageData = await SubmitReturnsPeriodService.go(sessionId, payload)

if (pageData.error) {
return h.view(`${basePath}/view-returns-period.njk`, {
Expand Down
28 changes: 17 additions & 11 deletions app/presenters/notifications/setup/returns-period.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,40 @@ const { formatLongDate } = require('../../base.presenter')
/**
* Formats data for the `/notifications/setup/returns-period` page
*
* @param {module:SessionModel} session - The session instance to format
* @param {module:SessionModel} session.returnsPeriod - The returns period saved from a previous submission
*
* @returns {object} - The data formatted for the view template
*/
function go() {
function go(session) {
const savedReturnsPeriod = session.returnsPeriod ?? null

return {
backLink: '/manage',
returnsPeriod: _returnsPeriod()
returnsPeriod: _returnsPeriod(savedReturnsPeriod)
}
}

function _returnsPeriod() {
function _returnsPeriod(savedReturnsPeriod) {
const today = new Date()

const [firstReturnPeriod, secondReturnPeriod] = determineUpcomingReturnPeriods(today)

const currentReturnPeriod = _formatReturnPeriod(firstReturnPeriod)
const nextReturnPeriod = _formatReturnPeriod(secondReturnPeriod)
const currentReturnPeriod = _formatReturnPeriod(firstReturnPeriod, savedReturnsPeriod)
const nextReturnPeriod = _formatReturnPeriod(secondReturnPeriod, savedReturnsPeriod)

return [currentReturnPeriod, nextReturnPeriod]
}

function _formatReturnPeriod(returnPeriod) {
const textPrefix = _textPrefix(returnPeriod)
function _formatReturnPeriod(returnsPeriod, savedReturnsPeriod) {
const textPrefix = _textPrefix(returnsPeriod)
return {
value: returnPeriod.name,
text: `${textPrefix} ${formatLongDate(returnPeriod.startDate)} to ${formatLongDate(returnPeriod.endDate)}`,
value: returnsPeriod.name,
text: `${textPrefix} ${formatLongDate(returnsPeriod.startDate)} to ${formatLongDate(returnsPeriod.endDate)}`,
hint: {
text: `Due date ${formatLongDate(returnPeriod.dueDate)}`
}
text: `Due date ${formatLongDate(returnsPeriod.dueDate)}`
},
checked: returnsPeriod.name === savedReturnsPeriod
}
}

Expand Down
8 changes: 6 additions & 2 deletions app/services/notifications/setup/returns-period.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
*/

const NotificationsPresenter = require('../../../presenters/notifications/setup/returns-period.presenter.js')
const SessionModel = require('../../../models/session.model')

/**
* Formats data for the `/notifications/setup/returns-period` page
*
* @param {string} sessionId - The UUID for setup ad-hoc returns notification session record
*
* @returns {object} The view data for the returns period page
*/
function go() {
const formattedData = NotificationsPresenter.go()
async function go(sessionId) {
const session = await SessionModel.query().findById(sessionId)

const formattedData = NotificationsPresenter.go(session)

return {
activeNavBar: 'manage',
Expand Down
28 changes: 20 additions & 8 deletions app/services/notifications/setup/submit-returns-period.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,44 @@

const NotificationsPresenter = require('../../../presenters/notifications/setup/returns-period.presenter.js')
const ReturnsPeriodValidator = require('../../../validators/notifications/setup/returns-periods.validator.js')
const SessionModel = require('../../../models/session.model')

/**
* Formats data for the `/notifications/setup/returns-period` page
*
* @param {string} sessionId - The UUID of the current session
* @param {object} payload
* @returns {object} The view data for the returns period page, inc error/redirect when applicable
*/
async function go(payload) {
async function go(sessionId, payload) {
const session = await SessionModel.query().findById(sessionId)

const validationResult = _validate(payload)

if (!validationResult) {
if (validationResult) {
const formattedData = NotificationsPresenter.go(session)

return {
redirect: 'send-notice'
activeNavBar: 'manage',
error: validationResult,
pageTitle: 'Select the returns periods for the invitations',
...formattedData
}
}

const formattedData = NotificationsPresenter.go()
await _save(session, payload)

return {
activeNavBar: 'manage',
error: validationResult,
pageTitle: 'Select the returns periods for the invitations',
...formattedData
redirect: 'send-notice'
}
}

async function _save(session, payload) {
session.returnsPeriod = payload.returnsPeriod

return session.$update()
}

function _validate(payload) {
const validation = ReturnsPeriodValidator.go(payload)

Expand Down
Loading

0 comments on commit cc98e45

Please sign in to comment.