Skip to content

Commit

Permalink
refactor!: use the new @betagouv/publicodes-decoupage-administratif
Browse files Browse the repository at this point in the history
The API of the localisation is changed (in a breaking way)
  • Loading branch information
EmileRolley committed Jan 27, 2025
1 parent 076d7d5 commit f8a4521
Show file tree
Hide file tree
Showing 14 changed files with 774 additions and 751 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-crews-march.md

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
"release": "yarn compile && changeset publish"
},
"dependencies": {
"@betagouv/publicodes-decoupage-administratif": "^0.1.0",
"publicodes": "^1.5.1"
},
"devDependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^6.0.0",
"@changesets/cli": "^2.27.9",
"@etalab/decoupage-administratif": "^4.0.0",
"@publicodes/tools": "^1.3.0-2",
"@publicodes/tools": "https://pkg.pr.new/publicodes/tools/@publicodes/tools@86bbf76",
"@svitejs/changesets-changelog-github-compact": "^1.2.0",
"@types/jest": "^29.5.13",
"bun": "^1.1.32",
Expand All @@ -58,5 +58,6 @@
},
"publishConfig": {
"access": "public"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
2 changes: 1 addition & 1 deletion scripts/data-fetch/miniatures/aides-jeunes
Submodule aides-jeunes updated 136 files
111 changes: 0 additions & 111 deletions scripts/generate-aides-collectivities.js

This file was deleted.

110 changes: 110 additions & 0 deletions scripts/generate-aides-collectivities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Associates for each aid the corresponding collectivity.
*/

import fs from "node:fs";
import Publicodes from "publicodes";
import {
extractLocalisationFromAST,
Localisation as DecoupageAdministratifLocalisation,
} from "@betagouv/publicodes-decoupage-administratif";
import { getDataPath } from "./utils.js";

import rules, { RuleName } from "../publicodes-build/index.js";
import { Localisation } from "../src/data";

const engine = new Publicodes(rules);
const ruleNames = Object.keys(rules);

const AIDES_ETAT_FR: RuleName[] = [
"aides . bonus vélo",
"aides . prime à la conversion",
"aides . prime à la conversion . surprime ZFE",
];

const aidesRuleNames = ruleNames.filter((ruleName) => {
if (ruleName.startsWith("aides .")) {
if (!engine.getRule(ruleName as RuleName).rawNode.titre) {
if (ruleName.split(" . ").length === 2) {
console.warn(`No title for ${ruleName}`);
}
return false;
}
return true;
}
return false;
});

const res = Object.fromEntries(
aidesRuleNames.map((ruleName) => {
const rule = engine.getRule(ruleName as RuleName);
// TODO: should be handle more generically in the future.
if (ruleName === "aides . monaco") {
return [
ruleName,
{
collectivity: { kind: "pays", value: "Monaco" },
country: "monaco",
},
];
}
if (ruleName === "aides . luxembourg") {
return [
ruleName,
{
collectivity: { kind: "pays", value: "Luxembourg" },
country: "luxembourg",
},
];
}
if (AIDES_ETAT_FR.includes(ruleName as RuleName)) {
return [
ruleName,
{
collectivity: { kind: "pays", value: "France" },
country: "france",
},
];
}

// TODO: handle multiple localisations
const localisations = extractLocalisationFromAST(rule, "localisation");
if (localisations.length === 0) {
console.warn(`No localisation for ${ruleName}`);
return [ruleName, undefined];
}

return [
ruleName,
{
collectivity: {
kind: toKind(localisations[0].type),
value: localisations[0].valeur,
code: localisations[0].code,
},
country: "france",
},
];
})
);

fs.writeFileSync(getDataPath("aides-collectivities.json"), JSON.stringify(res));

/// Utils

function toKind(
type: DecoupageAdministratifLocalisation["type"]
): Localisation["collectivity"]["kind"] {
switch (type) {
case "commune":
return "code insee";
case "epci":
return "epci";
case "département":
return "département";
case "région":
return "région";
default:
throw new Error(`Unknown type: ${type}`);
}
}
2 changes: 1 addition & 1 deletion scripts/generate.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
await import("./generate-aides-collectivities.js");
await import("./generate-aides-collectivities.ts");
await import("./data-fetch/miniatures/extract-miniatures.js");
16 changes: 7 additions & 9 deletions src/lib/AidesVeloEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Engine, {
formatValue,
Situation as PublicodesSituation,
} from "publicodes";
import rules, { Questions, RuleName, Situation } from "../../publicodes-build";
import rules, { Inputs, RuleName, Situation } from "../../publicodes-build";
import {
AideRuleNames,
aidesAvecLocalisation,
Expand Down Expand Up @@ -58,7 +58,7 @@ const aidesAvecLocalisationEntries = Object.entries(
* inputs.
*/
export class AidesVeloEngine {
private inputs: Questions = {};
private inputs: Inputs = {};
private engine: Engine<RuleName>;

/**
Expand All @@ -83,7 +83,7 @@ export class AidesVeloEngine {
* format. For example, boolean values are represented as `true` or `false`
* instead of `oui` or `non` and the values are not wrapped in single quotes.
*/
public setInputs(inputs: Questions): this {
public setInputs(inputs: Inputs): this {
this.inputs = inputs;
this.engine.setSituation(
formatInputs(inputs) as PublicodesSituation<RuleName>
Expand Down Expand Up @@ -162,7 +162,7 @@ export class AidesVeloEngine {
description: this.formatDescription({
ruleName,
veloCat: this.engine.evaluate("vélo . type")
.nodeValue as Questions["vélo . type"],
.nodeValue as Inputs["vélo . type"],
ville: "votre ville",
}),
amount: nodeValue,
Expand Down Expand Up @@ -203,9 +203,7 @@ export class AidesVeloEngine {
* @returns The list of options or `undefined` if the question doesn't have
* any.
*/
public getOptions<T extends keyof Questions>(
name: T
): Questions[T][] | undefined {
public getOptions<T extends keyof Inputs>(name: T): Inputs[T][] | undefined {
return extractOptions(this.engine.getRule(name));
}

Expand All @@ -226,7 +224,7 @@ export class AidesVeloEngine {
ville,
}: {
ruleName: RuleName;
veloCat: Questions["vélo . type"];
veloCat: Inputs["vélo . type"];
ville: string;
}) {
const { rawNode } = this.engine.getRule(ruleName);
Expand Down Expand Up @@ -254,7 +252,7 @@ export class AidesVeloEngine {
}
}

function formatInputs(inputs: Questions): Partial<Situation> {
function formatInputs(inputs: Inputs): Partial<Situation> {
const entries = Object.entries(inputs);

const transformedEntries = entries
Expand Down
Loading

0 comments on commit f8a4521

Please sign in to comment.