Skip to content

Commit

Permalink
Fetching the charge versions data (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beckyrose200 authored Nov 10, 2022
1 parent 50bfd1d commit 3e339ba
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 39 deletions.
8 changes: 4 additions & 4 deletions app/controllers/test/supplementary.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const TestSupplementaryService = require('../../services/test/supplementary.service.js')
const SupplementaryService = require('../../services/test/supplementary.service.js')

class TestSupplementaryController {
class SupplementaryController {
static async index (_request, h) {
const result = await TestSupplementaryService.go()
const result = await SupplementaryService.go()

return h.response(result).code(200)
}
}

module.exports = TestSupplementaryController
module.exports = SupplementaryController
4 changes: 2 additions & 2 deletions app/routes/test.routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const TestSupplementaryController = require('../controllers/test/supplementary.controller')
const SupplementaryController = require('../controllers/test/supplementary.controller')

const routes = [
{
method: 'GET',
path: '/test/supplementary',
handler: TestSupplementaryController.index,
handler: SupplementaryController.index,
options: {
description: 'Test endpoint which returns all selected charge versions'
}
Expand Down
25 changes: 18 additions & 7 deletions app/services/test/supplementary.service.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
'use strict'

/**
* @module TestSupplementaryService
* @module SupplementaryService
*/

const { db } = require('../../../db/db')

/**
* Returns charge versions selected for supplementary billing
* At present this returns a set response until further development
*/
class TestSupplementaryService {

// Format into the response and return the data
class SupplementaryService {
static async go () {
const chargeVersions = await this._fetchChargeVersions()
const response = {
chargeVersions: [
{ id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' },
{ id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' }
]
chargeVersions
}

return response
}

static async _fetchChargeVersions () {
const chargeVersions = db.table('water.charge_versions')
.where('scheme', 'sroc')
.select('chargeVersionId')
.select('licenceRef')

return chargeVersions
}
}

module.exports = TestSupplementaryService
module.exports = SupplementaryService
27 changes: 12 additions & 15 deletions test/controllers/test/supplementary.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const Sinon = require('sinon')
const { describe, it, beforeEach, after } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const SupplementaryService = require('../../../app/services/test/supplementary.service.js')

// For running our service
const { init } = require('../../../app/server')

describe('Test supplementary controller', () => {
describe('Supplementary controller', () => {
let server

beforeEach(async () => {
Expand All @@ -22,29 +25,23 @@ describe('Test supplementary controller', () => {
Sinon.restore()
})

describe('Returning selected charge versions: GET /test/supplementary', () => {
let response

describe('GET /test/supplementary', () => {
const options = {
method: 'GET',
url: '/test/supplementary'
}

let response

beforeEach(async () => {
response = await server.inject(options)
})
Sinon.stub(SupplementaryService, 'go').resolves({ chargeVersions: [] })

it('returns a 200 status code', async () => {
expect(response.statusCode).to.equal(200)
response = await server.inject(options)
})

it('returns a JSON response', async () => {
const jsonBody = JSON.parse(response.payload)
expect(jsonBody).to.equal({
chargeVersions: [
{ id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' },
{ id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' }
]
describe('when the request is valid', () => {
it('returns success status 200', async () => {
expect(response.statusCode).to.equal(200)
})
})
})
Expand Down
51 changes: 40 additions & 11 deletions test/services/test/supplementary.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,49 @@
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

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

// Test helpers
const ChargeVersionHelper = require('../../support/helpers/charge_version.helper')
const DatabaseHelper = require('../../support/helpers/database.helper')

// Thing under test
const TestSupplementaryService = require('../../../app/services/test/supplementary.service.js')

describe('Test supplementary service', () => {
it('returns a set response', async () => {
const result = await TestSupplementaryService.go()
expect(result).to.equal({
chargeVersions: [
{ id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' },
{ id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' }
]
const SupplementaryService = require('../../../app/services/test/supplementary.service.js')

describe('Supplementary service', () => {
const testData = {
sroc: { licence_ref: '01/123', scheme: 'sroc' },
alcs: { licence_ref: '01/456', scheme: 'alcs' }
}
let testRecords

beforeEach(async () => {
await DatabaseHelper.clean()
})

describe('when there are charge versions to be included in supplimentery billing', () => {
beforeEach(async () => {
testRecords = await ChargeVersionHelper.add([testData.sroc, testData.alcs])
})

it('returns only those that match', async () => {
const result = await SupplementaryService.go()

expect(result.chargeVersions.length).to.equal(1)
expect(result.chargeVersions[0].charge_version_id).to.equal(testRecords[0].charge_version_id)
})
})

describe('when there are no charge versions to be included in supplimentery billing', () => {
beforeEach(async () => {
testRecords = await ChargeVersionHelper.add(testData.alcs)
})

it('returns no matches', async () => {
const result = await SupplementaryService.go()

expect(result.chargeVersions.length).to.equal(0)
})
})
})
19 changes: 19 additions & 0 deletions test/support/helpers/charge_version.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

/**
* @module ChargeVersionHelper
*/

const { db } = require('../../../db/db')

class ChargeVersionHelper {
static async add (data) {
const result = await db.table('water.charge_versions')
.insert(data)
.returning('charge_version_id')

return result
}
}

module.exports = ChargeVersionHelper

0 comments on commit 3e339ba

Please sign in to comment.