Skip to content

Commit

Permalink
Merge pull request #2037 from NMDSdevopsServiceAdm/testToLive
Browse files Browse the repository at this point in the history
Merge Test into Live ready for release 5.1
  • Loading branch information
aaron-russell authored Mar 11, 2020
2 parents 9f09dfe + b23471b commit 6ba4131
Show file tree
Hide file tree
Showing 7 changed files with 1,433 additions and 1,368 deletions.
18 changes: 8 additions & 10 deletions server/models/BulkImport/csv/establishments.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,6 @@ class Establishment {
},
attributes: ['id', 'locationId']
});
console.log(this._id);
console.log(locationExists);
let existingEstablishment = false;
await locationExists.map(async (establishment) => {
if (establishment.id === this._id) existingEstablishment = true;
Expand Down Expand Up @@ -1069,6 +1067,7 @@ class Establishment {

const localValidationErrors = [];
if (this._currentLine.SERVICEUSERS && this._currentLine.SERVICEUSERS.length > 0) {
// which is not valid
const isValid = this._currentLine.SERVICEUSERS.length ? listOfServiceUsers.every(thisService => !Number.isNaN(parseInt(thisService, 10))) : true;
if (!isValid) {
localValidationErrors.push({
Expand Down Expand Up @@ -1259,22 +1258,22 @@ class Establishment {
const myTotalPermTemp = parseInt(this._currentLine.TOTALPERMTEMP, 10);
const HEAD_OFFICE_MAIN_SERVICE = 72;

if (Number.isNaN(myTotalPermTemp)) {
if (myTotalPermTemp.length === 0) {
this._validationErrors.push({
lineNumber: this._lineNumber,
errCode: Establishment.TOTAL_PERM_TEMP_ERROR,
errType: 'TOTAL_PERM_TEMP_ERROR',
error: 'Total Permanent and Temporary (TOTALPERMTEMP) must be an whole number',
error: 'TOTALPERMTEMP is missing',
source: this._currentLine.PERMCQC,
name: this._currentLine.LOCALESTID
});
return false;
} else if (myTotalPermTemp < 0 || myTotalPermTemp > MAX_TOTAL) {
} else if (myTotalPermTemp < 0 || myTotalPermTemp > MAX_TOTAL || Number.isNaN(myTotalPermTemp)) {
this._validationErrors.push({
lineNumber: this._lineNumber,
errCode: Establishment.TOTAL_PERM_TEMP_ERROR,
errType: 'TOTAL_PERM_TEMP_ERROR',
error: `Total Permanent and Temporary (TOTALPERMTEMP) must be 0 or more, but less than ${MAX_TOTAL}`,
error: `TOTALPERMTEMP must be a number from 0 to ${MAX_TOTAL} if this is correct call support on 0113 241 0969`,
source: myTotalPermTemp,
name: this._currentLine.LOCALESTID
});
Expand Down Expand Up @@ -1486,7 +1485,6 @@ class Establishment {
};

if(isCQCRegulated && !hasRegisteredManagerVacancy() && registeredManager === 0) {
console.log('They have no RMs');
csvEstablishmentSchemaErrors.unshift(Object.assign(template, {
error: 'You do not have a staff record for a Registered Manager therefore must record a vacancy for one'
}));
Expand Down Expand Up @@ -1764,9 +1762,9 @@ class Establishment {
} else {
this._validationErrors.push({
lineNumber: this._lineNumber,
errCode: Establishment.SERVICE_USERS_ERROR,
errType: 'SERVICE_USERS_ERROR',
error: `Service Users (SERVICEUSERS): ${thisService} is unknown`,
warnCode: Establishment.SERVICE_USERS_ERROR,
warnType: 'SERVICE_USERS_ERROR',
warning: `Entry for code ${thisService} in SERVICEUSERS will be ignored as this is invalid`,
source: this._currentLine.SERVICEUSERS,
name: this._currentLine.LOCALESTID
});
Expand Down
54 changes: 50 additions & 4 deletions server/models/BulkImport/csv/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,23 @@ class Worker {
}
}

async _crossValidateMainJobRole(csvWorkerSchemaErrors, cqcRegEstablishment) {
const template = {
worker: this._currentLine.UNIQUEWORKERID,
name: this._currentLine.LOCALESTID,
lineNumber: this._lineNumber,
errCode: Worker.MAIN_JOB_ROLE_ERROR,
errType: 'MAIN_JOB_ROLE_ERROR',
source: this._currentLine.MAINJOBROLE
};

if (!cqcRegEstablishment && this.mainJobRoleId === 4) {
csvWorkerSchemaErrors.unshift(Object.assign(template, {
error: 'Workers MAINJOBROLE is Registered Manager but you are not providing a CQC regulated service. Please change to another Job Role'
}));
}
}

_validateMainJobDesc () {
const myMainJobDesc = this._currentLine.MAINJRDESC;
const MAX_LENGTH = 120;
Expand Down Expand Up @@ -2489,27 +2506,27 @@ class Worker {
}

// add a duplicate validation error to the current set
addDuplicate (originalLineNumber) {
addDuplicate (UNIQUEWORKERID) {
return {
origin: 'Workers',
lineNumber: this._lineNumber,
errCode: Worker.DUPLICATE_ERROR,
errType: 'DUPLICATE_ERROR',
error: 'UNIQUEWORKERID is not unique',
error: `UNIQUEWORKERID ${UNIQUEWORKERID} is not unique`,
source: this._currentLine.UNIQUEWORKERID,
worker: this._currentLine.UNIQUEWORKERID,
name: this._currentLine.LOCALESTID
};
}

// add a duplicate validation error to the current set
addChgDuplicate (originalLineNumber) {
addChgDuplicate (CHGUNIQUEWORKERID) {
return {
origin: 'Workers',
lineNumber: this._lineNumber,
errCode: Worker.DUPLICATE_ERROR,
errType: 'DUPLICATE_ERROR',
error: 'CHGUNIQUEWORKERID is not unique',
error: `CHGUNIQUEWORKERID ${CHGUNIQUEWORKERID} is not unique`,
source: this._currentLine.UNIQUEWORKERID,
worker: this._currentLine.UNIQUEWORKERID,
name: this._currentLine.LOCALESTID
Expand Down Expand Up @@ -2611,6 +2628,35 @@ class Worker {
return true;
}

async crossValidate ({
csvWorkerSchemaErrors,
myEstablishments
}) {
// if worker isn't being added or updated then exit early
if (!(['NEW', 'UPDATE'].includes(this._status))) {
return;
}
let cqcRegEstablishment = false;

myEstablishments.forEach(establishment => {
if (this.establishmentKey === establishment.key) {
switch (establishment.status) {
case 'NEW':
case 'UPDATE': {
cqcRegEstablishment = establishment.regType === 2;
}
/* fall through */

case 'DELETE':
break;
}
}
});

// ensure worker jobs tally up on TOTALPERMTEMP field, but only do it for new or updated establishments
this._crossValidateMainJobRole(csvWorkerSchemaErrors, cqcRegEstablishment);
}

// returns true on success, false is any attribute of Worker fails
validate () {
let status = true;
Expand Down
21 changes: 15 additions & 6 deletions server/models/classes/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,26 @@ class Worker extends EntityValidator {
this._status = document.status;
}


// Consequential updates when one value means another should be empty or null

// If their job isn't a registered nurse, remove their specialism and category
if (document.mainJob) {
if (document.mainJob.jobId !== 23) {
if (document.mainJob || document.otherJobs) {
let otherRegNurse = false;
let otherSocialWorker = false;
const mainJob = document.mainJob ? document.mainJob : this.mainJob;
const otherJobs = document.otherJobs ? document.otherJobs : this.otherJobs;
if (otherJobs && otherJobs.jobs) {
otherJobs.jobs.map(otherJob => {
if (otherJob.jobId === 23) otherRegNurse = true;
if (otherJob.jobId === 27) otherSocialWorker = true;
});
}
if (mainJob && mainJob.jobId !== 23 && !otherRegNurse) {
document.registeredNurse = null;
document.nurseSpecialism = { id: null, specialism: null };
}
// If their job isn't a social worker - remove the approved mental health worker
if (document.mainJob.jobId !== 27) {
if (mainJob && mainJob.jobId !== 27 && !otherSocialWorker) {
document.approvedMentalHealthWorker = null;
}
}
Expand All @@ -333,9 +342,9 @@ class Worker extends EntityValidator {
document.yearArrived = { value: null, year: null };
}
}

const notContract = ['Agency', 'Pool/Bank']
// Remove contracted hours If on a zero hour contract
if (document.zeroHoursContract === 'Yes' || document.contract === 'Agency') {
if (document.zeroHoursContract === 'Yes' || notContract.includes(document.contract)) {
document.weeklyHoursContracted = { value: null, hours: null };
}

Expand Down
14 changes: 11 additions & 3 deletions server/routes/establishments/bulkUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ const validateEstablishmentCsv = async (
await lineValidator.validate();
if (!lineValidator._ignore) {
lineValidator.transform();

const thisEstablishmentAsAPI = lineValidator.toAPI();

try {
Expand Down Expand Up @@ -1120,15 +1120,15 @@ const validateBulkUploadFiles = async (

if (allWorkersByKey[keyNoWhitespace]) {
// this worker is a duplicate
csvWorkerSchemaErrors.push(thisWorker.addDuplicate(allWorkersByKey[keyNoWhitespace]));
csvWorkerSchemaErrors.push(thisWorker.addDuplicate(thisWorker.uniqueWorker));

// remove the entity
delete myAPIWorkers[thisWorker.lineNumber];

// the worker will be known by LOCALSTID and UNIQUEWORKERID, but if CHGUNIQUEWORKERID is given, then it's combination of LOCALESTID and CHGUNIQUEWORKERID must be unique
} else if (changeKeyNoWhitespace && allWorkersByKey[changeKeyNoWhitespace]) {
// this worker is a duplicate
csvWorkerSchemaErrors.push(thisWorker.addChgDuplicate(allWorkersByKey[keyNoWhitespace]));
csvWorkerSchemaErrors.push(thisWorker.addChgDuplicate(thisWorker.changeUniqueWorker));

// remove the entity
delete myAPIWorkers[thisWorker.lineNumber];
Expand Down Expand Up @@ -1321,6 +1321,14 @@ const validateBulkUploadFiles = async (
});
}));

// Run validations that require information about establishments
await Promise.all(myWorkers.map(async worker => {
await worker.crossValidate({
csvWorkerSchemaErrors,
myEstablishments
});
}));

// /////////////////////////
// Prepare validation results

Expand Down
Loading

0 comments on commit 6ba4131

Please sign in to comment.