@@ -29,28 +29,38 @@ exports.Initialise = (config, router, prototypeKit) => {
2929 // one created by us.
3030 config.uploadPath ??= path.join(os.tmpdir(), 'reg-dyn-importer'); ;
3131
32+
3233 //--------------------------------------------------------------------
3334 // Removes any previous importer error from the session. When we set
3435 // an error we redirect to the referer and we expect that page to show
3536 // the error. Calling this in each POST endpoint ensures that we don't
3637 // remember errors after they have been shown,
3738 //--------------------------------------------------------------------
38- const cleanRequest = (request) => {
39+ router.all("*", (request, res, next ) => {
3940 delete request.session.data[IMPORTER_ERROR_KEY]
40- }
41+ next();
42+ });
43+
4144
4245 //--------------------------------------------------------------------
4346 // Make the route functions available in the templates. These functions
4447 // allow users to find the path that they should use to submit data,
4548 // and also allows them to specify which url to redirect to after
46- // the POST data has been processed.
49+ // the POST data has been processed. Where an extra error url is provided
50+ // it will be used if the POST request does not succeed (e.g. when
51+ // mapping the data this can be used to redirect to the review page
52+ // to view warnings)
4753 //--------------------------------------------------------------------
4854 for ([key, value] of IMPORTER_ROUTE_MAP) {
4955 const k = key;
5056 const v = value;
5157
52- prototypeKit.views.addFunction(k, (next)=>{
53- return `${v}?next=${encodeURIComponent(next)}`;
58+ prototypeKit.views.addFunction(k, (next, errorPage=null)=>{
59+ let url = `${v}?next=${encodeURIComponent(next)}`;
60+ if (errorPage) {
61+ url = url + `&error=${encodeURIComponent(errorPage)}`
62+ }
63+ return url
5464 }, {})
5565 }
5666
@@ -140,8 +150,13 @@ exports.Initialise = (config, router, prototypeKit) => {
140150 // Redirects the current request to the 'next' URL after decoding the
141151 // encoded URI.
142152 //--------------------------------------------------------------------
143- const redirectOnwards = (request, response) => {
144- response.redirect(decodeURIComponent(request.query.next));
153+ const redirectOnwards = (request, response, failed=false) => {
154+ console.log(request.query)
155+ if (failed && request.query.error) {
156+ response.redirect(decodeURIComponent(request.query.error));
157+ } else {
158+ response.redirect(decodeURIComponent(request.query.next));
159+ }
145160 }
146161
147162 //--------------------------------------------------------------------
@@ -152,7 +167,6 @@ exports.Initialise = (config, router, prototypeKit) => {
152167 // time we run through the process.
153168 //--------------------------------------------------------------------
154169 router.all(IMPORTER_ROUTE_MAP.get("importerStartPath"),(request, response) => {
155- cleanRequest(request);
156170 redirectOnwards(request, response);
157171 });
158172
@@ -163,8 +177,6 @@ exports.Initialise = (config, router, prototypeKit) => {
163177 const uploader = getUploader(config);
164178
165179 router.post(IMPORTER_ROUTE_MAP.get("importerUploadPath"), uploader.single("file"), (request, response) => {
166- cleanRequest(request);
167-
168180 let createResponse = session_lib.CreateSession(config, request);
169181
170182 if (createResponse.error) {
@@ -274,6 +286,15 @@ exports.Initialise = (config, router, prototypeKit) => {
274286 }
275287
276288 session.mapping = request.body;
289+ if (Object.values(session.mapping).every((v) => v=='') ) {
290+ request.session.data[IMPORTER_ERROR_KEY] = "No columns were mapped to the expected fields"
291+ if (!request.query.error) {
292+ response.redirect(request.get('Referrer'))
293+ } else {
294+ redirectOnwards(request, response, failed=true);
295+ }
296+ return;
297+ }
277298
278299 // Ensure the session is persisted. Currently in session, eventually another way
279300 request.session.data[IMPORTER_SESSION_KEY] = session;
0 commit comments