-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new quarterly return versions for water companies (#1648)
https://eaflood.atlassian.net/browse/WATER-4881 As part of our work in setting up quarterly returns for water companies we need to run a one off migration for each of them to create a new return version. This PR creates an end point that can be hit as part of a job. It will loop through each of the water companies that do not have a return version starting next year and create one.
- Loading branch information
1 parent
c40da27
commit 82a3c82
Showing
8 changed files
with
167 additions
and
3 deletions.
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
74 changes: 74 additions & 0 deletions
74
app/services/jobs/return-logs/fetch-water-undertakers.service.js
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,74 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches all water undertakers that have a valid licence after 1/04/2025 | ||
* @module FetchWaterUndertakersService | ||
*/ | ||
|
||
const { db } = require('../../../../db/db.js') | ||
const LicenceModel = require('../../../models/licence.model.js') | ||
const ReturnRequirementModel = require('../../../models/return-requirement.model.js') | ||
const ReturnVersionModel = require('../../../models/return-version.model.js') | ||
|
||
/** | ||
* Fetches all water undertaker licences that will have not been ended before 1/04/2025 | ||
* | ||
* @returns {Promise<module:LicenceModel>} the matching licences | ||
*/ | ||
async function go() { | ||
const quarterlyStartDate = new Date('2025-04-01') | ||
|
||
return await LicenceModel.query() | ||
.select([ | ||
'id', | ||
'expiredDate', | ||
'lapsedDate', | ||
'licenceRef', | ||
'revokedDate', | ||
'startDate', | ||
'waterUndertaker', | ||
db.raw("regions->>'regionalChargeArea' as regionalChargeArea") | ||
]) | ||
.withGraphFetched('returnVersions') | ||
.modifyGraph('returnVersions', (builder) => { | ||
builder | ||
.select(['id', 'startDate', 'reason']) | ||
.where('startDate', '<', quarterlyStartDate) | ||
.where('status', 'current') | ||
// A return version must include return requirements in order for us to be able to copy from it | ||
.whereExists( | ||
ReturnRequirementModel.query() | ||
.select(1) | ||
.whereColumn('returnVersions.id', 'returnRequirements.returnVersionId') | ||
) | ||
.orderBy('startDate', 'desc') | ||
}) | ||
.where('waterUndertaker', true) | ||
.where((builder) => { | ||
builder.whereNull('expiredDate').orWhere('expiredDate', '>=', quarterlyStartDate) | ||
}) | ||
.where((builder) => { | ||
builder.whereNull('lapsedDate').orWhere('lapsedDate', '>=', quarterlyStartDate) | ||
}) | ||
.where((builder) => { | ||
builder.whereNull('revokedDate').orWhere('revokedDate', '>=', quarterlyStartDate) | ||
}) | ||
.whereNotExists( | ||
ReturnVersionModel.query() | ||
.where('returnVersions.status', 'current') | ||
.where('startDate', '>=', quarterlyStartDate) | ||
.whereColumn('returnVersions.licenceId', 'licences.id') | ||
) | ||
.whereExists( | ||
ReturnVersionModel.query() | ||
.where('returnVersions.status', 'current') | ||
.where('quarterlyReturns', false) | ||
.where('startDate', '<', quarterlyStartDate) | ||
.whereColumn('returnVersions.licenceId', 'licences.id') | ||
) | ||
.whereIn('id', ['5599e2b2-4047-48e6-84aa-f6fc23741e05', '95de4f0f-f13f-4ea6-9460-b7f5d6ce8381']) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
65 changes: 65 additions & 0 deletions
65
app/services/jobs/return-logs/return-version-migration.service.js
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,65 @@ | ||
'use strict' | ||
|
||
/** | ||
* Determines which licences need new return versions created for quarterly returns | ||
* @module ReturnVersionMigrationService | ||
*/ | ||
|
||
const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../../lib/general.lib.js') | ||
const FetchWaterUndertakersService = require('./fetch-water-undertakers.service.js') | ||
const GenerateFromExisintRequirementsService = require('../../return-versions/setup/existing/generate-from-existing-requirements.service.js') | ||
const GenerateReturnVersionService = require('../../return-versions/setup/check/generate-return-version.service.js') | ||
const LicencesConfig = require('../../../../config/licences.config.js') | ||
const PersistReturnVersionService = require('../../return-versions/setup/check/persist-return-version.service.js') | ||
const UserModel = require('../../../models/user.model.js') | ||
|
||
/** | ||
* Determines which licences need new return versions created for quarterly returns | ||
* | ||
* This service will generate new return versions for each current licence that is a water undertaker (a water company). | ||
* The new return version will start on 1/04/2025. | ||
*/ | ||
async function go() { | ||
try { | ||
const startTime = currentTimeInNanoseconds() | ||
|
||
const usernames = ['[email protected]'] | ||
|
||
if (LicencesConfig.returnVersionBatchUser) { | ||
usernames.unshift(LicencesConfig.returnVersionBatchUser) | ||
} | ||
|
||
const user = await UserModel.query().select(['id']).whereIn('username', usernames).first() | ||
|
||
const licences = await FetchWaterUndertakersService.go() | ||
|
||
for (const licence of licences) { | ||
const returnRequirements = await GenerateFromExisintRequirementsService.go(licence.returnVersions[0].id) | ||
|
||
const data = { | ||
licence: { | ||
id: licence.id | ||
}, | ||
multipleUpload: returnRequirements.multipleUpload, | ||
note: { | ||
content: 'Changed due to water company licences moving to quarterly returns' | ||
}, | ||
reason: 'change-to-return-requirements', | ||
returnVersionStartDate: new Date('2025-04-01'), | ||
requirements: returnRequirements.requirements, | ||
quarterlyReturns: true | ||
} | ||
|
||
const returnVersionData = await GenerateReturnVersionService.go(data, user.id) | ||
await PersistReturnVersionService.go(returnVersionData) | ||
} | ||
|
||
calculateAndLogTimeTaken(startTime, 'Return version migration job complete') | ||
} catch (error) { | ||
global.GlobalNotifier.omfg('Return version migration job failed', 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
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