Skip to content
Snippets Groups Projects
Commit f876d902 authored by David Smadja's avatar David Smadja
Browse files

Get first article when link points to LEGITEXT

parent a8ddce3f
No related branches found
No related tags found
No related merge requests found
......@@ -6,15 +6,62 @@ interface queryDataArticle {
data: LegiArticle
}
interface queryFirstArticle {
premier_article_id: string
}
export const GET: RequestHandler = async ({ params, locals }) => {
const { article } = params as { article: string }
const { article: requestedArticle } = params as { article: string }
const { sql } = locals
const dbConnection = await sql.reserve()
let articleTxtFromDb: queryDataArticle[] = []
if (requestedArticle.startsWith("LEGITEXT")) {
const [firstArticle]: [queryFirstArticle?] =
await dbConnection<queryFirstArticle>`
WITH RECURSIVE section_hierarchy AS (
SELECT
element->>'@id' as section_id,
ARRAY[ordinality] as path_array,
1 as depth
FROM textelr,
jsonb_array_elements(data->'STRUCT'->'LIEN_SECTION_TA')
WITH ORDINALITY as t(element, ordinality)
WHERE element->>'@etat' = 'VIGUEUR'
AND id = ${requestedArticle}
UNION ALL
SELECT
sub_element->>'@id' as section_id,
sh.path_array || sub_ordinality::int as path_array,
sh.depth + 1
FROM section_hierarchy sh
JOIN section_ta st ON st.id = sh.section_id
CROSS JOIN jsonb_array_elements(st.data->'STRUCTURE_TA'->'LIEN_SECTION_TA')
WITH ORDINALITY as sub_t(sub_element, sub_ordinality)
WHERE sub_element->>'@etat' = 'VIGUEUR'
AND sh.depth < 10
AND st.data @? '$.STRUCTURE_TA.LIEN_SECTION_TA[*] ? (@."@etat" == "VIGUEUR")'
)
SELECT
st.data #>> '{STRUCTURE_TA,LIEN_ART,0,@id}' as premier_article_id
FROM section_hierarchy sh
JOIN section_ta st ON st.id = sh.section_id
WHERE st.data @? '$.STRUCTURE_TA.LIEN_ART[0] ? (@."@etat" == "VIGUEUR")'
ORDER BY sh.path_array
LIMIT 1;`
const articleTxtFromDb: queryDataArticle[] = await dbConnection<JSON>`
articleTxtFromDb = await dbConnection<JSON>`
select data
from article
where id=${article}`
where id=${firstArticle?.premier_article_id}`
} else if (requestedArticle.startsWith("LEGIARTI")) {
articleTxtFromDb = await dbConnection<JSON>`
select data
from article
where id=${requestedArticle}`
}
dbConnection.release()
if (articleTxtFromDb.length === 1) {
......
......@@ -8,6 +8,7 @@
let activePanel: "bill" | "law" = "bill"
let screenWidth = $state(1024)
let isMobilePhone = $derived(screenWidth < 768)
let isFetchingArticle = $state(false)
let { data }: PageProps = $props()
......@@ -17,9 +18,13 @@
$effect(() => {
if (lawArticle) {
isFetchingArticle = true
fetch(`/api/article/${lawArticle}`)
.then((res) => (res.ok ? res.json() : null))
.then((data) => (articleJson = data))
.then((data) => {
isFetchingArticle = false
articleJson = data
})
.catch(() => (lawArticle = ""))
} else {
articleJson = undefined
......@@ -31,6 +36,9 @@
<div class="fixed flex min-h-full w-full flex-row overflow-hidden">
<div class="h-screen w-1/2 overflow-y-auto"><Bill {billHTML}></Bill></div>
<div class="h-screen w-1/2 overflow-y-auto bg-blue-100">
{#if isFetchingArticle}
Article en cours de récupération...
{/if}
{#if articleJson !== undefined}
<Article {articleJson}></Article>
{:else}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment