Skip to content

Commit

Permalink
avoid using raw response
Browse files Browse the repository at this point in the history
  • Loading branch information
oswaldquek committed Dec 2, 2024
1 parent 9a3bb58 commit e49b0ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ControllerTestBuilder = require('@test/test-helpers/simplified-account/con
const sinon = require('sinon')
const { expect } = require('chai')
const Service = require('@models/Service.class')
const GatewayAccount = require('@models/GatewayAccount.class')

const mockResponse = sinon.spy()

Expand All @@ -13,10 +14,19 @@ const { req, res, call } = new ControllerTestBuilder('@controllers/simplified-ac
external_id: SERVICE_ID
}))
.withAccountType(ACCOUNT_TYPE)
.withAccount({
.withAccount(new GatewayAccount({
type: ACCOUNT_TYPE,
allowMoto: true
})
allow_moto: true,
gateway_account_id: 1,
gateway_account_credentials: [{
external_id: 'creds-id',
payment_provider: 'worldpay',
state: 'CREATED',
created_date: '2024-11-29T11:58:36.214Z',
gateway_account_id: 1,
credentials: {}
}]
}))
.withStubs({
'@utils/response': { response: mockResponse }
})
Expand Down
10 changes: 10 additions & 0 deletions app/models/GatewayAccount.class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const GatewayAccountCredential = require('@models/GatewayAccountCredential.class')

const CREDENTIAL_STATE = {
CREATED: 'CREATED',
ENTERED: 'ENTERED',
VERIFIED: 'VERIFIED_WITH_LIVE_PAYMENT',
ACTIVE: 'ACTIVE',
RETIRED: 'RETIRED'
}

/**
* @class GatewayAccount
* @property {string} name - The name of the gateway account
Expand Down Expand Up @@ -44,6 +52,8 @@ class GatewayAccount {
if (gatewayAccountData?.gateway_account_credentials) {
this.gatewayAccountCredentials = gatewayAccountData?.gateway_account_credentials
.map(credentialData => new GatewayAccountCredential(credentialData))

this.activeCredential = this.gatewayAccountCredentials.filter((credential) => credential.state === CREDENTIAL_STATE.ACTIVE)[0] || null
}
// TODO: this is a temporary compatability fix! If you find yourself using this for new code
// you should instead add any rawResponse data as part of the constructor
Expand Down
4 changes: 1 addition & 3 deletions app/models/WorldpayTasks.class.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { getActiveCredential } = require('@utils/credentials')

class WorldpayTasks {
/**
* @param {GatewayAccount} gatewayAccount
Expand All @@ -10,7 +8,7 @@ class WorldpayTasks {
this.tasks = []
this.incompleteTasks = true

const credential = getActiveCredential(gatewayAccount.rawResponse)
const credential = gatewayAccount.activeCredential

if (gatewayAccount.allowMoto) {
const worldpayCredentials = {
Expand Down

0 comments on commit e49b0ad

Please sign in to comment.