11import fs from "node:fs/promises" ;
22import path from "node:path" ;
33import { differenceInYears } from "date-fns/differenceInYears" ;
4+ import { subYears } from "date-fns/subYears" ;
45import merge from "lodash.merge" ;
56import set from "lodash.set" ;
67import { parse } from "yaml" ;
@@ -80,6 +81,10 @@ async function fetchFromOpenFiscaFrance() {
8081 "taxation_societes/impot_societe/seuil_superieur_benefices_taux_reduit" ,
8182 "taxation_societes/impot_societe/taux_normal" ,
8283 "taxation_societes/impot_societe/taux_reduit" ,
84+ "taxation_indirecte/tva/taux_reduit" ,
85+ "taxation_indirecte/tva/taux_intermediaire" ,
86+ "taxation_indirecte/tva/taux_normal" ,
87+ "taxation_indirecte/tva/taux_particulier_super_reduit" ,
8388 ] ;
8489
8590 for ( const regle of regles ) {
@@ -104,12 +109,14 @@ async function fetchFromOpenFiscaFrance() {
104109 ) & {
105110 description : string ;
106111 metadata : {
107- last_value_still_valid_on : string ;
112+ last_value_still_valid_on ? : string ;
108113 } ;
109114 } ;
110115
111116 const lastValueStillValidOn = new Date (
112- json . metadata . last_value_still_valid_on ,
117+ json . metadata . last_value_still_valid_on ??
118+ // TODO: récupérer la dernière date de mise à jour de la valeur à défaut de "last_value_still_valid_on"
119+ subYears ( aujourdhui , 30 ) ,
113120 ) ;
114121 const lastValueInvalidSinceYears = differenceInYears (
115122 aujourdhui ,
@@ -118,31 +125,42 @@ async function fetchFromOpenFiscaFrance() {
118125
119126 if ( lastValueInvalidSinceYears >= 1 ) {
120127 warnings . push (
121- `⚠️ ${ regle } a été vérifiée il y a au moins ${ lastValueInvalidSinceYears } an(s)` ,
128+ `⚠️ ${ regle } a été vérifiée pour la dernière fois il y a ${ lastValueInvalidSinceYears } an(s)` ,
129+ ) ;
130+ }
131+
132+ const value =
133+ "values" in json
134+ ? getLatestValue ( json . values )
135+ : json . brackets
136+ . filter ( ( bracket ) =>
137+ Object . values ( bracket ) . some (
138+ ( value ) => getLatestValue ( value ) != null ,
139+ ) ,
140+ )
141+ . map ( ( bracket ) =>
142+ Object . entries ( bracket ) . reduce < Record < string , LeafValue > > (
143+ ( acc , [ key , value ] ) =>
144+ Object . assign ( acc , {
145+ [ key ] : getLatestValue ( value ) ,
146+ } ) ,
147+ { } ,
148+ ) ,
149+ ) ;
150+
151+ if (
152+ ( Array . isArray ( value ) && value . some ( ( value ) => value == null ) ) ||
153+ value == null
154+ ) {
155+ warnings . push (
156+ `⚠️ ${ regle } a produit des valeurs vides qui suggèrent qu'elle n'est plus applicable` ,
122157 ) ;
123158 }
124159
125160 set ( output , regle . split ( "/" ) . join ( "." ) , {
126161 url : fileUrl ,
127162 description : json . description ,
128- value :
129- "values" in json
130- ? getLatestValue ( json . values )
131- : json . brackets
132- . filter ( ( bracket ) =>
133- Object . values ( bracket ) . some (
134- ( value ) => getLatestValue ( value ) != null ,
135- ) ,
136- )
137- . map ( ( bracket ) =>
138- Object . entries ( bracket ) . reduce (
139- ( acc , [ key , value ] ) =>
140- Object . assign ( acc , {
141- [ key ] : getLatestValue ( value ) ,
142- } ) ,
143- { } ,
144- ) ,
145- ) ,
163+ value : value ,
146164 } ) ;
147165
148166 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
@@ -186,11 +204,11 @@ function stringify(parent: Branch): string {
186204 `* {@link ${ child . url } Source}` ,
187205 `*/` ,
188206 `${ key } : ${
189- typeof child . value === "number"
207+ child . value == null || typeof child . value === "number"
190208 ? `${ child . value } ,`
191209 : [
192210 `[` ,
193- ...child . value ! . flatMap ( ( value ) => [
211+ ...child . value . flatMap ( ( value ) => [
194212 `{` ,
195213 ...Object . entries ( value ) . map (
196214 ( [ key , value ] ) => `${ key } : ${ value } ,` ,
0 commit comments