diff --git a/.husky/pre-commit b/.husky/pre-commit index 251a23e7..6f5cf54d 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,5 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npm run lint \ No newline at end of file +npm run lint:fix +npm run lint diff --git a/src/controllers/uploadController.js b/src/controllers/uploadController.js index fc00d304..9af2c1a5 100644 --- a/src/controllers/uploadController.js +++ b/src/controllers/uploadController.js @@ -18,30 +18,40 @@ class UploadController extends Controller { } async post (req, res, next) { + if (req.file !== undefined) { + try { + const jsonResult = await this.validateFile({ + filePath: req.file.path, + fileName: req.file.originalname, + dataset: req.sessionModel.get('dataset'), + dataSubject: req.sessionModel.get('data-subject'), + organization: 'mockOrg' + }) + req.body.validationResult = jsonResult + } catch (error) { + console.log(error) + } + } + super.post(req, res, next) + } + + async validateFile ({ filePath, fileName, dataset, dataSubject, organization }) { const formData = new FormData() - formData.append('dataset', req.sessionModel.get('dataset')) - formData.append('collection', req.sessionModel.get('data-subject')) - formData.append('organization', 'mockOrg') + formData.append('dataset', dataset) + formData.append('collection', dataSubject) + formData.append('organization', organization) - const filePath = req.file.path const file = new Blob([await readFile(filePath)], { type: lookup(filePath) }) - formData.append('upload_file', file, req.file.originalname) + formData.append('upload_file', file, fileName) - try { - // post the data to the api - const result = await fetch(apiRoute, { - method: 'POST', - body: formData - }) + // post the data to the api + const result = await fetch(apiRoute, { + method: 'POST', + body: formData + }) - const json = await result.json() - - req.sessionModel.set('validationResult', json) - super.post(req, res, next) - } catch (e) { - res.send(e) - } + return await result.json() } } diff --git a/src/routes/form-wizard/steps.js b/src/routes/form-wizard/steps.js index 6a066ded..87a89d0f 100644 --- a/src/routes/form-wizard/steps.js +++ b/src/routes/form-wizard/steps.js @@ -16,6 +16,7 @@ module.exports = { }, '/upload': { controller: require('../../controllers/uploadController'), + fields: ['validationResult'], next: 'errors' }, '/errors': { diff --git a/src/views/data-subject.html b/src/views/data-subject.html index e762c8b8..4a26f76d 100644 --- a/src/views/data-subject.html +++ b/src/views/data-subject.html @@ -3,9 +3,28 @@ {% from 'govuk/components/back-link/macro.njk' import govukBackLink %} {% from 'govuk/components/button/macro.njk' import govukButton %} {% from 'govuk/components/radios/macro.njk' import govukRadios %} +{% from 'govuk/components/error-message/macro.njk' import govukErrorMessage %} +{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %} + + {% set pageName = 'Data subject' %} +{% set errorMessage = 'Please select a data subject' %} + +{% if 'data-subject' in errors %} + {% set dataSubjectError = true %} +{% endif %} + +{% block pageTitle %} + {% if dataSubjectError %} + Error: {{super()}} + {% else %} + {{super()}} + {% endif %} +{% endblock %} + + {% block beforeContent %} {{ govukBackLink({ text: "Back", @@ -16,6 +35,17 @@ {% block content %}