Skip to content
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

Fix ordering of points and purposes in ret. req. #1135

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ async function _fetch (licenceId) {
.innerJoin('licenceVersions', 'licenceVersionPurposes.licenceVersionId', 'licenceVersions.id')
.where('licenceVersions.licenceId', licenceId)
.where('licenceVersions.status', 'current')
.orderBy([
{ column: 'purposes.description', order: 'asc' }
])
}

module.exports = {
Expand Down
12 changes: 11 additions & 1 deletion app/services/return-requirements/fetch-points.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ async function _fetchPoints (licenceId) {
function _abstractPointsData (result) {
const pointsData = []

// First extract from the current_version's purposes the various points
result.purposes.forEach((purpose) => {
purpose.purposePoints.forEach((point) => {
const pointDetail = point.point_detail
pointsData.push(pointDetail)
})
})

return pointsData
// Then sort the extracted points to give us a consistent display order and return
return pointsData.sort((first, second) => {
// NOTE: This ensures we don't call localeCompare with null or undefined values
const firstLocalName = first.LOCAL_NAME ? first.LOCAL_NAME : ''
const secondLocalName = second.LOCAL_NAME ? second.LOCAL_NAME : ''

// NOTE: localeCompare() handles dealing with values in different cases automatically! So we don't have to lowercase
// everything before then comparing.
return firstLocalName.localeCompare(secondLocalName)
})
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe('Return Requirements - Fetch Licence Purposes service', () => {

// Create 3 descriptions for the purposes
purposes = await Promise.all([
await PurposeHelper.add({ id: '8290bb6a-4265-4cc8-b9bb-37cde1357d5d', description: 'Large Garden Watering' }),
await PurposeHelper.add({ id: '14794d57-1acf-4c91-8b48-4b1ec68bfd6f', description: 'Heat Pump' }),
await PurposeHelper.add({ id: '49088608-ee9f-491a-8070-6831240945ac', description: 'Horticultural Watering' }),
await PurposeHelper.add({ id: '8290bb6a-4265-4cc8-b9bb-37cde1357d5d', description: 'Large Garden Watering' })
await PurposeHelper.add({ id: '49088608-ee9f-491a-8070-6831240945ac', description: 'Horticultural Watering' })
])

// Create the licenceVersionPurposes with the purposes and licenceVersion
Expand All @@ -46,16 +46,18 @@ describe('Return Requirements - Fetch Licence Purposes service', () => {
it('fetches the data', async () => {
const result = await FetchLicencePurposesService.go(licenceVersion.licenceId)

expect(result).to.equal([{
expect(result[0]).to.equal({
id: '14794d57-1acf-4c91-8b48-4b1ec68bfd6f',
description: 'Heat Pump'
}, {
})
expect(result[1]).to.equal({
id: '49088608-ee9f-491a-8070-6831240945ac',
description: 'Horticultural Watering'
}, {
})
expect(result[2]).to.equal({
id: '8290bb6a-4265-4cc8-b9bb-37cde1357d5d',
description: 'Large Garden Watering'
}])
})
})
})

Expand Down
42 changes: 38 additions & 4 deletions test/services/return-requirements/fetch-points.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ describe('Return Requirements - Return requirements Fetch Points service', () =>
NGR4_NORTH: 'null',
NGR4_SHEET: 'null'
}
}]
},
{
point_detail: {
NGR1_EAST: '6520',
NGR2_EAST: 'null',
NGR3_EAST: 'null',
NGR4_EAST: 'null',
LOCAL_NAME: 'POINT A, ADDINGTON SANDPITS',
NGR1_NORTH: '5937',
NGR1_SHEET: 'TQ',
NGR2_NORTH: 'null',
NGR2_SHEET: 'null',
NGR3_NORTH: 'null',
NGR3_SHEET: 'null',
NGR4_NORTH: 'null',
NGR4_SHEET: 'null'
}
}
]
}]
}
}
Expand All @@ -63,9 +81,25 @@ describe('Return Requirements - Return requirements Fetch Points service', () =>

describe('when called', () => {
it('returns result', async () => {
const result = await FetchPointsService.go(licence.id)
const results = await FetchPointsService.go(licence.id)

expect(results[0]).to.equal({
NGR1_EAST: '6520',
NGR2_EAST: 'null',
NGR3_EAST: 'null',
NGR4_EAST: 'null',
LOCAL_NAME: 'POINT A, ADDINGTON SANDPITS',
NGR1_NORTH: '5937',
NGR1_SHEET: 'TQ',
NGR2_NORTH: 'null',
NGR2_SHEET: 'null',
NGR3_NORTH: 'null',
NGR3_SHEET: 'null',
NGR4_NORTH: 'null',
NGR4_SHEET: 'null'
})

expect(result).to.equal([{
expect(results[1]).to.equal({
NGR1_EAST: '69212',
NGR2_EAST: 'null',
NGR3_EAST: 'null',
Expand All @@ -79,7 +113,7 @@ describe('Return Requirements - Return requirements Fetch Points service', () =>
NGR3_SHEET: 'null',
NGR4_NORTH: 'null',
NGR4_SHEET: 'null'
}])
})
})
})
})
Loading