diff --git a/app/services/charging-module/create-transaction.service.js b/app/services/charging-module/create-transaction.service.js new file mode 100644 index 0000000000..86c62f6b54 --- /dev/null +++ b/app/services/charging-module/create-transaction.service.js @@ -0,0 +1,19 @@ +'use strict' + +/** + * Connects with the Charging Module to create a new transaction + * @module ChargingModuleCreateTransactionService + */ + +const ChargingModuleRequestLib = require('../../lib/charging-module-request.lib.js') + +async function go (billingBatchId, transactionData) { + const path = `v3/wrls/bill-runs/${billingBatchId}/transactions` + const result = await ChargingModuleRequestLib.post(path, transactionData) + + return result +} + +module.exports = { + go +} diff --git a/test/services/charging-module/create-bill-run.service.test.js b/test/services/charging-module/create-bill-run.service.test.js index 701507e9f7..eeedf05c6a 100644 --- a/test/services/charging-module/create-bill-run.service.test.js +++ b/test/services/charging-module/create-bill-run.service.test.js @@ -31,8 +31,6 @@ describe('Charge module create bill run service', () => { }) describe('when the service can create a bill run', () => { - let result - beforeEach(async () => { Sinon.stub(ChargingModuleRequestLib, 'post').resolves({ succeeded: true, @@ -50,19 +48,19 @@ describe('Charge module create bill run service', () => { } } }) - - result = await ChargingModuleCreateBillRunService.go(testRegion.regionId, 'sroc') }) it('returns a `true` success status', async () => { + const result = await ChargingModuleCreateBillRunService.go(testRegion.regionId, 'sroc') + expect(result.succeeded).to.be.true() }) it('returns the bill run id and number in the `response`', async () => { - const { response } = result + const result = await ChargingModuleCreateBillRunService.go(testRegion.regionId, 'sroc') - expect(response.body.billRun.id).to.equal('2bbbe459-966e-4026-b5d2-2f10867bdddd') - expect(response.body.billRun.billRunNumber).to.equal(10004) + expect(result.response.body.billRun.id).to.equal('2bbbe459-966e-4026-b5d2-2f10867bdddd') + expect(result.response.body.billRun.billRunNumber).to.equal(10004) }) }) diff --git a/test/services/charging-module/create-transaction.service.test.js b/test/services/charging-module/create-transaction.service.test.js new file mode 100644 index 0000000000..ced7f62af7 --- /dev/null +++ b/test/services/charging-module/create-transaction.service.test.js @@ -0,0 +1,118 @@ +'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 + +// Things we need to stub +const ChargingModuleRequestLib = require('../../../app/lib/charging-module-request.lib.js') + +// Thing under test +const ChargingModuleCreateTransactionService = require('../../../app/services/charging-module/create-transaction.service.js') + +describe('Charge module create transaction service', () => { + const billingBatchId = '2bbbe459-966e-4026-b5d2-2f10867bdddd' + const transactionData = { billingTransactionId: '2395429b-e703-43bc-8522-ce3f67507ffa' } + + afterEach(() => { + Sinon.restore() + }) + + describe('when the service can create a transaction', () => { + beforeEach(async () => { + Sinon.stub(ChargingModuleRequestLib, 'post').resolves({ + succeeded: true, + response: { + info: { + gitCommit: '273604040a47e0977b0579a0fef0f09726d95e39', + dockerTag: 'ghcr.io/defra/sroc-charging-module-api:v0.19.0' + }, + statusCode: 200, + body: { + transaction: { + id: 'fd88e6c5-8da8-4e4f-b22f-c66554cd5bf3', + clientId: transactionData.billingTransactionId + } + } + } + }) + }) + + it('returns a `true` success status', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.succeeded).to.be.true() + }) + + it('returns the CM transaction ID and our ID in the `response`', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.response.body.transaction.id).to.equal('fd88e6c5-8da8-4e4f-b22f-c66554cd5bf3') + expect(result.response.body.transaction.clientId).to.equal(transactionData.billingTransactionId) + }) + }) + + describe('when the service cannot create a transaction', () => { + describe('because the request did not return a 2xx/3xx response', () => { + beforeEach(async () => { + Sinon.stub(ChargingModuleRequestLib, 'post').resolves({ + succeeded: false, + response: { + info: { + gitCommit: '273604040a47e0977b0579a0fef0f09726d95e39', + dockerTag: 'ghcr.io/defra/sroc-charging-module-api:v0.19.0' + }, + statusCode: 401, + body: { + statusCode: 401, + error: 'Unauthorized', + message: 'Invalid JWT: Token format not valid', + attributes: { error: 'Invalid JWT: Token format not valid' } + } + } + }) + }) + + it('returns a `false` success status', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.succeeded).to.be.false() + }) + + it('returns the error in the `response`', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.response.body.statusCode).to.equal(401) + expect(result.response.body.error).to.equal('Unauthorized') + expect(result.response.body.message).to.equal('Invalid JWT: Token format not valid') + }) + }) + + describe('because the request attempt returned an error, for example, TimeoutError', () => { + beforeEach(async () => { + Sinon.stub(ChargingModuleRequestLib, 'post').resolves({ + succeeded: false, + response: new Error("Timeout awaiting 'request' for 5000ms") + }) + }) + + it('returns a `false` success status', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.succeeded).to.be.false() + }) + + it('returns the error in the `response`', async () => { + const result = await ChargingModuleCreateTransactionService.go(billingBatchId, transactionData) + + expect(result.response.statusCode).not.to.exist() + expect(result.response.body).not.to.exist() + expect(result.response.message).to.equal("Timeout awaiting 'request' for 5000ms") + }) + }) + }) +})