Skip to content
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

Licence import flagging for supplementary billing #1338

Merged
merged 108 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 96 commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
565d571
Licence import flagging for supplementary billing
Beckyrose200 Sep 17, 2024
143b5c1
WIP
Beckyrose200 Sep 19, 2024
2e530b2
Refactoring code
Beckyrose200 Sep 19, 2024
97c4fbf
More refactoring and adding comments
Beckyrose200 Sep 19, 2024
0308fb9
Remove console logs
Beckyrose200 Sep 20, 2024
1665d0d
Update process-licence service test
Beckyrose200 Sep 20, 2024
ace2dfd
Update persist licence service
Beckyrose200 Sep 20, 2024
298360a
TransformLicenceSupplementaryFlags update comments and write unit tests
Beckyrose200 Sep 23, 2024
50291f6
Update requires
Beckyrose200 Sep 23, 2024
4ec00b8
Fix docstring comment
Beckyrose200 Sep 23, 2024
482b638
Update docstring comment
Beckyrose200 Sep 23, 2024
27cb64f
Add unit tests for fetch-licence-charge-versions service
Beckyrose200 Sep 23, 2024
d12ffaa
Update module
Beckyrose200 Sep 23, 2024
a371908
Increase line coverage on test
Beckyrose200 Sep 23, 2024
6b2873a
Update modules
Beckyrose200 Sep 23, 2024
0c2e581
Change presenter to service
Beckyrose200 Sep 23, 2024
1c3db90
WIP change on licence end dates
Beckyrose200 Sep 24, 2024
b2d9aa5
Update licence end dates function
Beckyrose200 Sep 24, 2024
4a8f63b
Flag for supplementary billing service and test
Beckyrose200 Sep 24, 2024
d0d26aa
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Sep 24, 2024
786c483
Fix spelling error in file
Beckyrose200 Sep 24, 2024
16073bc
Fix test file after merge conflict
Beckyrose200 Sep 24, 2024
7b0a1c7
Fix unit test after merge conflict
Beckyrose200 Sep 24, 2024
42c747e
Remove .only
Beckyrose200 Sep 24, 2024
a7be311
Update flag for supplementary billing service and test
Beckyrose200 Sep 24, 2024
0cddbdb
Sonar Cloud
Beckyrose200 Sep 24, 2024
039f747
Refactor flag for supp billing
Beckyrose200 Sep 24, 2024
74a1aa1
WIP
Beckyrose200 Sep 27, 2024
5b0f9ae
Amend Process Licence Service
Beckyrose200 Sep 27, 2024
e8cb0cf
Remove persisting at the import level
Beckyrose200 Sep 27, 2024
8c93e3d
Flag for supplementary billing service
Beckyrose200 Oct 2, 2024
1321be4
flag for supplementary billing unit test
Beckyrose200 Oct 2, 2024
2d25720
WIP
Beckyrose200 Oct 3, 2024
a196704
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 3, 2024
446d42f
Remove unused services
Beckyrose200 Oct 3, 2024
c6a0672
Add try/catch to flag for supplementary billing and unit test
Beckyrose200 Oct 3, 2024
aeb1c10
Remove unused unit tests
Beckyrose200 Oct 3, 2024
297045d
Update fetch charge versions comments
Beckyrose200 Oct 3, 2024
0974512
Add doc string comments to persist supplementary billing flags service
Beckyrose200 Oct 3, 2024
6d27b69
Add doc string comment to determine supplementary billing flags service
Beckyrose200 Oct 3, 2024
09c6614
Lint
Beckyrose200 Oct 3, 2024
fb07306
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 3, 2024
de06a88
Fetch charge versions service and unit tests
Beckyrose200 Oct 3, 2024
07def96
Fix JSDoc comment
Beckyrose200 Oct 3, 2024
9fdd407
Fix licence persisting test
Beckyrose200 Oct 3, 2024
ae4afae
Update fetch charge versions test
Beckyrose200 Oct 4, 2024
4c4f4ce
persist supplementary billing flags unit tests
Beckyrose200 Oct 4, 2024
e809fb6
Updated determine supplementary billing flags
Beckyrose200 Oct 4, 2024
b9aa32a
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 7, 2024
7ef9ef8
Spelling
Beckyrose200 Oct 7, 2024
1e1c24a
Linting
Beckyrose200 Oct 7, 2024
c580b36
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 7, 2024
3517a37
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 7, 2024
555ef6d
Process Imported Licence Service
Beckyrose200 Oct 8, 2024
915ace0
wip determine-supp-billing-flags
Beckyrose200 Oct 8, 2024
4c2f5d2
wip - fetch-charge-versions-service
Beckyrose200 Oct 8, 2024
b18a3f2
Slim services down
Beckyrose200 Oct 8, 2024
24e05ad
WIP
Beckyrose200 Oct 8, 2024
bd11de0
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 8, 2024
a35c42d
Update Process-Licence-Service unit tests
Beckyrose200 Oct 8, 2024
3cf4ca4
Determine supplementary billing flags service and unit tests
Beckyrose200 Oct 8, 2024
90cfb58
fetch existing licence details service unit tests
Beckyrose200 Oct 8, 2024
e6d2eef
Persist supplementary billing flags service and test
Beckyrose200 Oct 8, 2024
8c65f10
Revert spelling issue
Beckyrose200 Oct 8, 2024
0f40d26
Revert lint issue
Beckyrose200 Oct 8, 2024
31ef580
Remove wrong file name
Beckyrose200 Oct 8, 2024
13f6b30
Process imported licence service unit tests
Beckyrose200 Oct 9, 2024
74a2165
Refactored process imported licence unit test
Beckyrose200 Oct 9, 2024
2393462
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 9, 2024
2ac5f85
Sonar cloud
Beckyrose200 Oct 9, 2024
bda30d0
Sonar cloud
Beckyrose200 Oct 9, 2024
e1ee2d9
Remove unnecessary getTime()
Beckyrose200 Oct 9, 2024
1bc5974
Fix comments
Beckyrose200 Oct 9, 2024
424346a
Remove comment
Beckyrose200 Oct 9, 2024
d7f6126
Update omfg call and rename naldLicence to importedLicence
Beckyrose200 Oct 10, 2024
7b33ad6
add comments to _whereClauses function
Beckyrose200 Oct 10, 2024
aaa53b7
Rename params
Beckyrose200 Oct 10, 2024
5699570
Return on persist supplementary billing flags
Beckyrose200 Oct 10, 2024
adc731f
Return in process imported licence service
Beckyrose200 Oct 10, 2024
fa415e4
Update comment to explain why we use String not getTime()
Beckyrose200 Oct 10, 2024
c6fe8d7
Update comment for sroc supplementary flag
Beckyrose200 Oct 10, 2024
c0e95a2
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 10, 2024
7bf8b9f
Remove nald key word form unit tests
Beckyrose200 Oct 10, 2024
bd7ce55
Remove nald key word from unit tests
Beckyrose200 Oct 10, 2024
27c817a
ImportedLicence not transformedLicence
Beckyrose200 Oct 10, 2024
e0b286a
Remove transformed licence replace with imported
Beckyrose200 Oct 10, 2024
4be8934
Update require order
Beckyrose200 Oct 10, 2024
153629a
Sonar cloud ~ Refactor return consistently
Beckyrose200 Oct 10, 2024
dbcc4fa
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 10, 2024
0c40a36
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 10, 2024
0e42845
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 10, 2024
7294b9b
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 11, 2024
7c2efb3
Address peer review comments
Beckyrose200 Oct 11, 2024
b3290e9
Address peer review comments
Beckyrose200 Oct 11, 2024
70b7378
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 11, 2024
54056f0
Fix broken test
Beckyrose200 Oct 11, 2024
4e3b205
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 14, 2024
3454e96
determine-supplementary-billing-flags service unit test refactored
Beckyrose200 Oct 14, 2024
51255b1
remove.only
Beckyrose200 Oct 14, 2024
f995adc
Refactor persist supplementary billing flags unit tests
Beckyrose200 Oct 14, 2024
9c515c5
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 14, 2024
79d9e55
Update unit tests for fetch and persist services
Beckyrose200 Oct 15, 2024
d146201
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 15, 2024
a80aba3
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 17, 2024
109ce12
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 18, 2024
a73070b
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 21, 2024
fa84c00
Merge branch 'main' into licence-lapsed-supplemntary-billing
Beckyrose200 Oct 21, 2024
7257d4e
Update params JSDoc
Beckyrose200 Oct 21, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

/**
* Determines if an imported licence has a new end date
* @module DetermineSupplementaryBillingFlagsService
*/

const LicenceModel = require('../../models/licence.model.js')
const ProcessImportedLicenceService = require('../licences/supplementary/process-imported-licence.service.js')

/**
* Determines if an imported licence has a new end date.
*
* This service is responsible for determining whether a licence imported has a new end day and therefore should be
* flagged for supplementary billing.
*
* It compares the licences end dates (such as lapsed, revoked or expired dates) between WRLS licence and the imported
* data, and if there is a change in the dates allows the licence to go on to determining the flags.
*
* @param {object} importedLicence - the imported licence
* @param {object} licenceId - the WRLS licence data
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns {Promise} A promise is returned but it does not resolve to anything we expect the caller to use
*/
async function go (importedLicence, licenceId) {
try {
const licenceChanged = await _licenceChanged(importedLicence, licenceId)

if (!licenceChanged) {
return
}

return ProcessImportedLicenceService.go(importedLicence, licenceId)
} catch (error) {
global.GlobalNotifier.omfg('Determine supplementary billing flags on import failed ', { licenceId }, error)
}
}

async function _licenceChanged (importedLicence, licenceId) {
const query = LicenceModel.query()
.select(['id'])
.where('id', licenceId)

_whereClauses(query, importedLicence)

const result = await query

return result.length === 0
}

/**
* Adds where clauses to compare the end dates (expired, revoked, lapsed) of the imported licence with those stored
* in the database. It handles where the end dates can be null.
*
* In SQL, comparing `null` values using a regular `where` clause does not work as expected because
* `null` represents the absence of a value and `null = null` returns false. To address this, we use
* `whereNull` to explicitly check for null values in the database.
*
* If an end date is present on the imported licence, the query uses a standard `where` clause to check
* for a match. If the end date is null, the query uses `whereNull` to compare against the null values.
*
* This ensures that value types (dates and null) can be correctly compared, allowing us to detect changes
* between the imported licence and the existing WRLS licence data.
*
* @private
*/
function _whereClauses (query, importedLicence) {
const { expiredDate, lapsedDate, revokedDate } = importedLicence

if (expiredDate) {
query.where('expiredDate', expiredDate)
} else {
query.whereNull('expiredDate')
}

if (revokedDate) {
query.where('revokedDate', revokedDate)
} else {
query.whereNull('revokedDate')
}

if (lapsedDate) {
query.where('lapsedDate', lapsedDate)
} else {
query.whereNull('lapsedDate')
}
}

module.exports = {
go
}
12 changes: 8 additions & 4 deletions app/services/import/legacy/process-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @module ImportLegacyProcessLicenceService
*/

const DetermineSupplementaryBillingFlagsService = require('../determine-supplementary-billing-flags.service.js')
const LicenceStructureValidator = require('../../../validators/import/licence-structure.validator.js')
const PersistImportService = require('../persist-import.service.js')
const ProcessLicenceReturnLogsService = require('../../jobs/return-logs/process-licence-return-logs.service.js')
Expand Down Expand Up @@ -33,6 +34,13 @@ async function go (licenceRef) {
const { naldLicenceId, regionCode, transformedLicence, wrlsLicenceId } =
await TransformLicenceService.go(licenceRef)

// We have other services that need to know when a licence has been imported. However, they only care about changes
// to existing licences. So, if wrlsLicenceId is populated it means the import is updating an existing licence.
if (wrlsLicenceId) {
DetermineSupplementaryBillingFlagsService.go(transformedLicence, wrlsLicenceId)
await ProcessLicenceReturnLogsService.go(wrlsLicenceId)
}

// Pass the transformed licence through each transformation step, building the licence as we go
await TransformLicenceVersionsService.go(regionCode, naldLicenceId, transformedLicence)
await TransformLicenceVersionPurposesService.go(regionCode, naldLicenceId, transformedLicence)
Expand All @@ -52,10 +60,6 @@ async function go (licenceRef) {
// Either insert or update the licence in WRLS
const licenceId = await PersistImportService.go(transformedLicence, transformedCompanies)

if (wrlsLicenceId) {
await ProcessLicenceReturnLogsService.go(wrlsLicenceId)
}

calculateAndLogTimeTaken(startTime, 'Legacy licence import complete', { licenceId, licenceRef })
} catch (error) {
global.GlobalNotifier.omfg('Legacy licence import errored', { licenceRef }, error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

/**
* Fetches existing supplementary details about a licence being updated during import
* @module FetchExistingLicenceDetailsService
*/

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

/**
* Fetches existing supplementary details about a licence being updated during import
*
* We need to get the existing end dates so we can compare with those being imported to determine which have been
* changed (it could be more than one).
*
* The query also determines what relevant charge versions exist for the licence. When processing the licence we have
* to work out whether to set the include in presroc, include in sroc, and include in two-part tariff (by way of
* supplementary billing years) flags on the licence.
*
* We can skip those steps if we know the licence being updated doesn't have the relevant charge versions. If it doesn't
* have them, then it could never have been previously billed for them!
*
* @param {string} licenceId - The UUID of the licence details being fetched
*
* @returns {Promise<object>} - The data needed to determine which supplementary flags the licence needs
*/
async function go (licenceId) {
const query = _query()

const { rows: [row] } = await db.raw(query, [licenceId])

return row
}

function _query () {
return `
SELECT
l.id,
l.expired_date,
l.lapsed_date,
l.revoked_date,
(CASE l.include_in_presroc_billing
WHEN 'yes' THEN TRUE
ELSE FALSE
END) AS flagged_for_presroc,
l.include_in_sroc_billing AS flagged_for_sroc,
EXISTS(
SELECT
1
FROM
public.charge_versions cv
WHERE
cv.licence_id = l.id
AND cv.start_date < '2022-04-01'
) AS pre_sroc_charge_versions,
EXISTS(
SELECT
1
FROM
public.charge_versions cv
WHERE
cv.licence_id = l.id
AND cv.start_date > '2022-03-31'
) AS sroc_charge_versions,
EXISTS(
SELECT
1
FROM
public.charge_versions cv
INNER JOIN
public.charge_references cr ON cr.charge_version_id = cv.id
INNER JOIN
public.charge_elements ce ON ce.charge_reference_id = cr.id
WHERE
cv.licence_id = l.id
AND cv.start_date > '2022-03-31'
AND cr.adjustments->>'s127' = 'true'
AND ce.section_127_Agreement = TRUE
) AS two_part_tariff_charge_versions
FROM
public.licences l
WHERE
l.id = ?;
`
}

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

/**
* Persists the supplementary billing flags for a licence
* @module PersistSupplementaryBillingFlagsService
*/

const CreateLicenceSupplementaryYearService = require('./create-licence-supplementary-year.service.js')
const LicenceModel = require('../../../models/licence.model.js')

/**
* Persists the supplementary billing flags for a licence
*
* Updates the licences includeInPresrocBilling and includeInSrocBilling flags.
* Adds financial years related to two-part tariff billing into the LicenceSupplementaryYears table.
*
* NOTE: Due to the column data type of the includeInPresrocBilling & includeInSrocBilling, one is a string value and
* one is a boolean.
*
* @param {object[]} twoPartTariffBillingYears - The years that need persisting in the LicenceSupplementaryYears table
* @param {boolean} flagForPreSrocSupplementary - `true` or `false` depending on if the licence needs to be flagged
* for pre sroc billing
* @param {boolean} flagForSrocSupplementary - `true` or `false` depending on if the licence needs to be flagged for
* sroc billing
* @param {string} licenceId - The UUID of the licence that needs the flags persisting for
*
* @returns {Promise<object>} - Resolves with the result of persisting two-part tariff billing years,
*/
async function go (twoPartTariffBillingYears, flagForPreSrocSupplementary, flagForSrocSupplementary, licenceId) {
const includeInPresrocBilling = flagForPreSrocSupplementary ? 'yes' : 'no'

await _updateLicenceFlags(includeInPresrocBilling, flagForSrocSupplementary, licenceId)

return _flagForLicenceSupplementaryYears(twoPartTariffBillingYears, licenceId)
}

/**
* Persists two-part tariff financial years in the LicenceSupplementaryYears table.
* @private
*/
async function _flagForLicenceSupplementaryYears (twoPartTariffBillingYears, licenceId) {
if (twoPartTariffBillingYears.length === 0) {
return
}

const twoPartTariff = true

return CreateLicenceSupplementaryYearService.go(licenceId, twoPartTariffBillingYears, twoPartTariff)
}

async function _updateLicenceFlags (includeInPresrocBilling, flagForSrocSupplementary, licenceId) {
return LicenceModel.query()
.patch({ includeInPresrocBilling, includeInSrocBilling: flagForSrocSupplementary })
.where('id', licenceId)
}

module.exports = {
go
}
Loading
Loading