Commit d540190c authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Add search for legislature and pagination in list of "dossiers".

parent bc0fe897
......@@ -117,7 +117,8 @@ async function configureDatabase() {
uid text NOT NULL PRIMARY KEY,
data jsonb,
date_dernier_acte timestamptz,
date_prochain_acte timestamptz
date_prochain_acte timestamptz,
legislature int NOT NULL
)
`,
)
......@@ -127,6 +128,9 @@ async function configureDatabase() {
await db.none(
"CREATE INDEX IF NOT EXISTS dossiers_date_prochain_acte_idx ON dossiers (date_prochain_acte)",
)
await db.none(
"CREATE INDEX IF NOT EXISTS dossiers_legislature_idx ON dossiers (legislature)",
)
for (const command of [
"COMMENT ON TABLE dossiers IS 'set of documents and operations produced during the" +
" legislative process of a bill'",
......@@ -134,6 +138,7 @@ async function configureDatabase() {
"COMMENT ON COLUMN dossiers.data IS 'JSON data of dossier'",
"COMMENT ON COLUMN dossiers.date_dernier_acte IS 'date & time of the latest acte'",
"COMMENT ON COLUMN dossiers.date_prochain_acte IS 'date & time of the next acte'",
"COMMENT ON COLUMN dossiers.legislature IS 'number of the legislature'",
]) {
await db.none(command)
}
......
import querystring from "querystring"
import {
validateDateIso8601String,
validateInteger,
......@@ -95,13 +97,16 @@ export const listDossiers = wrapAsyncMiddleware(async function listDossiers(
),
)
}
const { date, follow, limit, offset, q: term } = query
const { date, follow, legislature, limit, offset, q: term } = query
const joinClauses: string[] = []
const whereClauses: string[] = []
if (date !== null) {
whereClauses.push("(date_dernier_acte >= $<date> OR date_prochain_acte NOTNULL)")
}
if (legislature !== null) {
whereClauses.push("(legislature = $<legislature>")
}
if (term !== null) {
whereClauses.push(`uid IN (
SELECT uid
......@@ -126,12 +131,32 @@ export const listDossiers = wrapAsyncMiddleware(async function listDossiers(
`,
{
date,
limit,
legislature,
limit: limit + 1,
offset,
term,
},
)).map(extractDataFromEntry)
let next = null
if (dossiers.length > limit) {
dossiers.pop()
const nextQuery = Object.entries({
...query,
follow: Array.from(follow).sort(),
offset: offset + limit,
}).reduce((nextQuery: { [key: string]: any }, [key, value]) => {
if (value !== null && (!Array.isArray(value) || value.length > 0)) {
nextQuery[key] = value
}
return nextQuery
}, {})
next = `${req.protocol}://${req.get("host")}${
req.originalUrl.split("?")[0]
}?${querystring.stringify(nextQuery)}`
}
const dataGetter = new DataGetter(follow)
for (const dossier of dossiers) {
dataGetter.addDossier(dossier)
......@@ -145,6 +170,7 @@ export const listDossiers = wrapAsyncMiddleware(async function listDossiers(
JSON.stringify(
{
...dataGetter.toJson(),
next,
uids: dossiers.map(({ uid }) => uid),
},
null,
......@@ -249,6 +275,20 @@ function validateListDossiersQuery(data: any) {
}
}
{
const key = "legislature"
remainingKeys.delete(key)
const [value, error] = validateOption(validateMissing, [
validateStringToNumber,
validateInteger,
validateTest(value => value >= 0, "Value must be greater than or equal to 0"),
])(data[key])
data[key] = value
if (error !== null) {
errors[key] = error
}
}
{
const key = "limit"
remainingKeys.delete(key)
......@@ -257,7 +297,8 @@ function validateListDossiersQuery(data: any) {
[
validateStringToNumber,
validateInteger,
validateTest(value => value >= 0, "Value must be greater than or equal to 0"),
validateTest(value => value >= 1, "Value must be greater than or equal to 1"),
validateTest(value => value <= 100, "Value must be less than or equal to 100"),
],
)(data[key])
data[key] = value
......
......@@ -644,25 +644,25 @@ async function main() {
const existingDossiersUids = new Set(
(await db.any(
`
SELECT uid
FROM dossiers
`,
SELECT uid
FROM dossiers
`,
)).map(({ uid }) => uid),
)
const existingDossiersAutocompletions = new Set(
(await db.any(
`
SELECT uid, autocomplete
FROM dossiers_autocomplete
`,
SELECT uid, autocomplete
FROM dossiers_autocomplete
`,
)).map(({ uid, autocomplete }) => `${uid}_${autocomplete}`),
)
const existingDossiersTextsSearches = new Set(
(await db.any(
`
SELECT uid, text_search
FROM dossiers_text_search
`,
SELECT uid, text_search
FROM dossiers_text_search
`,
)).map(({ uid, text_search }) => `${uid}_${text_search}`),
)
......@@ -677,24 +677,28 @@ async function main() {
uid,
data,
date_dernier_acte,
date_prochain_acte
date_prochain_acte,
legislature
)
VALUES (
$<uid>,
$<data:json>,
$<dateDernierActe>,
$<dateProchainActe>
$<dateProchainActe>,
$<legislature>
)
ON CONFLICT (uid)
DO UPDATE SET
data = $<data:json>,
date_dernier_acte = $<dateDernierActe>,
date_prochain_acte = $<dateProchainActe>
date_prochain_acte = $<dateProchainActe>,
legislature = $<legislature>
`,
{
data: dossier,
dateDernierActe,
dateProchainActe,
legislature: options.legislature,
uid: dossier.uid,
},
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment