Skip to content

Commit

Permalink
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Browse files Browse the repository at this point in the history
…ons-page-layout-and-controls
  • Loading branch information
Demwunz authored May 14, 2024
2 parents 55b9d35 + 01a63e7 commit c06f337
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 41 deletions.
6 changes: 4 additions & 2 deletions app/services/licences/fetch-licence-bills.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @module FetchLicenceBillsService
*/

const BillModel = require('../../models/bill.model')
const BillModel = require('../../models/bill.model.js')

const DatabaseConfig = require('../../../config/database.config')
const DatabaseConfig = require('../../../config/database.config.js')

/**
* Fetches all bills for a licence which is needed for the view '/licences/{id}/bills` page
Expand Down Expand Up @@ -37,7 +37,9 @@ async function _fetch (licenceId, page) {
'bills.netAmount'
])
.innerJoinRelated('billLicences')
.innerJoin('billRuns', 'billRuns.id', 'bills.billRunId')
.where('billLicences.licence_id', licenceId)
.where('billRuns.status', 'sent')
.withGraphFetched('billRun')
.modifyGraph('billRun', (builder) => {
builder.select(['batchType'])
Expand Down
134 changes: 95 additions & 39 deletions test/services/licences/fetch-licence-bills.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,124 @@ const { expect } = Code

// Test helpers
const DatabaseSupport = require('../../support/database.js')
const BillLicenceHelper = require('../../support/helpers/bill-licence.helper')
const BillHelper = require('../../support/helpers/bill.helper')
const BillRunHelper = require('../../support/helpers/bill-run.helper')
const BillLicenceHelper = require('../../support/helpers/bill-licence.helper.js')
const BillHelper = require('../../support/helpers/bill.helper.js')
const BillRunHelper = require('../../support/helpers/bill-run.helper.js')

// Thing under test
const FetchLicenceBillService = require('../../../app/services/licences/fetch-licence-bills.service')
const FetchLicenceBillService = require('../../../app/services/licences/fetch-licence-bills.service.js')

describe('Fetch Licence Bills service', () => {
const billId = '72988ec1-9fb2-4b87-b0a0-3c0be628a72c'
const billingAccountId = '0ba3b707-72ee-4296-b177-a19afff10688'
const billRunId = 'd40004d5-a99b-40a7-adf2-22d36d5b20b5'
const createdDate = new Date('2022-01-01')
const licenceId = '96d97293-1a62-4ad0-bcb6-24f68a203e6b'

describe('Fetch licence bills service', () => {
beforeEach(async () => {
await DatabaseSupport.clean()
})

describe('when the licence has bills', () => {
const createdDate = new Date('2022-01-01')

const licenceId = '96d97293-1a62-4ad0-bcb6-24f68a203e6b'
const billId = '72988ec1-9fb2-4b87-b0a0-3c0be628a72c'
const billingAccountId = '0ba3b707-72ee-4296-b177-a19afff10688'

beforeEach(async () => {
await BillLicenceHelper.add({
licenceId,
billId
})
})

describe("and they are linked to a 'sent' bill run", () => {
beforeEach(async () => {
await BillRunHelper.add({ id: billRunId, status: 'sent' })

await BillHelper.add(
{
id: billId,
billRunId,
invoiceNumber: '123',
accountNumber: 'T21404193A',
netAmount: 12345,
billingAccountId,
createdAt: createdDate
})

// Add an extra bill linked to the same bill run to test only bills for the licence are retrieved
await BillHelper.add({ billRunId })
})

it('returns results', async () => {
const result = await FetchLicenceBillService.go(licenceId, 1)

expect(result.pagination).to.equal({
total: 1
})

expect(result.bills).to.equal(
[{
accountNumber: 'T21404193A',
billRun: {
batchType: 'supplementary'
},
billingAccountId,
createdAt: createdDate,
credit: null,
deminimis: false,
financialYearEnding: 2023,
id: billId,
invoiceNumber: '123',
legacyId: null,
netAmount: 12345
}]
)
})
})

describe("but they are not linked to a 'sent' bill run", () => {
beforeEach(async () => {
await BillRunHelper.add({ id: billRunId })

await BillHelper.add(
{
id: billId,
billRunId,
invoiceNumber: '123',
accountNumber: 'T21404193A',
netAmount: 12345,
billingAccountId,
createdAt: createdDate
})

// Add an extra bill linked to the same bill run to test only bills for the licence are retrieved
await BillHelper.add({ billRunId })
})

await BillRunHelper.add({ batchType: 'annual' })
it('returns no results', async () => {
const result = await FetchLicenceBillService.go(licenceId, 1)

await BillHelper.add(
{
id: billId,
invoiceNumber: '123',
accountNumber: 'T21404193A',
netAmount: 12345,
billingAccountId,
createdAt: createdDate
expect(result.pagination).to.equal({
total: 0
})
// Add an extra record to ensure the bill is retrieved by the license id
await BillHelper.add()

expect(result.bills).to.be.empty()
})
})
})

describe('when the licence has no bills', () => {
beforeEach(async () => {
await BillRunHelper.add({ id: billRunId, status: 'sent' })
await BillHelper.add({ id: billId, billRunId })
await BillLicenceHelper.add({ billId })
})

it('returns results', async () => {
it('returns no results', async () => {
const result = await FetchLicenceBillService.go(licenceId, 1)

expect(result.pagination).to.equal({
total: 1
total: 0
})

expect(result.bills).to.equal(
[{
accountNumber: 'T21404193A',
billRun: null,
billingAccountId,
createdAt: createdDate,
credit: null,
deminimis: false,
financialYearEnding: 2023,
id: billId,
invoiceNumber: '123',
legacyId: null,
netAmount: 12345
}]
)
expect(result.bills).to.be.empty()
})
})
})

0 comments on commit c06f337

Please sign in to comment.