From c808681409bed4f052a8a1e16f5e2269b478a7cb Mon Sep 17 00:00:00 2001 From: Warren Ayling Date: Fri, 20 Sep 2019 13:43:37 +0100 Subject: [PATCH 1/2] Handling non-associated worker and workewr with no DOB on training cross-validation. Handling workerKeyNoWhitespace when localStIf and/or uniqueWorkerId is null. --- server/routes/establishments/bulkUpload.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server/routes/establishments/bulkUpload.js b/server/routes/establishments/bulkUpload.js index 7b93bb692e..9308779ef9 100644 --- a/server/routes/establishments/bulkUpload.js +++ b/server/routes/establishments/bulkUpload.js @@ -375,7 +375,6 @@ router.route('/uploaded').put(async (req, res) => { if(importedTraining){ const trainingCsvValidator = new CsvTrainingValidator(importedTraining[firstRow], firstLineNumber); if(trainingCsvValidator.preValidate(trainingHeaders)){ - console.log("WA DEBUG - prevalidated training - ", importedTraining) // count records and update metadata trainingMetadata.records = importedTraining.length; metadataS3Promises.push(uploadAsJSON(username, establishmentId, trainingMetadata, `${establishmentId}/latest/${trainingMetadata.filename}.metadata.json`)); @@ -1051,7 +1050,7 @@ const validateBulkUploadFiles = async (commit, username , establishmentId, isPar myTrainings.forEach(thisTraingRecord => { const establishmentKeyNoWhitespace = (thisTraingRecord.localeStId || '').replace(/\s/g, ""); - const workerKeyNoWhitespace = (thisTraingRecord.localeStId + thisTraingRecord.uniqueWorkerId).replace(/\s/g, ""); + const workerKeyNoWhitespace = ((thisTraingRecord.localeStId || '') + (thisTraingRecord.uniqueWorkerId || '')).replace(/\s/g, ""); if (!allEstablishmentsByKey[establishmentKeyNoWhitespace]) { // not found the associated establishment @@ -1072,10 +1071,14 @@ const validateBulkUploadFiles = async (commit, username , establishmentId, isPar const foundWorkerByLineNumber = allWorkersByKey[workerKeyNoWhitespace]; const knownWorker = foundWorkerByLineNumber ? myAPIWorkers[foundWorkerByLineNumber] : null; + // training cross-validation against worker's date of birth (DOB) can only be applied, if: + // 1. the associated Worker can be matched + // 2. the worker has DOB defined (it's not a mandatory property) const trainingCompletedDate = moment.utc(thisTraingRecord._currentLine.DATECOMPLETED, "DD-MM-YYYY") - const workerDob = moment.utc(workersKeyed[workerKeyNoWhitespace].DOB, "DD-MM-YYYY") + const foundAssociatedWorker = workersKeyed[workerKeyNoWhitespace]; + const workerDob = foundAssociatedWorker && foundAssociatedWorker.DOB ? moment.utc(workersKeyed[workerKeyNoWhitespace].DOB, "DD-MM-YYYY") : null; - if (workerDob.isValid() && trainingCompletedDate.diff(workerDob, 'years') < 14 ) { + if (workerDob && workerDob.isValid() && trainingCompletedDate.diff(workerDob, 'years') < 14 ) { csvTrainingSchemaErrors.push(thisTraingRecord.dobTrainingMismatch()); } From a08d6db8145362d6552298c133cc7400b7d36a8c Mon Sep 17 00:00:00 2001 From: Paul Humphreys Date: Fri, 20 Sep 2019 15:05:33 +0100 Subject: [PATCH 2/2] Ensure numberOfStaff is numeric before doing validation (#1482) Ensure numberOfStaff is numeric before range checks --- .../establishment/properties/staffProperty.js | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/server/models/classes/establishment/properties/staffProperty.js b/server/models/classes/establishment/properties/staffProperty.js index b89132d745..d995144aa6 100644 --- a/server/models/classes/establishment/properties/staffProperty.js +++ b/server/models/classes/establishment/properties/staffProperty.js @@ -12,18 +12,23 @@ exports.StaffProperty = class StaffProperty extends ChangePropertyPrototype { // concrete implementations async restoreFromJson(document) { - if (document.numberOfStaff !== null) { - const givenStaff = isNaN(parseInt(document.numberOfStaff, 10)) ? null : parseInt(document.numberOfStaff, 10); - const MAX_STAFF=999; - const MIN_STAFF=0; - if (givenStaff !== null && - givenStaff >= MIN_STAFF && - givenStaff <= MAX_STAFF) { - this.property = givenStaff; - } else { - this.property = null; - } + if (/^-?[0-9]+$/.test(String(document.numberOfStaff))) { + const givenStaff = parseInt(document.numberOfStaff, 10); + const MAX_STAFF = 999; + const MIN_STAFF = 0; + + //if document.numberOfStaff is numeric it must be in range + if (givenStaff >= MIN_STAFF && givenStaff <= MAX_STAFF) { + this.property = givenStaff; + } + else { + this.property = null; } + } + else if(document.numberOfStaff) { + //any other non falsy value fails validation + this.property = null; + } } restorePropertyFromSequelize(document) {