Add flag to retrieve the latest version of an article

This commit is contained in:
Emmanuel 2023-09-28 17:51:23 +02:00
parent f4fa28e0cc
commit 7633d1848d
4 changed files with 116 additions and 23 deletions

View file

@ -3,6 +3,8 @@ import type {
DossierLegislatif,
Idcc,
Jo,
JorfArticle,
LegiArticle,
SectionTa,
Textekali,
Textelr,
@ -11,7 +13,7 @@ import type {
} from "$lib/legal"
export interface Aggregate {
article?: { [id: string]: Article }
article?: { [id: string]: Article | JorfArticle | LegiArticle }
dossier_legislatif?: { [id: string]: DossierLegislatif }
id?: string
idcc?: { [id: string]: Idcc }

View file

@ -4,7 +4,9 @@ import type { Aggregate, Follow } from "$lib/aggregates"
import {
rootTypeFromLegalId,
type Article,
type JorfArticle,
type LegalObjectType,
type LegiArticle,
type SectionTa,
type Textekali,
type Textelr,
@ -13,7 +15,7 @@ import {
import { db } from "$lib/server/databases"
export class Aggregator {
article: { [id: string]: Article } = {}
article: { [id: string]: Article | JorfArticle | LegiArticle } = {}
follow: Set<Follow>
requestedTypeAndIds: Set<TypeAndId> = new Set()
section_ta: { [id: string]: SectionTa } = {}
@ -26,13 +28,19 @@ export class Aggregator {
this.follow = new Set(follow)
}
addArticle(article: Article): void {
addArticle(article: Article | JorfArticle | LegiArticle): void {
const id = article.META.META_COMMUN.ID
this.article[id] = article
if (this.follow.has("LIENS.LIEN[@sens=cible,@typelien=CREATION].@id")) {
for (const lien of iterArrayOrSingleton(article.LIENS?.LIEN)) {
if (lien["@sens"] === "cible" && lien["@typelien"] === "CREATION") {
for (const lien of iterArrayOrSingleton(
(article as LegiArticle).LIENS?.LIEN,
)) {
if (
lien["@id"] !== undefined &&
lien["@sens"] === "cible" &&
lien["@typelien"] === "CREATION"
) {
this.requestId(lien["@id"])
}
}

View file

@ -1,10 +1,16 @@
import { type Audit, auditSetNullish, cleanAudit } from "@auditors/core"
import {
auditSetNullish,
auditSingleton,
auditStringToBoolean,
auditTrimString,
cleanAudit,
type Audit,
} from "@auditors/core"
import { error } from "@sveltejs/kit"
import { auditFollowQuery } from "$lib/auditors/queries"
import type { Follow } from "$lib/aggregates"
import type { Article } from "$lib/legal"
import { auditFollowQuery } from "$lib/auditors/queries"
import type { JorfArticle, LegiArticle } from "$lib/legal"
import { Aggregator } from "$lib/server/aggregates"
import { db } from "$lib/server/databases"
@ -30,14 +36,28 @@ function auditQuery(audit: Audit, query: URLSearchParams): [unknown, unknown] {
const remainingKeys = new Set(Object.keys(data))
auditFollowQuery(audit, data, errors, remainingKeys)
audit.attribute(
data,
"latest",
true,
errors,
remainingKeys,
auditSingleton(
auditTrimString,
auditStringToBoolean,
auditSetNullish(false),
),
)
return audit.reduceRemaining(data, errors, remainingKeys, auditSetNullish({}))
}
export const GET: RequestHandler = async ({ params, url }) => {
let id = params.id
const [query, queryError] = auditQuery(cleanAudit, url.searchParams) as [
{
follow: Set<Follow>
latest: boolean
},
unknown,
]
@ -51,15 +71,37 @@ export const GET: RequestHandler = async ({ params, url }) => {
)
throw error(400, JSON.stringify(queryError, null, 2))
}
const { follow } = query
const article = (
await db<{ data: Article }[]>`
SELECT data FROM article
WHERE ID = ${params.id}
`
const { follow, latest } = query
let article = (
await db<{ data: JorfArticle | LegiArticle }[]>`
SELECT data FROM article
WHERE ID = ${id}
`
).map(({ data }) => data)[0]
if (article === undefined) {
throw error(404, `ARTICLE ${params.id} non trouvé`)
throw error(404, `ARTICLE ${id} non trouvé`)
}
if (latest) {
const latestVersion = article.VERSIONS.VERSION.at(-1)
const latestVersionId = latestVersion?.LIEN_ART["@id"]
const isLatestVersion = article.META.META_COMMUN.ID === latestVersionId
if (!isLatestVersion && latestVersionId !== undefined) {
const latestArticle = (
await db<{ data: JorfArticle | LegiArticle }[]>`
SELECT data FROM article
WHERE ID = ${latestVersionId}
`
).map(({ data }) => data)[0]
if (latestArticle === undefined) {
console.error(
`Dernière version ${latestVersionId} de l'ARTICLE ${article.META.META_COMMUN.ID} non trouvée`,
)
} else {
article = latestArticle
id = latestVersionId
}
}
}
const aggregator = new Aggregator(follow)
@ -71,7 +113,7 @@ export const GET: RequestHandler = async ({ params, url }) => {
{
...aggregator.toJson(),
follow: [...follow],
id: params.id,
id,
},
null,
2,

View file

@ -1,9 +1,16 @@
import { type Audit, auditSetNullish, cleanAudit } from "@auditors/core"
import {
auditSetNullish,
auditSingleton,
auditTrimString,
auditStringToBoolean,
cleanAudit,
type Audit,
} from "@auditors/core"
import { error } from "@sveltejs/kit"
import type { Follow } from "$lib/aggregates"
import { auditFollowQuery, auditQQueryParameter } from "$lib/auditors/queries"
import type { Article } from "$lib/legal"
import type { JorfArticle, LegiArticle } from "$lib/legal"
import { Aggregator } from "$lib/server/aggregates"
import { db } from "$lib/server/databases"
@ -30,13 +37,25 @@ function auditQuery(audit: Audit, query: URLSearchParams): [unknown, unknown] {
auditFollowQuery(audit, data, errors, remainingKeys)
auditQQueryParameter(audit, data, errors, remainingKeys)
audit.attribute(
data,
"latest",
true,
errors,
remainingKeys,
auditSingleton(
auditTrimString,
auditStringToBoolean,
auditSetNullish(false),
),
)
return audit.reduceRemaining(data, errors, remainingKeys, auditSetNullish({}))
}
export const GET: RequestHandler = async ({ url }) => {
const [query, queryError] = auditQuery(cleanAudit, url.searchParams) as [
{ follow: Set<Follow>; q?: string },
{ follow: Set<Follow>; latest: boolean; q?: string },
unknown,
]
if (queryError !== null) {
@ -49,7 +68,7 @@ export const GET: RequestHandler = async ({ url }) => {
)
throw error(400, JSON.stringify(queryError, null, 2))
}
const { follow, q } = query
const { follow, q, latest } = query
const aggregator = new Aggregator(follow)
let id: string | undefined = undefined
@ -62,13 +81,35 @@ export const GET: RequestHandler = async ({ url }) => {
// https://www.legifrance.gouv.fr/loda/id/LEGIARTI000006317314/1983-12-30
id = q.match(/LEGIARTI\d+/)?.[0]
if (id != null) {
const article = (
await db<{ data: Article }[]>`
let article = (
await db<{ data: JorfArticle | LegiArticle }[]>`
SELECT data FROM article
WHERE id = ${id}
`
).map(({ data }) => data)[0]
if (article !== undefined) {
if (latest) {
const latestVersion = article.VERSIONS.VERSION.at(-1)
const latestVersionId = latestVersion?.LIEN_ART["@id"]
const isLatestVersion =
article.META.META_COMMUN.ID === latestVersionId
if (!isLatestVersion && latestVersionId !== undefined) {
const latestArticle = (
await db<{ data: JorfArticle | LegiArticle }[]>`
SELECT data FROM article
WHERE ID = ${latestVersionId}
`
).map(({ data }) => data)[0]
if (latestArticle === undefined) {
console.error(
`Dernière version ${latestVersionId} de l'ARTICLE ${article.META.META_COMMUN.ID} non trouvée`,
)
} else {
article = latestArticle
id = latestVersionId
}
}
}
aggregator.addArticle(article)
}
}