mon-entreprise/site/scripts/fetch-versement-mobilité.js

43 lines
975 B
JavaScript
Raw Normal View History

import { writeInDataDir } from './utils.js'
2023-09-28 16:03:46 +00:00
const CURRENT_YEAR = 2023
const CSV_URL =
2023-09-28 16:03:46 +00:00
'https://fichierdirect.declaration.urssaf.fr/static/tauxTransport.20230928.csv'
const INDEX = {
TAUX: 2,
CODE_COMMUNE: 0,
DATE_EFFET: 4,
}
2022-11-24 15:29:50 +00:00
const response = await fetch(CSV_URL)
const rawCSV = await response.text()
const data = rawCSV
.split('\n')
.slice(1, -1)
.map((row) => row.split(';'))
.filter((r) => r[INDEX.TAUX] !== '0')
.filter((r) => !r[INDEX.DATE_EFFET].startsWith((CURRENT_YEAR + 1).toString()))
.map((r) => {
r[INDEX.CODE_COMMUNE] = r[INDEX.CODE_COMMUNE].slice(1, -1) // Remove single quote ''
return r
})
.sort((a, b) =>
+a[INDEX.CODE_COMMUNE] < +b[INDEX.CODE_COMMUNE]
? -1
: +a[INDEX.CODE_COMMUNE] > +b[INDEX.CODE_COMMUNE]
2023-09-28 16:03:46 +00:00
? 1
: a[INDEX.DATE_EFFET] > b[INDEX.DATE_EFFET]
? -1
: 1
2022-11-24 15:29:50 +00:00
)
.reduce(
(acc, r) => ({
[r[INDEX.CODE_COMMUNE]]: +r[INDEX.TAUX],
...acc,
}),
{}
)
2022-11-24 15:29:50 +00:00
writeInDataDir('versement-mobilité.json', data)