From bcf51732c1bdc73fc91789a9ffc3cb344f0e788c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Courvoisier?= Date: Fri, 19 Aug 2022 14:53:21 +0200 Subject: [PATCH] Add fields for fixing #88 and #111 --- src/api/export.js | 14 ++++---------- src/api/search.js | 14 ++++---------- src/modules/elastic/decision.js | 3 +++ src/modules/elastic/export.js | 5 +++++ src/modules/elastic/query.js | 19 +++++++++++++++++-- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/api/export.js b/src/api/export.js index 110ea376..83a43101 100644 --- a/src/api/export.js +++ b/src/api/export.js @@ -5,6 +5,8 @@ const { checkSchema, validationResult } = require('express-validator'); const Elastic = require('../modules/elastic'); const taxons = require('../taxons'); const route = 'export'; +const iso8601 = + /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; api.get( `/${route}`, @@ -211,11 +213,7 @@ api.get( const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ route: `${req.method} ${req.path}`, errors: errors.array() }); - } else if ( - req.query && - typeof req.query.date_start === 'string' && - /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_start) === false - ) { + } else if (req.query && typeof req.query.date_start === 'string' && iso8601.test(req.query.date_start) === false) { return res.status(400).json({ route: `${req.method} ${req.path}`, errors: [ @@ -227,11 +225,7 @@ api.get( }, ], }); - } else if ( - req.query && - typeof req.query.date_end === 'string' && - /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_end) === false - ) { + } else if (req.query && typeof req.query.date_end === 'string' && iso8601.test(req.query.date_end) === false) { return res.status(400).json({ route: `${req.method} ${req.path}`, errors: [ diff --git a/src/api/search.js b/src/api/search.js index 4610080c..3a622695 100644 --- a/src/api/search.js +++ b/src/api/search.js @@ -5,6 +5,8 @@ const { checkSchema, validationResult } = require('express-validator'); const Elastic = require('../modules/elastic'); const taxons = require('../taxons'); const route = 'search'; +const iso8601 = + /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; api.get( `/${route}`, @@ -233,11 +235,7 @@ api.get( }, ], }); - } else if ( - req.query && - typeof req.query.date_start === 'string' && - /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_start) === false - ) { + } else if (req.query && typeof req.query.date_start === 'string' && iso8601.test(req.query.date_start) === false) { return res.status(400).json({ route: `${req.method} ${req.path}`, errors: [ @@ -249,11 +247,7 @@ api.get( }, ], }); - } else if ( - req.query && - typeof req.query.date_end === 'string' && - /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_end) === false - ) { + } else if (req.query && typeof req.query.date_end === 'string' && iso8601.test(req.query.date_end) === false) { return res.status(400).json({ route: `${req.method} ${req.path}`, errors: [ diff --git a/src/modules/elastic/decision.js b/src/modules/elastic/decision.js index a819b57b..b6fd7539 100644 --- a/src/modules/elastic/decision.js +++ b/src/modules/elastic/decision.js @@ -151,6 +151,7 @@ async function decision(query) { ? taxons[taxonFilter].chamber.taxonomy[rawResult._source.chamber] : rawResult._source.chamber, decision_date: rawResult._source.decision_date, + decision_datetime: rawResult._source.decision_datetime, ecli: rawResult._source.ecli, jurisdiction: query.resolve_references && taxons[taxonFilter].jurisdiction.taxonomy[rawResult._source.jurisdiction] @@ -188,6 +189,7 @@ async function decision(query) { ? taxons[taxonFilter].location.taxonomy[rawResult._source.location] : rawResult._source.location, update_date: rawResult._source.update_date, + update_datetime: rawResult._source.update_datetime, summary: rawResult._source.summary, themes: rawResult._source.themes, nac: rawResult._source.nac ? rawResult._source.nac : null, @@ -214,6 +216,7 @@ async function decision(query) { rawResult._source.rapprochements && rawResult._source.rapprochements.value ? rawResult._source.rapprochements.value : [], + legacy: rawResult._source.legacy ? rawResult._source.legacy : {}, }; if (response.type === 'undefined') { diff --git a/src/modules/elastic/export.js b/src/modules/elastic/export.js index 6607e8cb..d78d1dcd 100644 --- a/src/modules/elastic/export.js +++ b/src/modules/elastic/export.js @@ -73,7 +73,9 @@ async function batchexport(query) { }) : rawResult._source.publication, decision_date: rawResult._source.decision_date, + decision_datetime: rawResult._source.decision_datetime, update_date: rawResult._source.update_date, + update_datetime: rawResult._source.update_datetime, solution: query.resolve_references && taxons[taxonFilter].solution.taxonomy[rawResult._source.solution] ? taxons[taxonFilter].solution.taxonomy[rawResult._source.solution] @@ -109,11 +111,13 @@ async function batchexport(query) { rawResult._source.rapprochements && rawResult._source.rapprochements.value ? rawResult._source.rapprochements.value : [], + legacy: rawResult._source.legacy ? rawResult._source.legacy : {}, }; if (query.abridged) { delete result.source; delete result.text; delete result.update_date; + delete result.update_datetime; delete result.zones; delete result.contested; delete result.forward; @@ -121,6 +125,7 @@ async function batchexport(query) { delete result.rapprochements; delete result.timeline; delete result.partial; + delete result.legacy; } if (Array.isArray(result.timeline) && result.timeline.length < 2) { diff --git a/src/modules/elastic/query.js b/src/modules/elastic/query.js index 82cd8d7f..cd0e1f17 100644 --- a/src/modules/elastic/query.js +++ b/src/modules/elastic/query.js @@ -362,9 +362,24 @@ function buildQuery(query, target, relaxed) { // Date start/end (filter): if (query.date_start || query.date_end) { - let date_field = 'decision_date'; + let datetime = false; + if ( + (query.date_start && /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_start) === false) || + (query.date_end && /^\d\d\d\d-\d\d-\d\d$/.test(req.query.date_end) === false) + ) { + datetime = true; + } + let date_field = datetime ? 'decision_datetime' : 'decision_date'; + if (target === 'export' && query.date_type) { - date_field = query.date_type === 'creation' ? 'decision_date' : 'update_date'; + date_field = + query.date_type === 'creation' + ? datetime + ? 'decision_datetime' + : 'decision_date' + : datetime + ? 'update_datetime' + : 'update_date'; } let range = { range: {},