Skip to content

Add new check 'busy' bill runs service #922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/services/bill-runs/check-busy-bill-runs.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

/**
* Check if any bill runs are being processed or cancelled
* @module CheckBusyBillRunsService
*/

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

/**
* Check if any bill runs are busy building or cancelling
*
* A bill run is 'cancelling' if its status is `cancel`. A bill run is 'building' if its status is `processing`,
* `queued`, or `sending`.
*
* @returns {Promise<string>} the state of busy bill runs; 'cancelling', 'building', 'both', or 'none'
*/
async function go () {
const { building, cancelling } = await _fetch()

if (building && cancelling) {
return 'both'
}

if (building) {
return 'building'
}

if (cancelling) {
return 'cancelling'
}

return 'none'
}

async function _fetch () {
const results = await db.select(
db.raw("EXISTS(SELECT 1 FROM public.bill_runs bb WHERE bb.status = 'cancel') AS cancelling"),
db.raw("EXISTS(SELECT 1 FROM public.bill_runs bb WHERE bb.status IN ('processing', 'queued', 'sending')) AS building")
)

return results[0]
}

module.exports = {
go
}
96 changes: 96 additions & 0 deletions test/services/bill-runs/check-busy-bill-runs.service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

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

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

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

// Thing under test
const CheckBusyBillRunsService = require('../../../app/services/bill-runs/check-busy-bill-runs.service.js')

describe('Check Busy Bill Runs service', () => {
beforeEach(async () => {
await DatabaseSupport.clean()

// We always add a bill run that is not 'busy' to confirm the service is differentiating between 'busy' and
// 'not busy'.
await BillRunHelper.add({ status: 'ready' })
})

describe('when there are both building and cancelling bill runs', () => {
beforeEach(async () => {
await BillRunHelper.add({ status: 'cancel' })
await BillRunHelper.add({ status: 'processing' })
})

it("returns 'both'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('both')
})
})

describe('when there are cancelling bill runs', () => {
beforeEach(async () => {
await BillRunHelper.add({ status: 'cancel' })
})

it("returns 'cancelling'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('cancelling')
})
})

describe('when there are building bill runs', () => {
describe("because their status is 'processing'", () => {
beforeEach(async () => {
await BillRunHelper.add({ status: 'processing' })
})

it("returns 'building'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('building')
})
})

describe("because their status is 'queued'", () => {
beforeEach(async () => {
await BillRunHelper.add({ status: 'queued' })
})

it("returns 'building'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('building')
})
})

describe("because their status is 'sending'", () => {
beforeEach(async () => {
await BillRunHelper.add({ status: 'sending' })
})

it("returns 'building'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('building')
})
})
})

describe('when there are no building or cancelling bill runs', () => {
it("returns 'none'", async () => {
const result = await CheckBusyBillRunsService.go()

expect(result).to.equal('none')
})
})
})
Loading