@@ -30,28 +30,38 @@ exports.Initialise = (config, router, prototypeKit) => {
3030 // one created by us.
3131 config . uploadPath ??= path . join ( os . tmpdir ( ) , 'reg-dyn-importer' ) ; ;
3232
33+
3334 //--------------------------------------------------------------------
3435 // Removes any previous importer error from the session. When we set
3536 // an error we redirect to the referer and we expect that page to show
3637 // the error. Calling this in each POST endpoint ensures that we don't
3738 // remember errors after they have been shown,
3839 //--------------------------------------------------------------------
39- const cleanRequest = ( request ) => {
40+ router . all ( "*" , ( request , res , next ) => {
4041 delete request . session . data [ IMPORTER_ERROR_KEY ]
41- }
42+ next ( ) ;
43+ } ) ;
44+
4245
4346 //--------------------------------------------------------------------
4447 // Make the route functions available in the templates. These functions
4548 // allow users to find the path that they should use to submit data,
4649 // and also allows them to specify which url to redirect to after
47- // the POST data has been processed.
50+ // the POST data has been processed. Where an extra error url is provided
51+ // it will be used if the POST request does not succeed (e.g. when
52+ // mapping the data this can be used to redirect to the review page
53+ // to view warnings)
4854 //--------------------------------------------------------------------
4955 for ( [ key , value ] of IMPORTER_ROUTE_MAP ) {
5056 const k = key ;
5157 const v = value ;
5258
53- prototypeKit . views . addFunction ( k , ( next ) => {
54- return `${ v } ?next=${ encodeURIComponent ( next ) } ` ;
59+ prototypeKit . views . addFunction ( k , ( next , errorPage = null ) => {
60+ let url = `${ v } ?next=${ encodeURIComponent ( next ) } ` ;
61+ if ( errorPage ) {
62+ url = url + `&error=${ encodeURIComponent ( errorPage ) } `
63+ }
64+ return url
5565 } , { } )
5666 }
5767
@@ -169,8 +179,13 @@ exports.Initialise = (config, router, prototypeKit) => {
169179 // Redirects the current request to the 'next' URL after decoding the
170180 // encoded URI.
171181 //--------------------------------------------------------------------
172- const redirectOnwards = ( request , response ) => {
173- response . redirect ( decodeURIComponent ( request . query . next ) ) ;
182+ const redirectOnwards = ( request , response , failed = false ) => {
183+ console . log ( request . query )
184+ if ( failed && request . query . error ) {
185+ response . redirect ( decodeURIComponent ( request . query . error ) ) ;
186+ } else {
187+ response . redirect ( decodeURIComponent ( request . query . next ) ) ;
188+ }
174189 }
175190
176191 //--------------------------------------------------------------------
@@ -181,7 +196,6 @@ exports.Initialise = (config, router, prototypeKit) => {
181196 // time we run through the process.
182197 //--------------------------------------------------------------------
183198 router . all ( IMPORTER_ROUTE_MAP . get ( "importerStartPath" ) , ( request , response ) => {
184- cleanRequest ( request ) ;
185199 redirectOnwards ( request , response ) ;
186200 } ) ;
187201
@@ -192,8 +206,6 @@ exports.Initialise = (config, router, prototypeKit) => {
192206 const uploader = getUploader ( config ) ;
193207
194208 router . post ( IMPORTER_ROUTE_MAP . get ( "importerUploadPath" ) , uploader . single ( "file" ) , ( request , response ) => {
195- cleanRequest ( request ) ;
196-
197209 let createResponse = session_lib . CreateSession ( config , request ) ;
198210
199211 if ( createResponse . error ) {
@@ -320,6 +332,15 @@ exports.Initialise = (config, router, prototypeKit) => {
320332 }
321333
322334 session . mapping = request . body ;
335+ if ( Object . values ( session . mapping ) . every ( ( v ) => v == '' ) ) {
336+ request . session . data [ IMPORTER_ERROR_KEY ] = "No columns were mapped to the expected fields"
337+ if ( ! request . query . error ) {
338+ response . redirect ( request . get ( 'Referrer' ) )
339+ } else {
340+ redirectOnwards ( request , response , failed = true ) ;
341+ }
342+ return ;
343+ }
323344
324345 // Ensure the session is persisted. Currently in session, eventually another way
325346 request . session . data [ IMPORTER_SESSION_KEY ] = session ;
0 commit comments