Skip to content

Commit

Permalink
PP-13312 Response to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
james-peacock-gds committed Dec 13, 2024
1 parent e027609 commit 2349ec2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const { formatSimplifiedAccountPathsFor } = require('@utils/simplified-account/f
const paths = require('@root/paths')

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 ?? []
let noCardTypesSelectedError
if (res.locals?.flash?.noCardTypesSelectedError) {
noCardTypesSelectedError = { summary: [{ text: res.locals?.flash?.noCardTypesSelectedError }] }
}
try {
const { card_types: allCards } = await getAllCardTypes()
const { card_types: acceptedCards } = await getAcceptedCardTypesForServiceAndAccountType(serviceId, accountType)
const { card_types: acceptedCards } = await getAcceptedCardTypesForServiceAndAccountType(serviceExternalId, accountType)
const cardTypesSelected = noCardTypesSelectedError ? [] : acceptedCards
const currentAcceptedCardTypeIds = acceptedCards.map(card => card.id)
const cardTypes = formatCardTypesForTemplate(allCards, cardTypesSelected, req.account, isAdminUser)
Expand All @@ -32,16 +32,15 @@ async function get (req, res, next) {
}

async function post (req, res, next) {
console.log(req.body)
const serviceId = req.service.externalId
const serviceExternalId = req.service.externalId
const accountType = req.account.type
const selectedDebitCards = (typeof req.body.debit === 'string' ? [req.body.debit] : req.body.debit) || []
const selectedCreditCards = (typeof req.body.credit === 'string' ? [req.body.credit] : req.body.credit) || []
const selectedCardTypeIds = [...selectedDebitCards, ...selectedCreditCards]
const currentAcceptedCardTypeIds = req.body.currentAcceptedCardTypeIds ? req.body.currentAcceptedCardTypeIds.split(',') : []
if (!selectedDebitCards.length && !selectedCreditCards.length) {
req.flash('noCardTypesSelectedError', 'You must choose at least one card')
return res.redirect(formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceId, accountType))
return res.redirect(formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceExternalId, accountType))
}

const noChangesToAcceptedCardTypes = (
Expand All @@ -50,18 +49,18 @@ async function post (req, res, next) {
)
if (noChangesToAcceptedCardTypes) {
return res.redirect(
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceId, accountType)
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceExternalId, accountType)
)
}

try {
const payload = {
card_types: selectedCardTypeIds
}
await postAcceptedCardsForServiceAndAccountType(serviceId, accountType, payload)
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, serviceId, accountType)
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.cardTypes.index, serviceExternalId, accountType)
)
} catch (err) {
next(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const userFixtures = require('@test/fixtures/user.fixtures')
const paths = require('@root/paths')

const ACCOUNT_TYPE = 'live'
const SERVICE_ID = 'service-id-123abc'
const SERVICE_EXTERNAL_ID = 'service-id-123abc'

const adminUser = new User(userFixtures.validUserResponse({
external_id: 'user-id-for-admin-user',
service_roles: {
service: {
service: { external_id: SERVICE_ID },
service: { external_id: SERVICE_EXTERNAL_ID },
role: { name: 'admin' }
}
}
Expand All @@ -24,7 +24,7 @@ const viewOnlyUser = new User(userFixtures.validUserResponse(
service_roles: {
service:
{
service: { external_id: SERVICE_ID },
service: { external_id: SERVICE_EXTERNAL_ID },
role: { name: 'view-only' }
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ const mockGetAcceptedCardTypesForServiceAndAccountType = sinon.stub().resolves({
const mockPostAcceptedCardsForServiceAndAccountType = sinon.stub().resolves({})

const { req, res, nextRequest, call } = new ControllerTestBuilder('@controllers/simplified-account/settings/card-types/card-types.controller')
.withServiceExternalId(SERVICE_ID)
.withServiceExternalId(SERVICE_EXTERNAL_ID)
.withAccountType(ACCOUNT_TYPE)
.withStubs({
'@utils/response': { response: mockResponse },
Expand Down
26 changes: 13 additions & 13 deletions app/services/clients/connector.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,14 @@ ConnectorClient.prototype = {
},

/**
* Retrieves the accepted card Types for the given external service external id and account type
* @param serviceId (required)
* @param accountType (required)
* Retrieves the accepted card Types for the given service external id and account type
* @param {String} serviceExternalId
* @param {String} accountType
* @returns {Promise<Object>}
*/
getAcceptedCardsForServiceAndAccountType: async function (serviceId, accountType) {
const url = `${this.connectorUrl}/v1/frontend/service/{serviceId}/account/{accountType}/card-types`
.replace('{serviceId}', encodeURIComponent(serviceId))
getAcceptedCardsForServiceAndAccountType: async function (serviceExternalId, accountType) {
const url = `${this.connectorUrl}/v1/frontend/service/{serviceExternalId}/account/{accountType}/card-types`
.replace('{serviceExternalId}', encodeURIComponent(serviceExternalId))
.replace('{accountType}', encodeURIComponent(accountType))
configureClient(client, url)
const response = await client.get(url, 'get accepted card types for account')
Expand All @@ -287,15 +287,15 @@ ConnectorClient.prototype = {
},

/**
* Updates the accepted card Types for to the given service and account type
* @param serviceId (required)
* @param accountType (required)
* @param payload (required)
* Updates the accepted card Types for the given service and account type
* @param {String} serviceExternalId
* @param {String} accountType
* @param {{card_types: string|string[]}} payload
* @returns {Promise<Object>}
*/
postAcceptedCardsForServiceAndAccountType: async function (serviceId, accountType, payload) {
const url = `${this.connectorUrl}/v1/frontend/service/{serviceId}/account/{accountType}/card-types`
.replace('{serviceId}', encodeURIComponent(serviceId))
postAcceptedCardsForServiceAndAccountType: async function (serviceExternalId, accountType, payload) {
const url = `${this.connectorUrl}/v1/frontend/service/{serviceExternalId}/account/{accountType}/card-types`
.replace('{serviceExternalId}', encodeURIComponent(serviceExternalId))
.replace('{accountType}', encodeURIComponent(accountType))
configureClient(client, url)
const response = await client.post(url, payload, 'post accepted card types for account')
Expand Down
4 changes: 2 additions & 2 deletions app/views/simplified-account/settings/card-types/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<h1 class="govuk-heading-l page-title">Card types</h1>

{% if isAdminUser %}
{% include "./cardTypesCheckboxes.njk" %}
{% include "./_card-types-checkboxes.njk" %}
{% else %}
{% include "./cardTypesList.njk" %}
{% include "./_card-types-list.njk" %}
{% endif %}

{% endblock %}

0 comments on commit 2349ec2

Please sign in to comment.