Skip to content

Commit

Permalink
Add fields for fixing #88 and #111
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCourvoisier committed Aug 19, 2022
1 parent a16da36 commit bcf5173
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
14 changes: 4 additions & 10 deletions src/api/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [
Expand Down
14 changes: 4 additions & 10 deletions src/api/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [
Expand Down
3 changes: 3 additions & 0 deletions src/modules/elastic/decision.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand All @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions src/modules/elastic/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -109,18 +111,21 @@ 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;
delete result.visa;
delete result.rapprochements;
delete result.timeline;
delete result.partial;
delete result.legacy;
}

if (Array.isArray(result.timeline) && result.timeline.length < 2) {
Expand Down
19 changes: 17 additions & 2 deletions src/modules/elastic/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down

0 comments on commit bcf5173

Please sign in to comment.