Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mise à jour du paquet mesaidesvelo avec le nouveau paquet @betagouv/aides-velo #4705

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eb82a3e
pkg: remove 'aides-velo' and add '@betagouv/aides-velo'
EmileRolley Oct 31, 2024
61a6e7b
refactor(aides-velo)!: use the '@betagouv/aides-velo' package
EmileRolley Oct 31, 2024
8a34e45
fix(lib)!: IndividuMethods.age now returns undefined if date_naissanc…
EmileRolley Oct 31, 2024
8baf716
refactor(data/benefits)!: refactor aides-velo-generator with @betagou…
EmileRolley Oct 31, 2024
d6a9d61
fix(data): add missing institutions needed by @betagouv/aides-velo
EmileRolley Oct 31, 2024
04bdfc0
fix: nitpicks
EmileRolley Oct 31, 2024
033dcd3
ci: change dependabot to watch on @betagouv/aides-velo instead of aid…
EmileRolley Oct 31, 2024
52de903
fix(data): remove dependency to @lib/benefits/compute-aides-velo.js
EmileRolley Nov 4, 2024
2c9da02
fix(lib): remove custom paths imports
EmileRolley Nov 4, 2024
0ad2d6b
fix(lib): add missing individu-properties for interetsVelo
EmileRolley Nov 4, 2024
650138e
Revert "fix(lib)!: IndividuMethods.age now returns undefined if date_…
Shamzic Nov 6, 2024
dc0d9f3
fix: corrige le test aides-vélo avec l'âge undefined
Shamzic Nov 6, 2024
5687e52
refactor: compute-aides-velo
Shamzic Nov 6, 2024
a4df002
chore: resize logos institutions & fix extensions
Shamzic Nov 6, 2024
84ef88d
fix: image logo de la ville Taillain Médoc en format png
Shamzic Nov 6, 2024
e297429
chore: fixe une limite de la description des aides à 600 pour autoris…
Shamzic Nov 6, 2024
67bafa3
refactor: typage et structure de aides-velo-generator
Shamzic Nov 7, 2024
de94b9a
fix: typage dateDeValeur du mock de test d'une Situation
Shamzic Nov 8, 2024
381f825
chore: update betagouv/aides-velo, package-lock.json & package.json
Shamzic Nov 27, 2024
0c17c8c
fix: ajoute nouvelles institutions manquantes + logos
Shamzic Nov 27, 2024
0ddcbfa
fix: limite description fixée à 450
Shamzic Nov 27, 2024
301f787
fix: màj taille max de la description des aides
Shamzic Dec 3, 2024
d2ece8c
feat: cache la checkbox de l'aide vélo adapté aux PMR
Shamzic Dec 3, 2024
af825b6
refactor: ordre dépendances
Shamzic Dec 3, 2024
ef4a028
feat: ajoute les informations du type de vélo dans le label de l'aide…
Shamzic Dec 3, 2024
241728a
refactor: filtre du label des aides vélo
Shamzic Dec 3, 2024
f76a32f
refactor: supprime l'occurence "Ville de" dans les titres des aides vélo
Shamzic Dec 9, 2024
f71fe41
chore: maj aides-velo
Shamzic Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ updates:
labels:
- dependencies
allow:
- dependency-name: "aides-velo"
- dependency-name: "@betagouv/aides-velo"
- package-ecosystem: "pip"
directory: "./openfisca"
schedule:
Expand Down
168 changes: 111 additions & 57 deletions data/benefits/aides-velo-generator.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,123 @@
import aidesVelo from "aides-velo"
import { VeloBenefit } from "../../data/types/benefits.d.js"
import { Institution } from "../../data/types/institutions.d.js"
const benefits = [...aidesVelo()]
import { AidesVeloEngine } from "@betagouv/aides-velo"
import { VeloBenefit, AidesVeloBenefit } from "../../data/types/benefits.d.js"
import {
Institution,
InstitutionsByType,
} from "../../data/types/institutions.d.js"

function generate_benefit_list(institutions: Institution[]): VeloBenefit[] {
const potentialInstitutions = {
const aidesVeloEngine = new AidesVeloEngine()
const aidesVelo: AidesVeloBenefit[] = aidesVeloEngine.getAllAidesIn()

const VELO_BENEFIT_DEFAULT_VALUES = {
prefix: "l'",
type: "float",
periodicite: "ponctuelle",
interestFlag: "_interetsAidesVelo",
}

function filterInstitutionsByType(
institutions: Institution[]
): InstitutionsByType {
return {
région: institutions.filter((i) => i.type === "region"),
département: institutions.filter((i) => i.type === "departement"),
epci: institutions.filter((i) => i.type === "epci"),
"code insee": institutions.filter((i) => i.type === "commune"),
}
}

function findAidesVeloBenefitMatchingInstitution(
benefit: AidesVeloBenefit,
institutionsByType: InstitutionsByType
): string | undefined {
const { collectivity } = benefit
if (!collectivity) return undefined

if (collectivity.kind === "pays") return "etat"

Shamzic marked this conversation as resolved.
Show resolved Hide resolved
const institutions = institutionsByType[collectivity.kind]
if (!institutions) return undefined

benefits.forEach((b: VeloBenefit) => {
if (b && b.collectivity) {
switch (b.collectivity.kind) {
case "pays": {
if (b.collectivity.value === "France") {
b.institution = "etat"
} else {
b.discard = true
}
break
}
case "région":
case "département":
case "code insee": {
const institutionList = potentialInstitutions[b.collectivity.kind]
b.institution = institutionList.find(
(i) => i.code_insee === b.collectivity.value
)?.slug
break
}
case "epci": {
const institutionList = potentialInstitutions[b.collectivity.kind]
b.institution = institutionList.find(
(i) => i.code_siren === b.collectivity.code
)?.slug
break
}
}
const matchingInstitution = institutions.find((institution) => {
if (collectivity.kind === "epci") {
return institution.code_siren === collectivity.code
}
return institution.code_insee === collectivity.value
})

return benefits
.filter((b: VeloBenefit) => !b.discard)
.map((b: VeloBenefit) => {
const description =
b.description && !b.description.match(/((\s\$)+|(^\$)+)\w+/)
? b.description
: `Aide à l'achat d'un vélo : ${b.title}`
return {
label: `Aide à l'achat d'un vélo : ${b.title}`,
description,
id: `aidesvelo_${b.id}`.replace(/[ .']+/g, "_"),
external_id: b.id,
collectivity: b.collectivity,
title: b.title,
institution: b.institution,
prefix: "l'",
type: "float",
periodicite: "ponctuelle",
link: b.url,
interestFlag: "_interetsAidesVelo",
}
})
return matchingInstitution?.slug
}

export default generate_benefit_list
function formatAidesVeloBenefitDescription(benefit: AidesVeloBenefit): string {
const hasValidDescription =
benefit.description && !benefit.description.match(/((\s\$)+|(^\$)+)\w+/)
return (
(hasValidDescription && benefit.description) ||
`Aide à l'achat d'un vélo : ${benefit.title}`
)
}

function buildVeloBenefitLabel(benefit: AidesVeloBenefit): string {
let title = "Aide à l'achat d'un vélo"

const veloTypes = [
"musculaire",
"mécanique",
"électrique",
"adapté",
"cargo",
"pliant",
]
const trottinetteTypes = ["trottinette", "trottinette électrique"]

let types = veloTypes.filter((type) => benefit.description?.includes(type))

if (types.length > 1) {
title += ` (${types.join(", ")})`
} else if (types.length === 1) {
title += ` ${types[0]}`
}

types = trottinetteTypes.filter((type) => benefit.description?.includes(type))

if (types.length > 0) {
title = title.replace("vélo électrique", "vélo")
title += ` ou d'une trottinette électrique`
}

if (benefit.title.includes("Ville de")) {
return title
}

return `${title} : ${benefit.title}`
}

function formatAidesVeloToVeloBenefit(
benefit: AidesVeloBenefit,
institutionsByType: InstitutionsByType
): VeloBenefit {
return {
...benefit,
...VELO_BENEFIT_DEFAULT_VALUES,
label: buildVeloBenefitLabel(benefit),
description: formatAidesVeloBenefitDescription(benefit),
id: `aidesvelo_${benefit.id}`.replace(/[ .']+/g, "_"),
external_id: benefit.id,
collectivity: benefit.collectivity,
title: benefit.title,
link: benefit.url,
institution: findAidesVeloBenefitMatchingInstitution(
benefit,
institutionsByType
),
}
}

export default function generateVeloBenefitList(
institutions: Institution[]
): VeloBenefit[] {
const institutionsByType = filterInstitutionsByType(institutions)
return aidesVelo.map((benefit: AidesVeloBenefit) =>
formatAidesVeloToVeloBenefit(benefit, institutionsByType)
)
}
5 changes: 5 additions & 0 deletions data/institutions/ca_du_centre_littoral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté d'agglomération du Centre Littoral
imgSrc: img/institutions/logo_ca_du_centre_littoral.png
prefix: de la
type: epci
code_siren: "249730045"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté de communes du Crestois et de Pays de Saillans Coeur de Drôme
imgSrc: img/institutions/logo_cc_du_crestois_et_de_pays_de_saillans_coeur_de_drome.png
prefix: de la
type: epci
code_siren: "200040509"
5 changes: 5 additions & 0 deletions data/institutions/cc_du_haut_vallespir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté de communes du Haut Vallespir
imgSrc: img/institutions/logo_cc_du_haut_vallespir.png
prefix: de la
type: epci
code_siren: "246600548"
5 changes: 5 additions & 0 deletions data/institutions/cc_portes_de_sologne.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté de communes des Portes de Sologne
imgSrc: img/institutions/logo_cc_portes_de_sologne.png
prefix: de la
type: epci
code_siren: "200005932"
5 changes: 5 additions & 0 deletions data/institutions/cc_presquile_de_crozon_aulne_maritime.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté de communes Presqu'île de Crozon-Aulne maritime
imgSrc: img/institutions/logo_cc_presquile_de_crozon_aulne_maritime.png
prefix: de la
type: epci
code_siren: "200066868"
5 changes: 5 additions & 0 deletions data/institutions/cc_selestat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Communauté de communes de Sélestat
imgSrc: img/institutions/logo_cc_selestat.png
prefix: de la
type: epci
code_siren: "246700967"
6 changes: 6 additions & 0 deletions data/institutions/ville_avignon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Ville d'Avignon
imgSrc: img/institutions/logo_ville_avignon.png
type: commune
prefix: de la
code_insee: "84007"
code_siren: "218400075"
6 changes: 6 additions & 0 deletions data/institutions/ville_montmorillon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Ville de Montmorillon
imgSrc: img/institutions/logo_ville_montmorillon.png
type: commune
prefix: de la
code_insee: "86165"
code_siren: "218601656"
6 changes: 6 additions & 0 deletions data/institutions/ville_taillan_medoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Ville du Taillan-Médoc
imgSrc: img/institutions/logo_ville_taillan_medoc.png
type: commune
prefix: de la
code_insee: "33519"
code_siren: "213305196"
41 changes: 17 additions & 24 deletions data/types/benefits.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Institution } from "./institutions.d.js"

// extends "Aide" type defined in "aides-velo" package
type Aide = {
title: string
description: string
url: string
amount?: number
}
import {
Aide as AidesVelo,
AidesRulesNames as AidesVeloId,
} from "@betagouv/aides-velo"

export interface Benefit {
id: string
Expand All @@ -16,24 +12,21 @@ export interface Benefit {
mock?: boolean
}

export interface VeloBenefit extends Aide {
id: string
url?: string
collectivity: {
kind: string
value: string
code?: string
}
institution?: string

discard?: boolean

source?: string
export interface AidesVeloBenefit extends AidesVelo {
amount?: number
}

export interface VeloBenefit extends AidesVeloBenefit {
id: string | AidesVeloId
label: string
institution?: string
external_id: string
type: string
periodicite: string
prefix: string
interestFlag: string
link?: string
external_id?: string
type?: string
periodicite?: string
source?: string
}

export interface CovoiturageBenefit {
Expand Down
3 changes: 3 additions & 0 deletions data/types/institutions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ export interface Institution extends InstitutionRaw {
export interface InstitutionsMap {
[key: string]: Institution
}
export interface InstitutionsByType {
[key: string]: Institution[]
}
Loading
Loading