-
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.
Merge branch 'main' into new-view-completed-monthly-return-logs-page
- Loading branch information
Showing
13 changed files
with
212 additions
and
35 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
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
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 |
---|---|---|
|
@@ -71,7 +71,7 @@ describe('View Licence presenter', () => { | |
|
||
describe('when the licence does not have a primary user (registered user)', () => { | ||
beforeEach(() => { | ||
licence.licenceDocumentHeader.licenceEntityRole = null | ||
licence.licenceDocumentHeader.licenceEntityRoles = [] | ||
}) | ||
|
||
it('returns "Unregistered licence"', () => { | ||
|
@@ -347,16 +347,18 @@ function _licence() { | |
licenceDocumentHeader: { | ||
id: 'e8f491f0-0c60-4083-9d41-d2be69f17a1e', | ||
licenceName: 'Between two ferns', | ||
licenceEntityRole: { | ||
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d', | ||
licenceEntity: { | ||
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2', | ||
user: { | ||
id: 10036, | ||
username: '[email protected]' | ||
licenceEntityRoles: [ | ||
{ | ||
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d', | ||
licenceEntity: { | ||
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2', | ||
user: { | ||
id: 10036, | ||
username: '[email protected]' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
licenceSupplementaryYears: [], | ||
workflows: [{ id: 'b6f44c94-25e4-4ca8-a7db-364534157ba7', status: 'to_setup' }] | ||
|
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 |
---|---|---|
|
@@ -101,16 +101,18 @@ function _licence() { | |
licenceDocumentHeader: { | ||
id: 'e8f491f0-0c60-4083-9d41-d2be69f17a1e', | ||
licenceName: 'Between two ferns', | ||
licenceEntityRole: { | ||
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d', | ||
licenceEntity: { | ||
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2', | ||
user: { | ||
id: 10036, | ||
username: '[email protected]' | ||
licenceEntityRoles: [ | ||
{ | ||
id: 'd7eecfc1-7afa-49f7-8bef-5dc477696a2d', | ||
licenceEntity: { | ||
id: 'ba7702cf-cd87-4419-a04c-8cea4e0cfdc2', | ||
user: { | ||
id: 10036, | ||
username: '[email protected]' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
licenceSupplementaryYears: [], | ||
workflows: [{ id: 'b6f44c94-25e4-4ca8-a7db-364534157ba7', status: 'to_setup' }] | ||
|
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