Skip to content

Commit

Permalink
feat: upgrade @betagouv/aides-velo and remove communes.json
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 31, 2024
1 parent 75da80a commit b27823e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 454,210 deletions.
11 changes: 4 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"kube:env": "eval $(minikube docker-env)"
},
"dependencies": {
"@betagouv/aides-velo": "^0.1.0-6",
"@betagouv/aides-velo": "^0.1.0-8",
"@getbrevo/brevo": "^1.0.1",
"@incubateur-ademe/nosgestesclimat": "^3.3.2",
"@nestjs/common": "^9.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/repository/aidesRetrofit.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Publicodes from 'publicodes';
import { Commune } from '@betagouv/aides-velo';

import rulesRetrofit from '../data/aidesRetrofit.json';
import localisations from '../../infrastructure/repository/commune/communes.json';
import { data as aidesVeloData } from '@betagouv/aides-velo';
import { AideVelo } from '../../domain/aides/aideVelo';

@Injectable()
Expand Down Expand Up @@ -64,7 +64,7 @@ async function aidesRetrofit(
}

function getLocalisationByCP(cp: string): Commune {
const lieux = localisations as Commune[];
const lieux = aidesVeloData.communes;
const lieu = lieux.find((lieu) => lieu.codesPostaux.includes(cp));
return lieu;
}
125 changes: 0 additions & 125 deletions src/infrastructure/repository/aidesVelo.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,128 +114,3 @@ export class AidesVeloRepository {
return veloTypes;
}
}

// function getAidesVeloParType(
// engine: Engine,
// situation: InputParameters = {},
// ): AidesVelo {
// engine.setSituation(formatInput(situation));
//
// //maximiser les aides
// const aides = Object.entries(aidesAndCollectivities)
// .filter(
// ([, { country: aideCountry }]) =>
// !situation['localisation . pays'] ||
// aideCountry === situation['localisation . pays'],
// )
// .flatMap(([ruleName]) => {
// try {
// const rule = engine.getRule(ruleName);
// const collectivity = aidesAndCollectivities[ruleName].collectivity;
//
// const metaData = {
// libelle: rule.title as string,
// description: rule.rawNode.description as string,
// lien: (rule.rawNode as any).lien as string,
// collectivite: collectivity as Collectivite,
// montant: null,
// plafond: null,
// logo: App.getAideVeloMiniaturesURL() + miniatures[ruleName],
// };
// if (!situation['vélo . type']) {
// return [metaData];
// }
// const { nodeValue } = engine.evaluate({ valeur: ruleName, unité: '€' });
//
// if (typeof nodeValue === 'number' && nodeValue > 0) {
// return [
// {
// ...metaData,
// description: formatDescription({
// ruleName,
// engine,
// veloCat: situation['vélo . type'],
// ville: 'votre ville',
// }),
// montant: nodeValue,
// plafond: nodeValue,
// },
// ];
// } else {
// return [];
// }
// } catch (error) {
// return [];
// }
// });
// return aides;
// }
//
// const formatInput = (input: InputParameters) => {
// const entries = Object.entries(input);
//
// const transformedEntries = entries.map(([key, val]) => {
// let transformedVal;
//
// if (typeof val === 'boolean') {
// transformedVal = val ? 'oui' : 'non';
// } else if (key === 'localisation . epci') {
// transformedVal = `'${epciSirenToName[val] || val}'`;
// } else if (typeof val === 'string') {
// transformedVal = `'${val}'`;
// } else {
// transformedVal = val;
// }
//
// return [key, transformedVal];
// });
//
// const transformedInput = Object.fromEntries(transformedEntries);
//
// return transformedInput;
// };
//
// const epciSirenToName = Object.fromEntries(
// Object.values(aidesAndCollectivities).flatMap(({ collectivity }) => {
// if (collectivity.kind !== 'epci') {
// return [];
// }
// return [[(collectivity as any).code, collectivity.value]];
// }),
// );
//
// // FIXME: should use the commune name to differentiate between the different
// // communes with the same postal code.
// function getLocalisationByCP(cp: string): Localisation | undefined {
// const lieux = localisations as Localisation[];
// // FIXME AIDE : sens fonctionel du premier match ?
// const lieu = lieux.find((lieu) => {
// return lieu.codesPostaux.includes(cp);
// });
// return lieu;
// }
//
// export function formatDescription({
// ruleName,
// engine,
// veloCat,
// ville,
// }): string {
// const { rawNode } = engine.getRule(ruleName);
// const description = rawNode?.description ?? '';
// const plafondRuleName = `${ruleName} . $plafond`;
// const plafondIsDefined = Object.keys(engine.getParsedRules()).includes(
// plafondRuleName,
// );
// const plafond = plafondIsDefined && engine.evaluate(plafondRuleName);
// return description
// .replace(
// /\$vélo/g,
// veloCat === 'motorisation' ? 'kit de motorisation' : `vélo ${veloCat}`,
// )
// .replace(
// /\$plafond/,
// formatValue(plafond?.nodeValue, { displayedUnit: '€' }),
// )
// .replace(/\$ville/, ville?.nom);
// }
20 changes: 16 additions & 4 deletions src/infrastructure/repository/commune/commune.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { Injectable } from '@nestjs/common';
import _codes_postaux from './codes_postaux.json';
import _communes from './communes.json';
import _epiccom from './epicom2024.json';
/**
* NOTE: Initially, a 'communes.json' file was used to store the commune data.
* It was copied from the mquendal/mesaidesvelo repository. However, as we
* created a dedicated package (@betagouv/aides-velo) to isolate the logic and
* generate the 'communes.json' file, we use it as a source of truth for the
* commune data.
*
* NOTE: We should consider at some point to create a standalone package for
* the commune data and use it as a source of truth for all the packages that
* need it with extra utilities to manipulate the data or decide to directly
* use the @etalab/decoupage-administratif package.
*/
import { data as aidesVeloData } from '@betagouv/aides-velo';

export type CommuneParCodePostal = {
INSEE: string;
Expand All @@ -17,7 +29,7 @@ export type Commune = {
region: string;
population: number;
zfe: boolean;
epci: string;
epci?: string;
codesPostaux: string[];
};

Expand Down Expand Up @@ -129,7 +141,7 @@ export class CommuneRepository {

let commune = this.getCommuneByCodeINSEE(liste[0].INSEE);
if (!commune) {
for (const commune_insee of _communes as Commune[]) {
for (const commune_insee of aidesVeloData.communes) {
if (commune_insee.codesPostaux.includes(code_postal)) {
commune = commune_insee;
break;
Expand All @@ -156,6 +168,6 @@ export class CommuneRepository {
}

private getCommuneByCodeINSEE(code_insee: string): Commune {
return (_communes as Commune[]).find((c) => c.code === code_insee);
return aidesVeloData.communes.find((c) => c.code === code_insee);
}
}
Loading

0 comments on commit b27823e

Please sign in to comment.