From c381086e3bfae3854a63672f7502bfd5f701e9e2 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Wed, 29 Nov 2023 11:25:55 +0000 Subject: [PATCH] update controllers to use severity field --- src/controllers/errorsController.js | 44 ++++++++++++++++------------- src/controllers/uploadController.js | 4 ++- src/utils/utils.js | 6 ++++ 3 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 src/utils/utils.js diff --git a/src/controllers/errorsController.js b/src/controllers/errorsController.js index d503e77e..436349a2 100644 --- a/src/controllers/errorsController.js +++ b/src/controllers/errorsController.js @@ -2,11 +2,13 @@ import MyController from './MyController.js' +import { severityLevels } from '../utils/utils.js' + class ErrorsController extends MyController { get (req, res, next) { const validationResult = req.sessionModel.get('validationResult') - const { aggregatedIssues, issueCounts } = this.aggregateIssues(validationResult) + const { aggregatedIssues, issueCounts } = this.getAggregatedErrors(validationResult) const rows = Object.values(aggregatedIssues) @@ -19,31 +21,33 @@ class ErrorsController extends MyController { super.get(req, res, next) } - aggregateIssues (apiResponseData) { + getAggregatedErrors (apiResponseData) { const aggregatedIssues = {} const issueCounts = {} apiResponseData['issue-log'].forEach(issue => { - const entryNumber = issue['entry-number'] - - const rowValues = apiResponseData['converted-csv'][issue['line-number'] - 2] - if (!(entryNumber in aggregatedIssues)) { - aggregatedIssues[entryNumber] = Object.keys(rowValues).reduce((acc, originalColumnName) => { - const mappedColumnName = this.lookupMappedColumnNameFromOriginal(originalColumnName, apiResponseData['column-field-log']) - acc[mappedColumnName] = { - error: false, - value: rowValues[originalColumnName] - } - return acc - }, {}) - } + if (issue.severity === severityLevels.error) { + const entryNumber = issue['entry-number'] - if (entryNumber in aggregatedIssues) { - aggregatedIssues[entryNumber][issue.field] = { - error: this.lookupIssueType(issue['issue-type']), - value: rowValues[this.lookupOriginalColumnNameFromMapped(issue.field, apiResponseData['column-field-log'])] + const rowValues = apiResponseData['converted-csv'][issue['line-number'] - 2] + if (!(entryNumber in aggregatedIssues)) { + aggregatedIssues[entryNumber] = Object.keys(rowValues).reduce((acc, originalColumnName) => { + const mappedColumnName = this.lookupMappedColumnNameFromOriginal(originalColumnName, apiResponseData['column-field-log']) + acc[mappedColumnName] = { + error: false, + value: rowValues[originalColumnName] + } + return acc + }, {}) + } + + if (entryNumber in aggregatedIssues) { + aggregatedIssues[entryNumber][issue.field] = { + error: this.lookupIssueType(issue['issue-type']), + value: rowValues[this.lookupOriginalColumnNameFromMapped(issue.field, apiResponseData['column-field-log'])] + } + issueCounts[issue.field] = issueCounts[issue.field] ? issueCounts[issue.field] + 1 : 1 } - issueCounts[issue.field] = issueCounts[issue.field] ? issueCounts[issue.field] + 1 : 1 } }) diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index 9c6bf1fc..cfcd92d1 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -6,6 +6,8 @@ import { lookup } from 'mime-types' import MyController from './MyController.js' import config from '../../config/index.js' +import { severityLevels } from '../utils/utils.js' + const upload = multer({ dest: 'uploads/' }) const apiRoute = config.api.url + config.api.validationEndpoint @@ -26,7 +28,7 @@ class UploadController extends MyController { dataSubject: req.sessionModel.get('data-subject'), organisation: 'local-authority-eng:CAT' // ToDo: this needs to be dynamic, not collected in the prototype, should it be? }) - this.errorCount = jsonResult['issue-log'].length + this.errorCount = jsonResult['issue-log'].filter(issue => issue.severity === severityLevels.error).length req.body.datafile = req.file req.body.validationResult = jsonResult } catch (error) { diff --git a/src/utils/utils.js b/src/utils/utils.js new file mode 100644 index 00000000..5f616e5e --- /dev/null +++ b/src/utils/utils.js @@ -0,0 +1,6 @@ +export const severityLevels = { + notice: 'notice', + informational: 'info', + warning: 'warning', + error: 'error' +}