From c28c23c8b47d4bcfcc44baad19db501cbe8a5c4f Mon Sep 17 00:00:00 2001 From: Nathan Brown Date: Fri, 25 Oct 2024 13:02:22 +0100 Subject: [PATCH] feat(cb2-1234): add axle sorting (#33) --- .gitignore | 3 ++ .vscode/launch.json | 61 --------------------- .vscode/settings.json | 14 ----- src/models/ministryPlate.ts | 17 +++--- tests/unit/ministryPlate.test.ts | 92 +++++++++++++++++++++++++++++++- 5 files changed, 102 insertions(+), 85 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 1e63a3f..8d2fed5 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ npm-audit.html # idea specific hidden files .idea/** *.iml + +# vscode +.vscode/** diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 3d07127..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Launch Serverless Offline", - "program": "${workspaceRoot}/node_modules/serverless/bin/serverless", - "args": [ - "offline", - "start" - ], - "env": { - "NODE_ENV": "local", - "SERVICE": "cvs-svc-template", - "API_VERSION": "1.0.0" - }, - "windows": { - "program": "${workspaceRoot}\\node_modules\\serverless\\bin\\serverless" - } - }, - { - "name": "Jest Debug all tests", - "type": "node", - "request": "launch", - "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest", - "cwd": "${workspaceRoot}", - "console": "integratedTerminal", - "protocol": "inspector", - "windows": { - "program": "${workspaceRoot}\\node_modules\\.bin\\jest" - }, - "env": { - "NODE_ENV": "test", - "SERVICE": "cvs-svc-template", - "API_VERSION": "1.0.0" - // Here we set some environment vars that should be set locally. - // They can and will overwrite the ones coming from your serverless.yml - // Please refer to the relevant .env files - test for example... - } - }, - { - "name": "Jest Debug opened file", - "type": "node", - "request": "launch", - "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest", - "args": ["${fileBasenameNoExtension}"], - "cwd": "${workspaceRoot}", - "protocol": "inspector", - "console": "integratedTerminal", - "internalConsoleOptions": "neverOpen", - "env": { - "NODE_ENV": "test", - "SERVICE": "cvs-svc-template", - "API_VERSION": "1.0.0" - // Here we set some environment vars that should be set locally. - // They can and will overwrite the ones coming from your serverless.yml - // Please refer to the relevant .env files - test for example... - } - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index f471eb8..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "eslint.enable": true, - "editor.defaultFormatter": "esbenp.prettier-vscode", - "eslint.packageManager": "npm", - "editor.codeActionsOnSave": { - "source.fixAll.eslint": true - }, - "[javascript]": { - "editor.formatOnSave": true - }, - "[typescript]": { - "editor.formatOnSave": true - } -} diff --git a/src/models/ministryPlate.ts b/src/models/ministryPlate.ts index 577ae32..90a7511 100644 --- a/src/models/ministryPlate.ts +++ b/src/models/ministryPlate.ts @@ -118,8 +118,8 @@ export class MinistryPlateDocument extends DocumentModel { tyreUseCode: techRecord.techRecord_tyreUseCode, axles: this.populateAxles( techRecord.techRecord_vehicleType === 'hgv' - ? (techRecord.techRecord_axles as HGVAxles[] ?? []) - : (techRecord.techRecord_axles as TRLAxles[] ?? []), + ? (techRecord.techRecord_axles as HGVAxles[]) ?? [] + : (techRecord.techRecord_axles as TRLAxles[]) ?? [], generateTrlEec, ), } @@ -154,17 +154,18 @@ export class MinistryPlateDocument extends DocumentModel { axle4: {}, } as Axles; const terminatingCondition = Math.min(axles.length, 4); + const sortedAxles = axles.sort((a, b) => a.axleNumber - b.axleNumber); for (let i = 0; i < terminatingCondition; i++) { plateAxles[`axle${i + 1}`] = { weights: { - gbWeight: axles[i].weights_gbWeight?.toString(), - eecWeight: generateTrlEec ? axles[i].weights_eecWeight?.toString() : null, - designWeight: axles[i].weights_designWeight?.toString(), + gbWeight: sortedAxles[i].weights_gbWeight?.toString(), + eecWeight: generateTrlEec ? sortedAxles[i].weights_eecWeight?.toString() : null, + designWeight: sortedAxles[i].weights_designWeight?.toString(), }, tyres: { - tyreSize: axles[i].tyres_tyreSize, - plyRating: axles[i].tyres_dataTrAxles ?? axles[i].tyres_plyRating, - fitmentCode: axles[i].tyres_fitmentCode, + tyreSize: sortedAxles[i].tyres_tyreSize, + plyRating: sortedAxles[i].tyres_dataTrAxles ?? sortedAxles[i].tyres_plyRating, + fitmentCode: sortedAxles[i].tyres_fitmentCode, }, }; } diff --git a/tests/unit/ministryPlate.test.ts b/tests/unit/ministryPlate.test.ts index 7ebf8d4..a9ca878 100644 --- a/tests/unit/ministryPlate.test.ts +++ b/tests/unit/ministryPlate.test.ts @@ -59,7 +59,7 @@ describe('Document Model tests', () => { tyres_tyreSize: '1', tyres_plyRating: '2', tyres_fitmentCode: '3', - weights_gbWeight: 123, + weights_gbWeight: 321, weights_eecWeight: 123, weights_designWeight: 123, }, @@ -73,7 +73,95 @@ describe('Document Model tests', () => { }, ] as unknown as HGVAxles[]; const document = new MinistryPlateDocument(request); - expect(document.PLATES_DATA.axles.axle4.weights.gbWeight).toBe('123'); + expect(document.PLATES_DATA.axles.axle4.weights.gbWeight).toBe('321'); + }); + + it('should order axles by axle number', () => { + (request.techRecord as TechRecordType<'hgv', 'get'>).techRecord_axles = [ + { + axleNumber: 4, + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 4, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + axleNumber: 2, + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 2, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + axleNumber: 1, + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 1, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + axleNumber: 3, + tyres_tyreSize: '9', + tyres_plyRating: '9', + tyres_fitmentCode: '9', + weights_gbWeight: 3, + weights_eecWeight: 999, + weights_designWeight: 999, + }, + ] as unknown as HGVAxles[]; + const document = new MinistryPlateDocument(request); + expect(document.PLATES_DATA.axles.axle4.weights.gbWeight).toBe('4'); + expect(document.PLATES_DATA.axles.axle3.weights.gbWeight).toBe('3'); + expect(document.PLATES_DATA.axles.axle2.weights.gbWeight).toBe('2'); + expect(document.PLATES_DATA.axles.axle1.weights.gbWeight).toBe('1'); + }); + + it('should remain order if no axle number and not fail', () => { + (request.techRecord as TechRecordType<'hgv', 'get'>).techRecord_axles = [ + { + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 1, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 2, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + tyres_tyreSize: '1', + tyres_plyRating: '2', + tyres_fitmentCode: '3', + weights_gbWeight: 3, + weights_eecWeight: 123, + weights_designWeight: 123, + }, + { + tyres_tyreSize: '9', + tyres_plyRating: '9', + tyres_fitmentCode: '9', + weights_gbWeight: 4, + weights_eecWeight: 999, + weights_designWeight: 999, + }, + ] as unknown as HGVAxles[]; + const document = new MinistryPlateDocument(request); + expect(document.PLATES_DATA.axles.axle4.weights.gbWeight).toBe('4'); + expect(document.PLATES_DATA.axles.axle3.weights.gbWeight).toBe('3'); + expect(document.PLATES_DATA.axles.axle2.weights.gbWeight).toBe('2'); + expect(document.PLATES_DATA.axles.axle1.weights.gbWeight).toBe('1'); }); it('should not fail if no axles present', () => {