Skip to content

Commit

Permalink
refactor the logic around adding errors to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kapppa-joe committed Nov 11, 2024
1 parent 55f05f6 commit b1a5fe1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
69 changes: 14 additions & 55 deletions backend/server/models/BulkImport/csv/crossValidate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const { chain } = require('lodash');
const models = require('../../../models');

const MAIN_JOB_ROLE_ERROR = () => 1280;

const TRANSFERSTAFFRECORD_ERROR = () => 1400;
const { addCrossValidateError, MAIN_JOB_ERRORS, TRANSFER_STAFF_RECORD_ERRORS } = require('./crossValidateErrors');

const crossValidate = async (csvWorkerSchemaErrors, myEstablishments, JSONWorker) => {
if (workerNotChanged(JSONWorker)) {
Expand All @@ -17,17 +14,11 @@ const crossValidate = async (csvWorkerSchemaErrors, myEstablishments, JSONWorker

const _crossValidateMainJobRole = (csvWorkerSchemaErrors, isCqcRegulated, JSONWorker) => {
if (!isCqcRegulated && JSONWorker.mainJobRoleId === 4) {
csvWorkerSchemaErrors.unshift({
worker: JSONWorker.uniqueWorkerId,
name: JSONWorker.localId,
lineNumber: JSONWorker.lineNumber,
errCode: MAIN_JOB_ROLE_ERROR(),
errType: 'MAIN_JOB_ROLE_ERROR',
source: JSONWorker.mainJobRoleId,
column: 'MAINJOBROLE',
error:
'Workers MAINJOBROLE is Registered Manager but you are not providing a CQC regulated service. Please change to another Job Role',
});
addCrossValidateError(
csvWorkerSchemaErrors,
MAIN_JOB_ERRORS.RegisteredManagerWithoutCqcRegulatedService,
JSONWorker,
);
}
};

Expand Down Expand Up @@ -92,7 +83,7 @@ const _validateTransferIsPossible = async (csvWorkerSchemaErrors, relatedEstabli
const newWorkplaceId = await _getNewWorkplaceId(newWorkplaceLocalRef, relatedEstablishmentIds);

if (newWorkplaceId === null) {
_addErrorForNewWorkplaceNotFound(csvWorkerSchemaErrors, JSONWorker);
addCrossValidateError(csvWorkerSchemaErrors, TRANSFER_STAFF_RECORD_ERRORS.NewWorkplaceNotFound, JSONWorker);
return;
}

Expand All @@ -102,7 +93,11 @@ const _validateTransferIsPossible = async (csvWorkerSchemaErrors, relatedEstabli
);

if (sameLocalIdExistInNewWorkplace) {
_addErrorForSameLocalIdExistInNewWorkplace(csvWorkerSchemaErrors, JSONWorker);
addCrossValidateError(
csvWorkerSchemaErrors,
TRANSFER_STAFF_RECORD_ERRORS.SameLocalIdExistInNewWorkplace,
JSONWorker,
);
return;
}

Expand Down Expand Up @@ -142,32 +137,6 @@ const _addNewWorkplaceIdToWorkerEntity = (myAPIEstablishments, JSONWorker, newWo
workerEntity._newWorkplaceId = newWorkplaceId;
};

const _addErrorForNewWorkplaceNotFound = (csvWorkerSchemaErrors, JSONWorker) => {
csvWorkerSchemaErrors.unshift({
worker: JSONWorker.uniqueWorkerId,
name: JSONWorker.localId,
lineNumber: JSONWorker.lineNumber,
errCode: TRANSFERSTAFFRECORD_ERROR() + 1,
errType: 'TRANSFERSTAFFRECORD_ERROR',
source: JSONWorker.transferStaffRecord,
column: 'TRANSFERSTAFFRECORD',
error: 'Cannot find an existing workplace with the reference provided in TRANSFERSTAFFRECORD',
});
};

const _addErrorForSameLocalIdExistInNewWorkplace = (csvWorkerSchemaErrors, JSONWorker) => {
csvWorkerSchemaErrors.unshift({
worker: JSONWorker.uniqueWorkerId,
name: JSONWorker.localId,
lineNumber: JSONWorker.lineNumber,
errCode: TRANSFERSTAFFRECORD_ERROR() + 2,
errType: 'TRANSFERSTAFFRECORD_ERROR',
source: JSONWorker.uniqueWorkerId,
column: 'UNIQUEWORKERID',
error: 'The UNIQUEWORKERID for this worker is already used in the new workplace given in TRANSFERSTAFFRECORD',
});
};

const _crossValidateWorkersWithSameRefMovingToSameWorkplace = (
csvWorkerSchemaErrors,
allMovingWorkers,
Expand All @@ -190,7 +159,7 @@ const _crossValidateWorkersWithSameRefMovingToSameWorkplace = (
}

// if arrive at here, there is already another new or moving worker with that workerRef coming to the same new workplace
_addErrorForWorkersWithSameRefsMovingToSameWorkplace(csvWorkerSchemaErrors, JSONWorker);
addCrossValidateError(csvWorkerSchemaErrors, TRANSFER_STAFF_RECORD_ERRORS.SameRefsMovingToWorkplace, JSONWorker);
}
};

Expand All @@ -203,17 +172,7 @@ const _buildWorkplaceDictWithNewWorkers = (allNewWorkers) => {
};

const _addErrorForWorkersWithSameRefsMovingToSameWorkplace = (csvWorkerSchemaErrors, JSONWorker) => {
csvWorkerSchemaErrors.unshift({
worker: JSONWorker.uniqueWorkerId,
name: JSONWorker.localId,
lineNumber: JSONWorker.lineNumber,
errCode: TRANSFERSTAFFRECORD_ERROR() + 3,
errType: 'TRANSFERSTAFFRECORD_ERROR',
source: JSONWorker.uniqueWorkerId,
column: 'UNIQUEWORKERID',
error:
'There are more than one worker with this UNIQUEWORKERID moving into the new workplace given in TRANSFERSTAFFRECORD.',
});
addCrossValidateError(csvWorkerSchemaErrors, TRANSFER_STAFF_RECORD_ERRORS.SameRefsMovingToWorkplace, JSONWorker);
};

module.exports = {
Expand Down
57 changes: 57 additions & 0 deletions backend/server/models/BulkImport/csv/crossValidateErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const MAIN_JOB_ROLE_ERROR_CODE = 1280;
const TRANSFER_STAFF_RECORD_BASE_ERROR_CODE = 1400;

const MAIN_JOB_ERRORS = Object.freeze({
RegisteredManagerWithoutCqcRegulatedService: {
errCode: MAIN_JOB_ROLE_ERROR_CODE,
errType: 'MAIN_JOB_ROLE_ERROR',
column: 'MAINJOBROLE',
_sourceFieldName: 'mainJobRoleId',
error:
'Workers MAINJOBROLE is Registered Manager but you are not providing a CQC regulated service. Please change to another Job Role',
},
});

const TRANSFER_STAFF_RECORD_ERRORS = Object.freeze({
NewWorkplaceNotFound: {
errCode: TRANSFER_STAFF_RECORD_BASE_ERROR_CODE + 1,
errType: 'TRANSFERSTAFFRECORD_ERROR',
column: 'TRANSFERSTAFFRECORD',
_sourceFieldName: 'transferStaffRecord',
error: 'Cannot find an existing workplace with the reference provided in TRANSFERSTAFFRECORD',
},
SameLocalIdExistInNewWorkplace: {
errCode: TRANSFER_STAFF_RECORD_BASE_ERROR_CODE + 2,
errType: 'TRANSFERSTAFFRECORD_ERROR',
column: 'UNIQUEWORKERID',
_sourceFieldName: 'uniqueWorkerId',
error: 'The UNIQUEWORKERID for this worker is already used in the new workplace given in TRANSFERSTAFFRECORD',
},
SameRefsMovingToWorkplace: {
errCode: TRANSFER_STAFF_RECORD_BASE_ERROR_CODE + 3,
errType: 'TRANSFERSTAFFRECORD_ERROR',
column: 'UNIQUEWORKERID',
_sourceFieldName: 'uniqueWorkerId',
error:
'There are more than one worker with this UNIQUEWORKERID moving into the new workplace given in TRANSFERSTAFFRECORD.',
},
});

const addCrossValidateError = (errorsArray, errorType, JSONWorker) => {
const newErrorObject = {
...errorType,
worker: JSONWorker.uniqueWorkerId,
name: JSONWorker.localId,
lineNumber: JSONWorker.lineNumber,
source: JSONWorker[errorType._sourceFieldName],
};
delete newErrorObject._sourceFieldName;

errorsArray.unshift(newErrorObject);
};

module.exports = {
addCrossValidateError,
MAIN_JOB_ERRORS,
TRANSFER_STAFF_RECORD_ERRORS,
};

0 comments on commit b1a5fe1

Please sign in to comment.