-
Notifications
You must be signed in to change notification settings - Fork 0
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
New 'import' job to trigger return log and supplementary flag checks #1453
Open
jonathangoulding
wants to merge
47
commits into
main
Choose a base branch
from
feature-import-job-to-trigger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
ada1f84
New 'import' job to trigger return log and supplementary flag checks
jonathangoulding d836274
fix: js doc syntax
jonathangoulding cdf70e2
fix: js doc syntax
jonathangoulding c581418
feat: add batching to process the licence refs in promise all
jonathangoulding b5b127e
fix: log message
jonathangoulding 44b764b
refactor: batch map into promise all
jonathangoulding aa0ab6f
refactor: remove unneeded return
jonathangoulding cca3bb8
fix: stub in import licence test
jonathangoulding d9cc30b
chore: pre pr checks
jonathangoulding 8660dcb
fix: licence ref from the licence
jonathangoulding a0e47b7
fix: licence ref from the licence
jonathangoulding 0029a4c
refactor: import service to use only the services needed
jonathangoulding 2a71f8b
refactor: import service to use only the services needed
jonathangoulding c0345fd
feat: add timer
jonathangoulding 610245d
chore: pre pr checks
jonathangoulding b8acdc8
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding dcbb017
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding b24fb5a
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 501d1a0
chore: pr fixes
jonathangoulding 55afb7c
Update app/services/jobs/import/import-licence.service.js
jonathangoulding cb1a033
chore: pr fixes
jonathangoulding 07840a7
refactor: seperate process licences with per licence
jonathangoulding 7c18695
refactor: add batch size to config
jonathangoulding 116e436
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding e504bf5
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 1c2c907
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 0b42ed8
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding bb3bb39
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding c39ee02
refactor: process import licences into the import licences service
jonathangoulding 7a85f7b
feat: add p map to handle concurrency
jonathangoulding 869b4d9
Merge remote-tracking branch 'refs/remotes/origin/main' into feature-…
jonathangoulding 9c2d096
feat: add p map to handle concurrency
jonathangoulding 1c8132c
feat: add p map to handle concurrency
jonathangoulding a69e9f2
chore: pre pr check
jonathangoulding 666d2f4
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 7f2783e
Merge branch 'main' into feature-import-job-to-trigger
Cruikshanks 6650833
Prettified!
Cruikshanks 1f6bef0
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 56e3fb4
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 00b60c2
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding 93176cb
merge from main
robertparkinson 000aefc
Merge remote-tracking branch 'refs/remotes/origin/main' into feature-…
jonathangoulding 269df0f
refactor: logic to use new interfaces
jonathangoulding 24ce7ef
refactor: logic to use new interfaces
jonathangoulding 0d23e02
chore: pre pr checks
jonathangoulding dc34e65
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding b2bc8ce
Merge branch 'main' into feature-import-job-to-trigger
jonathangoulding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches all licences that are in NALD and WRLS | ||
* @module FetchLicences | ||
*/ | ||
|
||
const { db } = require('../../../../db/db.js') | ||
|
||
/** | ||
* Fetches all licences that are in NALD and WRLS | ||
* | ||
* A licence is valid if at least one licence version is not in draft | ||
* | ||
* @returns {Promise<object[]>} - An array of licences | ||
*/ | ||
async function go() { | ||
const query = ` | ||
SELECT DISTINCT ON (nal."LIC_NO") | ||
l.id as id, | ||
TO_DATE(NULLIF(nal."EXPIRY_DATE", 'null'), 'DD/MM/YYYY') AS expired_date, | ||
TO_DATE(NULLIF(nal."LAPSED_DATE", 'null'), 'DD/MM/YYYY') AS lapsed_date, | ||
TO_DATE(NULLIF(nal."EXPIRY_DATE", 'null'), 'DD/MM/YYYY') AS revoked_date | ||
FROM import."NALD_ABS_LIC_VERSIONS" nalv | ||
INNER JOIN import."NALD_ABS_LICENCES" nal | ||
ON nal."ID" = nalv."AABL_ID" | ||
AND nal."FGAC_REGION_CODE" = nalv."FGAC_REGION_CODE" | ||
INNER JOIN | ||
public.licences l ON l.licence_ref = nal."LIC_NO" | ||
WHERE nalv."STATUS" <> 'DRAFT' | ||
` | ||
|
||
const { rows } = await db.raw(query) | ||
|
||
return rows | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict' | ||
|
||
/** | ||
* Extracts and imports licence from NALD | ||
* @module ImportLicenceService | ||
*/ | ||
|
||
let pMap | ||
;(async () => { | ||
pMap = (await import('p-map')).default | ||
})() | ||
|
||
const FetchLicences = require('./fetch-licences.service.js') | ||
const ProcessImportLicence = require('./process-import-licence.service.js') | ||
const config = require('../../../../config/jobs.config.js') | ||
const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../../lib/general.lib.js') | ||
|
||
const { batchSize } = config.importLicence | ||
|
||
/** | ||
* Processes NALD licences due for import on a nightly basis | ||
* | ||
* Overnight the {@link https://github.com/DEFRA/water-abstraction-import | water-abstraction-import} app imports each | ||
* abstraction licence found in NALD. New licences are added, existing ones are updated. | ||
* | ||
* When an existing licence is imported, there are some additional processes we need to trigger. The first is | ||
* `DetermineSupplementaryBillingFlagsService`, which checks whether a licence needs to be flagged for supplementary | ||
* billing if a change has been made. | ||
* | ||
* For the same reason, `ProcessLicenceReturnLogsService` needs to check if any changes need to be made to a licence's | ||
* return logs. | ||
* | ||
* Because we are migrating from the legacy apps we couldn't trigger these processes in **water-abstraction-import**. | ||
* Instead, we have this job. The one caveat is that it needs to be scheduled to run _after_ the | ||
* {@link https://github.com/DEFRA/water-abstraction-team/blob/main/jobs/import.md#nald-import | NALD import job}, | ||
* (specifically once the NALD data has been extracted and imported into the `import` schema), but _before_ the | ||
* {@link https://github.com/DEFRA/water-abstraction-team/blob/main/jobs/import.md#nald-import | Licence import job} | ||
* runs. This is so these processes can see the differences between the NALD licence record and ours, to determine | ||
* if and what they need to do. | ||
* | ||
* > If a licence in NALD does not have a status of DRAFT, and at least one non-draft licence version then it will be | ||
* excluded | ||
* | ||
*/ | ||
async function go() { | ||
try { | ||
const startTime = currentTimeInNanoseconds() | ||
|
||
const licences = await FetchLicences.go() | ||
|
||
await _processLicences(licences) | ||
|
||
calculateAndLogTimeTaken(startTime, `Importing ${licences.length} licences from NALD`) | ||
} catch (error) { | ||
global.GlobalNotifier.omfg('Importing Licence job failed', null, error) | ||
} | ||
} | ||
|
||
/** | ||
* Process Licences | ||
* | ||
* When we process the licences we want to batch them into smaller chunks in an attempt to make the | ||
* import process as efficient as possible | ||
* | ||
* @param {object[]} licences - An array of licences | ||
* | ||
* @private | ||
*/ | ||
async function _processLicences(licences) { | ||
if (!pMap) { | ||
throw new Error('pMap is not yet loaded.') | ||
} | ||
|
||
const mapper = async (licence) => { | ||
return ProcessImportLicence.go(licence) | ||
} | ||
|
||
await pMap(licences, mapper, { concurrency: batchSize }) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
44 changes: 44 additions & 0 deletions
44
app/services/jobs/import/process-import-licence.service.js
jonathangoulding marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
/** | ||
* Process licence for the licences imported from NALD | ||
* @module ProcessImportLicence | ||
*/ | ||
|
||
const DetermineLicenceEndDateChangedService = require('../../import/determine-licence-end-date-changed.service') | ||
const ProcessBillingFlagService = require('../../licences/supplementary/process-billing-flag.service') | ||
const GenerateReturnLogsService = require('../../import/generate-return-logs.service') | ||
|
||
/** | ||
* Process licence for the licences imported from NALD | ||
* | ||
* When the licence exists in the WRLS databse and we are improting from NALD. | ||
* | ||
* We need to check if the licence needs to be flagged for supplementary billing and | ||
* determine if any new return logs need to be created depending on the current cycle. | ||
* | ||
* @param {object} licence - a licence | ||
*/ | ||
async function go(licence) { | ||
try { | ||
const payload = { | ||
expiredDate: licence.expired_date, | ||
lapsedDate: licence.lapsed_date, | ||
revokedDate: licence.revoked_date, | ||
licenceId: licence.id | ||
} | ||
|
||
const licenceEndDateChanged = await DetermineLicenceEndDateChangedService.go(payload, licence.id) | ||
|
||
if (licenceEndDateChanged) { | ||
ProcessBillingFlagService.go(payload) | ||
GenerateReturnLogsService.go(licence.id, payload) | ||
} | ||
} catch (error) { | ||
global.GlobalNotifier.omfg(`Importing licence ${licence.id}`, null, error) | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
/** | ||
* Config values used for jobs | ||
* @module JobsConfig | ||
*/ | ||
|
||
// We require dotenv directly in each config file to support unit tests that depend on this this subset of config. | ||
// Requiring dotenv in multiple places has no effect on the app when running for real. | ||
require('dotenv').config() | ||
|
||
const config = { | ||
importLicence: { | ||
batchSize: parseInt(process.env.IMORT_LICENCE_BATCH_SIZE) || 10 | ||
} | ||
} | ||
|
||
module.exports = config |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be a merge issue ?
i am not in the git blame @robertparkinson