Skip to content

Commit

Permalink
Add new billing accounts send customer change svc (#396)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4092

We are working on migrating the change billing account address functionality from the legacy code to this project. A key part of this is being able to tell the [Charging Module API](https://github.com/DEFRA/sroc-charging-module-api) about the change!

We've [[Added a] new CHA create customer change presenter](#395) and a [[..] new CHA create customer change service](#394). We now add a new `SendCustomerChangeService` to our /billing-accounts to manage both including dealing with any errors in the process.
  • Loading branch information
Cruikshanks authored Sep 4, 2023
1 parent a7f4a37 commit bc562cf
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/services/billing-accounts/send-customer-change.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

/**
* Sends customer changes to the Charging Module
* @module SendCustomerChangeService
*/

const CreateCustomerChangePresenter = require('../../presenters/charging-module/create-customer-change.presenter.js')
const CreateCustomerChangeService = require('../charging-module/create-customer-change.service.js')
const ExpandedError = require('../../errors/expanded.error.js')

/**
* Generates the customer change request data from the model instances provided and sends it to the Charging Module
*
* When a change is made to a billing account in WRLS the Charging Module API needs to be told. It is responsible for
* generating the customer data feed to SOP which will be used when invoices are generated from the bill runs we also
* send it.
*
* This service handles taking the data forwarded from the UI, formatting it via a presenter into what the CHA expects
* and then sending the
* {@link https://defra.github.io/sroc-charging-module-api-docs/#/customer/CreateCustomerChange | POST request}.
*
* Should the request fail it will generate and throw an error.
*
* @param {module:InvoiceAccountModel} invoiceAccount The billing (invoice) account we are changing the address details
* for
* @param {module:AddressModel} address The new address
* @param {module:CompanyModel} company The agent company for the billing account if one was selected or setup by the
* user during the change address journey
* @param {module:ContactModel} contact The new contact for the billing account if an FAO was setup by the user during
* the change address journey
*/
async function go (invoiceAccount, address, company, contact) {
const requestData = CreateCustomerChangePresenter.go(invoiceAccount, address, company, contact)

const result = await CreateCustomerChangeService.go(requestData)

if (!result.succeeded) {
throw new ExpandedError('Customer change failed to send', { invoiceAccountId: invoiceAccount.invoiceAccountId })
}
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const ExpandedError = require('../../../app/errors/expanded.error.js')

// Things we need to stub
const ChargingModuleCreateCustomerChangePresenter = require('../../../app/presenters/charging-module/create-customer-change.presenter.js')
const ChargingModuleCreateCustomerChangeService = require('../../../app/services/charging-module/create-customer-change.service.js')

// Thing under test
const SendCustomerChangeService = require('../../../app/services/billing-accounts/send-customer-change.service.js')

describe('Send billing transactions service', () => {
const invoiceAccount = { invoiceAccountId: '3b53f101-d256-40f8-a6be-ddefb5f9647c' }

beforeEach(() => {
Sinon.stub(ChargingModuleCreateCustomerChangePresenter, 'go').returns({
region: 'B',
customerReference: 'B19120000A',
customerName: 'Mr W Aston',
addressLine1: 'Park Farm',
addressLine2: 'Sugar Lane',
addressLine3: 'West Waterford',
addressLine4: 'Angleton',
addressLine5: 'Southampton',
addressLine6: 'Hampshire',
postcode: 'SO74 3KD'
})
})

afterEach(() => {
Sinon.restore()
})

describe('when calling the Charging Module API is successful', () => {
beforeEach(() => {
Sinon.stub(ChargingModuleCreateCustomerChangeService, 'go').resolves({
succeeded: true
})
})

it('does not throw an error', async () => {
await expect(SendCustomerChangeService.go(invoiceAccount)).not.to.reject()
})
})

describe('when calling the Charging Module API is unsuccessful', () => {
beforeEach(() => {
Sinon.stub(ChargingModuleCreateCustomerChangeService, 'go').resolves({
succeeded: false
})
})

it('throws an error', async () => {
const result = await expect(SendCustomerChangeService.go(invoiceAccount)).to.reject()

expect(result).to.be.an.instanceOf(ExpandedError)
expect(result.message).to.equal('Customer change failed to send')
expect(result.invoiceAccountId).to.equal(invoiceAccount.invoiceAccountId)
})
})
})

0 comments on commit bc562cf

Please sign in to comment.