diff --git a/lib/www/server/lib/db/navdata/save.js b/lib/www/server/lib/db/navdata/save.js index 215b8b3..c6d16f6 100644 --- a/lib/www/server/lib/db/navdata/save.js +++ b/lib/www/server/lib/db/navdata/save.js @@ -211,6 +211,37 @@ async function saveOffline (navData, opts = {}) { client.release(); } +async function getCandidates (navData) { + + const configs = await getAllProjectConfigs(); + + // We just get the bits of interest: pattern and schema + const candidates = configs.map(c => { + if (!c?.data?.online?.line || c?.archived === true) { + return null; + } + + const p = c.data.online.line.pattern; // For short + + const rx = new RegExp(p.regex, p.flags); + const matches = navData.lineName.match(rx); + + if (!matches || ((matches.length+1) < p.captures.length)) { + return null; + } + + matches.shift(); // Get rid of the full matched text + const obj = Object.assign({}, navData, {schema: c.schema}); + p.captures.forEach( (k, i) => { + obj[k] = matches[i]; + }); + return obj; + }).filter(c => !!c); + DEBUG("Candidates: %j", candidates.map(c => c.schema)); + + return candidates; +} + async function save (navData, opts = {}) { const hasLatLon = ("latitude" in navData && "longitude" in navData); @@ -226,32 +257,7 @@ async function save (navData, opts = {}) { // So we have a lineName, see which projects match the line pattern. // For this we need to get all the project configs - const configs = await getAllProjectConfigs(); - - // We just get the bits of interest: pattern and schema - const candidates = configs.map(c => { - if (!(c && c.online && c.online.line)) { - return null; - } - - const p = c.online.line.pattern; // For short - - const rx = new RegExp(p.regex, p.flags); - const matches = navData.lineName.match(rx); - - if (!matches || ((matches.length+1) < p.captures.length)) { - return null; - } - - matches.shift(); // Get rid of the full matched text - const obj = Object.assign({}, navData, {schema: c.schema}); - p.captures.forEach( (k, i) => { - obj[k] = matches[i]; - }); - return obj; - }).filter(c => !!c); - DEBUG("Candidates: %j", candidates); -// console.log("CANDIDATES", candidates); + const candidates = await getCandidates(navData); if (candidates.length == 0) { // This is probably a test line, so we treat it as offline @@ -289,7 +295,7 @@ async function save (navData, opts = {}) { if (do_save) { const configs = await getAllProjectConfigs(); - const candidates = configs.map(c => Object.assign({}, navData, {_schema: c.schema})); + const candidates = await getCandidates(navData); const bestCandidate = await getNearestOfflinePreplot(candidates); if (bestCandidate) { navData.payload._schema = bestCandidate.schema;