diff --git a/app/services/jobs/return-logs/fetch-return-logs.service.js b/app/services/jobs/return-logs/fetch-return-logs.service.js index 282b22e0d6..592c2fe2dd 100644 --- a/app/services/jobs/return-logs/fetch-return-logs.service.js +++ b/app/services/jobs/return-logs/fetch-return-logs.service.js @@ -2,7 +2,7 @@ /** * Fetches data needed for the generating return logs - * @module FetchLicenceWithoutReturnsService + * @module FetchReturnLogsService */ const ReturnLogModel = require('../../../models/return-log.model.js') @@ -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} the list of return requirement ids */ async function go (isSummer, licenceReference) { @@ -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 @@ -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 } @@ -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) { @@ -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) { diff --git a/test/services/jobs/return-logs/fetch-return-logs.service.test.js b/test/services/jobs/return-logs/fetch-return-logs.service.test.js index a25f8599d9..1e6612e4ad 100644 --- a/test/services/jobs/return-logs/fetch-return-logs.service.test.js +++ b/test/services/jobs/return-logs/fetch-return-logs.service.test.js @@ -9,12 +9,14 @@ const { expect } = Code // Test helpers const LicenceHelper = require('../../../support/helpers/licence.helper.js') +const PrimaryPurposeHelper = require('../../../support/helpers/primary-purpose.helper.js') +const PurposeHelper = require('../../../support/helpers/purpose.helper.js') const RegionHelper = require('../../../support/helpers/region.helper.js') -const ReturnLogHelper = require('../../../support/helpers/return-log.helper.js') const ReturnRequirementHelper = require('../../../support/helpers/return-requirement.helper.js') const ReturnRequirementPointHelper = require('../../../support/helpers/return-requirement-point.helper.js') const ReturnRequirementPurposeHelper = require('../../../support/helpers/return-requirement-purpose.helper.js') const ReturnVersionHelper = require('../../../support/helpers/return-version.helper.js') +const SecondaryPurposeHelper = require('../../../support/helpers/secondary-purpose.helper.js') // Thing under test const FetchReturnLogsService = require('../../../../app/services/jobs/return-logs/fetch-return-logs.service.js') @@ -28,6 +30,10 @@ describe('Fetch return logs service', () => { const summerStartDate = new Date(new Date().getFullYear(), 10, 1).toISOString().split('T')[0] describe('When isSummer is false, one return requirement and a licenceRef provided', () => { + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() + let licence let region let returnVersion @@ -41,7 +47,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -63,13 +74,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -80,6 +111,10 @@ describe('Fetch return logs service', () => { }) describe('when isSummer is true, one return requirement and a licenceRef provided', () => { + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() + let licence let region let returnVersion @@ -93,7 +128,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ summer: true, returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -115,13 +155,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -132,6 +192,13 @@ describe('Fetch return logs service', () => { }) describe('when isSummer is false, two return requirements and a licenceRef provided', () => { + const primaryPurpose = PrimaryPurposeHelper.select() + const primaryPurpose2 = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const purpose2 = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() + const secondaryPurpose2 = SecondaryPurposeHelper.select() + let licence let region let returnVersion @@ -149,10 +216,18 @@ describe('Fetch return logs service', () => { returnRequirement = await ReturnRequirementHelper.add({ returnVersionId: returnVersion.id }) returnRequirement2 = await ReturnRequirementHelper.add({ returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) returnRequirementPoint2 = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement2.id }) returnRequirementPurpose2 = await ReturnRequirementPurposeHelper.add({ - returnRequirementId: returnRequirement2.id + primaryPurposeId: primaryPurpose2.id, + purposeId: purpose2.id, + returnRequirementId: returnRequirement2.id, + secondaryPurposeId: secondaryPurpose2.id }) }) @@ -175,13 +250,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -203,13 +298,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement2.legacyId, - periodStartDay: returnRequirement2.abstractionPeriodStartDay, - periodStartMonth: returnRequirement2.abstractionPeriodStartMonth, - periodEndDay: returnRequirement2.abstractionPeriodEndDay, - periodEndMonth: returnRequirement2.abstractionPeriodEndMonth + periodStartDay: returnRequirement2.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement2.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement2.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement2.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint2], - purposes: [returnRequirementPurpose2], + points: [{ + name: returnRequirementPoint2.description, + ngr1: returnRequirementPoint2.ngr1, + ngr2: returnRequirementPoint2.ngr2, + ngr3: returnRequirementPoint2.ngr3, + ngr4: returnRequirementPoint2.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose2.alias, + primary: { + code: primaryPurpose2.legacyId, + description: primaryPurpose2.description + }, + secondary: { + code: secondaryPurpose2.legacyId, + description: secondaryPurpose2.description + }, + tertiary: { + code: purpose2.legacyId, + description: purpose2.description + } + }], version: 1 }) expect(result[1].returnsFrequency).to.equal('day') @@ -220,6 +335,13 @@ describe('Fetch return logs service', () => { }) describe('when isSummer is true, two return requirements and a licenceRef provided', () => { + const primaryPurpose = PrimaryPurposeHelper.select() + const primaryPurpose2 = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const purpose2 = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() + const secondaryPurpose2 = SecondaryPurposeHelper.select() + let licence let region let returnVersion @@ -237,37 +359,18 @@ describe('Fetch return logs service', () => { returnRequirement = await ReturnRequirementHelper.add({ summer: true, returnVersionId: returnVersion.id }) returnRequirement2 = await ReturnRequirementHelper.add({ summer: true, returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) returnRequirementPoint2 = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement2.id }) returnRequirementPurpose2 = await ReturnRequirementPurposeHelper.add({ - returnRequirementId: returnRequirement2.id - }) - await ReturnLogHelper.add({ - dueDate: summerDueDate, - endDate: summerEndDate, - licenceRef: licence.licenceRef, - metadata: { - description: 'BOREHOLE AT AVALON', - isCurrent: true, - isFinal: false, - isSummer: false, - isTwoPartTariff: false, - isUpload: false, - nald: { - regionCode: region.naldRegionId, - areaCode: licence.regions.historicalAreaCode, - formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth - }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], - version: 1 - }, - returnReference: returnRequirement.legacyId, - startDate: summerStartDate + primaryPurposeId: primaryPurpose2.id, + purposeId: purpose2.id, + returnRequirementId: returnRequirement2.id, + secondaryPurposeId: secondaryPurpose2.id }) }) @@ -290,13 +393,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -318,13 +441,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement2.legacyId, - periodStartDay: returnRequirement2.abstractionPeriodStartDay, - periodStartMonth: returnRequirement2.abstractionPeriodStartMonth, - periodEndDay: returnRequirement2.abstractionPeriodEndDay, - periodEndMonth: returnRequirement2.abstractionPeriodEndMonth + periodStartDay: returnRequirement2.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement2.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement2.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement2.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint2], - purposes: [returnRequirementPurpose2], + points: [{ + name: returnRequirementPoint2.description, + ngr1: returnRequirementPoint2.ngr1, + ngr2: returnRequirementPoint2.ngr2, + ngr3: returnRequirementPoint2.ngr3, + ngr4: returnRequirementPoint2.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose2.alias, + primary: { + code: primaryPurpose2.legacyId, + description: primaryPurpose2.description + }, + secondary: { + code: secondaryPurpose2.legacyId, + description: secondaryPurpose2.description + }, + tertiary: { + code: purpose2.legacyId, + description: purpose2.description + } + }], version: 1 }) expect(result[1].returnsFrequency).to.equal('day') @@ -336,6 +479,9 @@ describe('Fetch return logs service', () => { describe('when isSummer is false, there is an expired date, one return requirement and a licenceRef provided', () => { const expiredDate = new Date(new Date().getFullYear() + 1, 1, 31).toISOString().split('T')[0] + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() let licence let region @@ -350,7 +496,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -372,13 +523,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -390,6 +561,9 @@ describe('Fetch return logs service', () => { describe('when isSummer is false, there is an expired date after the end of the cycle, one return requirement and a licenceRef provided', () => { const expiredDate = new Date(new Date().getFullYear() + 1, 3, 31).toISOString().split('T')[0] + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() let licence let region @@ -404,7 +578,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -426,13 +605,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -444,6 +643,9 @@ describe('Fetch return logs service', () => { describe('when isSummer is true, there is a lapsed date, one return requirement and a licenceRef provided', () => { const lapsedDate = new Date(new Date().getFullYear() + 1, 8, 31).toISOString().split('T')[0] + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() let licence let region @@ -458,7 +660,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ summer: true, returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -480,13 +687,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day') @@ -498,6 +725,9 @@ describe('Fetch return logs service', () => { describe('when isSummer is true, there is a revoked date that is after the cycle, one return requirement and a licenceRef provided', () => { const revokedDate = new Date(new Date().getFullYear() + 1, 10, 31).toISOString().split('T')[0] + const primaryPurpose = PrimaryPurposeHelper.select() + const purpose = PurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() let licence let region @@ -512,7 +742,12 @@ describe('Fetch return logs service', () => { returnVersion = await ReturnVersionHelper.add({ licenceId: licence.id }) returnRequirement = await ReturnRequirementHelper.add({ summer: true, returnVersionId: returnVersion.id }) returnRequirementPoint = await ReturnRequirementPointHelper.add({ returnRequirementId: returnRequirement.id }) - returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ returnRequirementId: returnRequirement.id }) + returnRequirementPurpose = await ReturnRequirementPurposeHelper.add({ + primaryPurposeId: primaryPurpose.id, + purposeId: purpose.id, + returnRequirementId: returnRequirement.id, + secondaryPurposeId: secondaryPurpose.id + }) }) it('should return one return log payload', async () => { @@ -534,13 +769,33 @@ describe('Fetch return logs service', () => { regionCode: region.naldRegionId, areaCode: licence.regions.historicalAreaCode, formatId: returnRequirement.legacyId, - periodStartDay: returnRequirement.abstractionPeriodStartDay, - periodStartMonth: returnRequirement.abstractionPeriodStartMonth, - periodEndDay: returnRequirement.abstractionPeriodEndDay, - periodEndMonth: returnRequirement.abstractionPeriodEndMonth + periodStartDay: returnRequirement.abstractionPeriodStartDay.toString(), + periodStartMonth: returnRequirement.abstractionPeriodStartMonth.toString(), + periodEndDay: returnRequirement.abstractionPeriodEndDay.toString(), + periodEndMonth: returnRequirement.abstractionPeriodEndMonth.toString() }, - points: [returnRequirementPoint], - purposes: [returnRequirementPurpose], + points: [{ + name: returnRequirementPoint.description, + ngr1: returnRequirementPoint.ngr1, + ngr2: returnRequirementPoint.ngr2, + ngr3: returnRequirementPoint.ngr3, + ngr4: returnRequirementPoint.ngr4 + }], + purposes: [{ + alias: returnRequirementPurpose.alias, + primary: { + code: primaryPurpose.legacyId, + description: primaryPurpose.description + }, + secondary: { + code: secondaryPurpose.legacyId, + description: secondaryPurpose.description + }, + tertiary: { + code: purpose.legacyId, + description: purpose.description + } + }], version: 1 }) expect(result[0].returnsFrequency).to.equal('day')