-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-13312 simplified account settings - card types (#4383)
- card types settings controller and view --------- Co-authored-by: Nathaniel Steers <[email protected]>
- Loading branch information
1 parent
c9643af
commit 416c4cb
Showing
11 changed files
with
298 additions
and
104 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
82 changes: 75 additions & 7 deletions
82
app/controllers/simplified-account/settings/card-types/card-types.controller.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 |
---|---|---|
@@ -1,22 +1,90 @@ | ||
const { response } = require('@utils/response') | ||
const { formatCardTypesForTemplate } = require('@utils/simplified-account/format/format-card-types') | ||
const { getAllCardTypes, getAcceptedCardTypesForServiceAndAccountType } = require('@services/card-types.service') | ||
const { getAllCardTypes, getAcceptedCardTypesForServiceAndAccountType, postAcceptedCardsForServiceAndAccountType } = require('@services/card-types.service') | ||
const { formatSimplifiedAccountPathsFor } = require('@utils/simplified-account/format') | ||
const { body, validationResult } = require('express-validator') | ||
const paths = require('@root/paths') | ||
const formatValidationErrors = require('@utils/simplified-account/format/format-validation-errors') | ||
|
||
async function get (req, res, next) { | ||
const serviceId = req.service.externalId | ||
const serviceExternalId = req.service.externalId | ||
const accountType = req.account.type | ||
const isAdminUser = req.user.isAdminUserForService(serviceId) | ||
const isAdminUser = req.user.isAdminUserForService(serviceExternalId) | ||
const messages = res.locals?.flash?.messages ?? [] | ||
try { | ||
const { card_types: allCards } = await getAllCardTypes() | ||
const { card_types: acceptedCards } = await getAcceptedCardTypesForServiceAndAccountType(serviceId, accountType) | ||
const { card_types: acceptedCards } = await getAcceptedCardTypesForServiceAndAccountType(serviceExternalId, accountType) | ||
const currentAcceptedCardTypeIds = acceptedCards.map(card => card.id) | ||
const cardTypes = formatCardTypesForTemplate(allCards, acceptedCards, req.account, isAdminUser) | ||
response(req, res, 'simplified-account/settings/card-types/index', | ||
{ cardTypes, isAdminUser }) | ||
response(req, res, 'simplified-account/settings/card-types/index', { | ||
messages, | ||
cardTypes, | ||
isAdminUser, | ||
currentAcceptedCardTypeIds | ||
}) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
async function post (req, res, next) { | ||
const serviceExternalId = req.service.externalId | ||
const accountType = req.account.type | ||
const currentAcceptedCardTypeIds = req.body.currentAcceptedCardTypeIds?.split(',') || [] | ||
|
||
let selectedCardTypeIds | ||
const sanitiseToArray = value => Array.isArray(value) ? value : (value ? [value] : []) | ||
const validations = [ | ||
body() | ||
.custom((_, { req }) => { | ||
selectedCardTypeIds = sanitiseToArray(req.body.debit).concat(sanitiseToArray(req.body.credit)) | ||
if (selectedCardTypeIds < 1) { | ||
throw new Error('You must choose at least one card') | ||
} | ||
return true | ||
}) | ||
] | ||
await Promise.all(validations.map(validation => validation.run(req))) | ||
const validationErrors = validationResult(req) | ||
if (!validationErrors.isEmpty()) { | ||
const { card_types: allCards } = await getAllCardTypes() | ||
const cardTypes = formatCardTypesForTemplate(allCards, [], req.account, true) | ||
const formattedValidationErrors = formatValidationErrors(validationErrors) | ||
return response(req, res, 'simplified-account/settings/card-types/index', { | ||
errors: { | ||
summary: formattedValidationErrors.errorSummary | ||
}, | ||
cardTypes, | ||
isAdminUser: true, | ||
currentAcceptedCardTypeIds | ||
}) | ||
} | ||
|
||
const noChangesToAcceptedCardTypes = ( | ||
currentAcceptedCardTypeIds.length === selectedCardTypeIds.length && | ||
currentAcceptedCardTypeIds.every(item => selectedCardTypeIds.includes(item)) | ||
) | ||
if (noChangesToAcceptedCardTypes) { | ||
return res.redirect( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceExternalId, accountType) | ||
) | ||
} | ||
|
||
try { | ||
const payload = { | ||
card_types: selectedCardTypeIds | ||
} | ||
await postAcceptedCardsForServiceAndAccountType(serviceExternalId, accountType, payload) | ||
req.flash('messages', { state: 'success', icon: '✓', heading: 'Accepted card types have been updated' }) | ||
return res.redirect( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceExternalId, accountType) | ||
) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
module.exports = { | ||
get | ||
get, | ||
post | ||
} |
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
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
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
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
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
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
Oops, something went wrong.