Skip to content

Commit

Permalink
Merge branch 'main' into import-company-for-licence
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangoulding authored Sep 3, 2024
2 parents ef2f160 + 709ff2c commit 529f77c
Show file tree
Hide file tree
Showing 2 changed files with 432 additions and 124 deletions.
105 changes: 79 additions & 26 deletions app/services/jobs/return-logs/fetch-return-logs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Fetches data needed for the generating return logs
* @module FetchLicenceWithoutReturnsService
* @module FetchReturnLogsService
*/

const ReturnLogModel = require('../../../models/return-log.model.js')
Expand All @@ -11,26 +11,29 @@ const ReturnVersionModel = require('../../../models/return-version.model.js')

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

const allYearDueDateDay = 28
const allYearDueDateMonth = 3
const allYearEndDay = 31
const allYearEndMonth = 2
const allYearStartDay = 1
const allYearStartMonth = 3
const ALL_YEAR_DUE_DATE_DAY = 28
const ALL_YEAR_DUE_DATE_MONTH = 3
const ALL_YEAR_END_DAY = 31
const ALL_YEAR_END_MONTH = 2
const ALL_YEAR_START_DAY = 1
const ALL_YEAR_START_MONTH = 3

const summerDueDateDay = 28
const summerDueDateMonth = 10
const summerEndDay = 31
const summerEndMonth = 9
const summerStartDay = 1
const summerStartMonth = 10
const SUMMER_DUE_DATE_DAY = 28
const SUMMER_DUE_DATE_MONTH = 10
const SUMMER_END_DAY = 31
const SUMMER_END_MONTH = 9
const SUMMER_START_DAY = 1
const SUMMER_START_MONTH = 10

const endOfSummerCycle = new Date(new Date().getFullYear() + 1, summerEndMonth, summerEndDay)
const endOfWinterAndAllYearCycle = new Date(new Date().getFullYear() + 1, allYearEndMonth, allYearEndDay)
const endOfSummerCycle = new Date(new Date().getFullYear() + 1, SUMMER_END_MONTH, SUMMER_END_DAY)
const endOfWinterAndAllYearCycle = new Date(new Date().getFullYear() + 1, ALL_YEAR_END_MONTH, ALL_YEAR_END_DAY)

/**
* Fetch all return requirements that need return logs created.
*
* @param {boolean} isSummer - are we running summer cycel or all year
* @param {string} licenceReference - if provided only do the return log for that licence reference
*
* @returns {Promise<Array>} the list of return requirement ids
*/
async function go (isSummer, licenceReference) {
Expand All @@ -52,17 +55,49 @@ async function _createMetaData (isSummer, endDate, requirements) {
regionCode: requirements.returnVersion.licence.region.naldRegionId,
areaCode: requirements.returnVersion.licence.areacode,
formatId: requirements.legacyId,
periodStartDay: requirements.abstractionPeriodStartDay,
periodStartMonth: requirements.abstractionPeriodStartMonth,
periodEndDay: requirements.abstractionPeriodEndDay,
periodEndMonth: requirements.abstractionPeriodEndMonth
periodStartDay: requirements.abstractionPeriodStartDay.toString(),
periodStartMonth: requirements.abstractionPeriodStartMonth.toString(),
periodEndDay: requirements.abstractionPeriodEndDay.toString(),
periodEndMonth: requirements.abstractionPeriodEndMonth.toString()
},
points: requirements.returnRequirementPoints,
purposes: requirements.returnRequirementPurposes,
points: _createPointsMetaData(requirements.returnRequirementPoints),
purposes: _createPurposesMetaData(requirements.returnRequirementPurposes),
version: 1
}
}

function _createPointsMetaData (returnRequirementPoints) {
return returnRequirementPoints.map((returnRequirementPoint) => {
return {
name: returnRequirementPoint.description,
ngr1: returnRequirementPoint.ngr1,
ngr2: returnRequirementPoint.ngr2,
ngr3: returnRequirementPoint.ngr3,
ngr4: returnRequirementPoint.ngr4
}
})
}

function _createPurposesMetaData (returnRequirementPurposes) {
return returnRequirementPurposes.map((returnRequirementPurpose) => {
return {
alias: returnRequirementPurpose.alias,
primary: {
code: returnRequirementPurpose.primaryPurpose.legacyId,
description: returnRequirementPurpose.primaryPurpose.description
},
secondary: {
code: returnRequirementPurpose.secondaryPurpose.legacyId,
description: returnRequirementPurpose.secondaryPurpose.description
},
tertiary: {
code: returnRequirementPurpose.purpose.legacyId,
description: returnRequirementPurpose.purpose.description
}
}
})
}

function _createReturnLogId (requirements, startDate, endDate) {
const regionCode = requirements.returnVersion.licence.region.naldRegionId
const licenceReference = requirements.returnVersion.licence.licenceRef
Expand Down Expand Up @@ -115,7 +150,25 @@ async function _fetchReturnRequirements (isSummer, licenceReference) {
builder.select(['id', 'naldRegionId'])
})
.withGraphFetched('returnRequirementPoints')
.withGraphFetched('returnRequirementPurposes')
.modifyGraph('returnRequirementPoints', (builder) => {
builder.select(['description',
'ngr1',
'ngr2',
'ngr3',
'ngr4'])
})
.withGraphFetched('returnRequirementPurposes.primaryPurpose')
.modifyGraph('returnRequirementPurposes.primaryPurpose', (builder) => {
builder.select(['legacyId', 'description'])
})
.withGraphFetched('returnRequirementPurposes.secondaryPurpose')
.modifyGraph('returnRequirementPurposes.secondaryPurpose', (builder) => {
builder.select(['legacyId', 'description'])
})
.withGraphFetched('returnRequirementPurposes.purpose')
.modifyGraph('returnRequirementPurposes.purpose', (builder) => {
builder.select(['legacyId', 'description'])
})

return results
}
Expand Down Expand Up @@ -153,8 +206,8 @@ async function _generateReturnLogPayload (isSummer, requirementsForReturns) {

function _getCycleDueDate (isSummer) {
return isSummer
? _formatDate(new Date(new Date().getFullYear() + 1, summerDueDateMonth, summerDueDateDay))
: _formatDate(new Date(new Date().getFullYear() + 1, allYearDueDateMonth, allYearDueDateDay))
? _formatDate(new Date(new Date().getFullYear() + 1, SUMMER_DUE_DATE_MONTH, SUMMER_DUE_DATE_DAY))
: _formatDate(new Date(new Date().getFullYear() + 1, ALL_YEAR_DUE_DATE_MONTH, ALL_YEAR_DUE_DATE_DAY))
}

function _getCycleEndDate (isSummer, returnVersion) {
Expand Down Expand Up @@ -191,8 +244,8 @@ function _getCycleEndDate (isSummer, returnVersion) {

function _getCycleStartDate (isSummer) {
return isSummer
? _formatDate(new Date(new Date().getFullYear(), summerStartMonth, summerStartDay))
: _formatDate(new Date(new Date().getFullYear(), allYearStartMonth, allYearStartDay))
? _formatDate(new Date(new Date().getFullYear(), SUMMER_START_MONTH, SUMMER_START_DAY))
: _formatDate(new Date(new Date().getFullYear(), ALL_YEAR_START_MONTH, ALL_YEAR_START_DAY))
}

function _isFinal (endDateString, isSummer) {
Expand Down
Loading

0 comments on commit 529f77c

Please sign in to comment.