Skip to content

Commit 2ab7285

Browse files
committed
Ensure required fields are included in mapping
1 parent 579df1d commit 2ab7285

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

lib/importer/nunjucks/importer/macros/field_mapper.njk

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@
1717
{% macro importerFieldMapper(data, caption='', columnTitle='Column', examplesTitle='Example values', fieldsTitle='Fields') %}
1818
{% set fields = data['importer.session']['fields'] %}
1919
{% set headings = importerGetHeaders(data) %}
20-
{% set error = headings.error %}
20+
{% set headingError = headings.error %}
21+
{% set error = importerError(data) %}
2122

22-
{% if error %}
23+
{% if headingError %}
2324
<p id="mapping-error" class="govuk-error-message">
24-
<span class="govuk-visually-hidden">Error:</span> {{ error.text }}
25+
<span class="govuk-visually-hidden">Error:</span> {{ headingError.text }}
2526
</p>
2627
{% endif %}
2728

29+
{% if error %}
30+
<div class="govuk-error-summary" data-module="govuk-error-summary">
31+
<div role="alert">
32+
<h2 class="govuk-error-summary__title">
33+
{{ error.text }}
34+
</h2>
35+
<div class="govuk-error-summary__body">
36+
<ul class="govuk-list govuk-error-summary__list">
37+
{% for e in error.extra %}
38+
<li>
39+
<a href="#">{{ e }}</a>
40+
</li>
41+
{% endfor %}
42+
</ul>
43+
</div>
44+
</div>
45+
</div>
46+
47+
{% endif %}
48+
2849

2950
<table class="govuk-table">
3051
{% if caption %}

lib/importer/src/index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const sheets_lib = require("./sheets.js");
77

88
const IMPORTER_SESSION_KEY = "importer.session";
99
const IMPORTER_ERROR_KEY = "importer.error";
10+
const IMPORTER_ERROR_EXTRA_KEY = "importer.error.extra";
1011

1112
//--------------------------------------------------------------------
1213
// The endpoints used to receive POST requests from importer macros,
@@ -73,6 +74,7 @@ exports.Initialise = (config, router, prototypeKit) => {
7374
const cleanRequest = (request) => {
7475
delete request.session.data['reference_number'];
7576
delete request.session.data[IMPORTER_ERROR_KEY];
77+
delete request.session.data[IMPORTER_ERROR_EXTRA_KEY];
7678
};
7779

7880

@@ -124,7 +126,7 @@ exports.Initialise = (config, router, prototypeKit) => {
124126
"importerError",
125127
(data) => {
126128
if (data[IMPORTER_ERROR_KEY]) {
127-
return { text: data[IMPORTER_ERROR_KEY] };
129+
return { text: data[IMPORTER_ERROR_KEY], extra: data[IMPORTER_ERROR_EXTRA_KEY] };
128130
}
129131

130132
return false;
@@ -510,9 +512,33 @@ exports.Initialise = (config, router, prototypeKit) => {
510512
return;
511513
}
512514

515+
request.session.data['reference_number'] = session.id.match(/\d+/g).join("").substring(0, 8);
516+
513517
session.mapping = request.body;
514518

515-
request.session.data['reference_number'] = session.id.match(/\d+/g).join("").substring(0, 8);
519+
const values = new Array();
520+
521+
for (const [key, value] of Object.entries(session.mapping)) {
522+
if (value != '') {
523+
values.push(value)
524+
}
525+
}
526+
527+
// For each key in the config that is required, we need to ensure that the field name is present
528+
// in the values array. If not then we will
529+
const required_fields = plugin_config.fields
530+
.filter((f) => f.required)
531+
.filter((f) => !values.includes(f.name))
532+
.map((f) => f.name)
533+
534+
// If required_fields has any values left, then they are required fields not present in the
535+
// mapping. In which case we should return an error for the user.
536+
if (required_fields.length > 0) {
537+
request.session.data[IMPORTER_ERROR_KEY] = "The following fields are required"
538+
request.session.data[IMPORTER_ERROR_EXTRA_KEY] = required_fields
539+
response.redirect(request.get('Referrer'));
540+
return;
541+
}
516542

517543
// Ensure the session is persisted. Currently in session, eventually another way
518544
request.session.data[IMPORTER_SESSION_KEY] = session;

0 commit comments

Comments
 (0)