Skip to content

Commit

Permalink
Migration typescript (2) (#3935)
Browse files Browse the repository at this point in the history
* update: complète et utilise situationLayout

* update: ajoute un type au générateur d'aide

* update: ajoute un enum pour les types de vélos

* update: utilise l'interface de générateur

* update: remplace le type de vélos

* update: typage des droits du store

* update: utilise les nouvelles interfaces dans compute

* update: ajoute un enum et le typage du patrimoine

* trigger ci

* chore: infère le type situation layout

* update: renomme l'enum VeloType en VeloCategory

* update: renomme l'interface IDataGenerator en BenefitCatalog

* update: utilise l'interface résultats plutôt qu'un type inféré
  • Loading branch information
Cugniere authored Sep 1, 2023
1 parent 266a0ac commit 3df4b0c
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 41 deletions.
3 changes: 2 additions & 1 deletion backend/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import pollResult from "./lib/mattermost-bot/poll-result.js"
import { generateSituation } from "../lib/situations.js"
import { computeAides } from "../lib/benefits/compute.js"
import benefits from "../data/all.js"
import { situationsLayout } from "../lib/types/situations.js"

const computeBenefits = computeAides.bind(benefits)

Expand Down Expand Up @@ -58,7 +59,7 @@ function mock(app: express.Application) {
app.get("/api/simulation/:id/results", function (req, res, next) {
try {
const simulation = cache[req.params.id]
const situation = generateSituation(simulation)
const situation = generateSituation(simulation) as situationsLayout
sendToOpenfisca(situation, async function (err, result) {
if (err) {
return next(err)
Expand Down
13 changes: 7 additions & 6 deletions data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
InstitutionsMap,
} from "./types/institutions.d.js"
import { StandardBenefit, BenefitsMap } from "./types/benefits.d.js"
import { BenefitCatalog } from "./types/generator.d.js"

function generateInstitutionId(institution: InstitutionRawLayout) {
return `${institution.type}_${
institution.code_insee || institution.code_siren || institution.slug
}`
}

function generateBenefitId(benefit) {
function generateBenefitId(benefit: StandardBenefit) {
return benefit.id || benefit.slug
}

Expand Down Expand Up @@ -45,7 +46,7 @@ function transformInstitutions(
}, {})
}

function setTop(benefit, institution: InstitutionRawLayout) {
function setTop(benefit: StandardBenefit, institution: InstitutionRawLayout) {
const default_top =
institution.top ||
(institution.type === "national"
Expand All @@ -70,10 +71,10 @@ function setDefaults(
export function generate(
collections: JamstackLayout["collections"],
additionalBenefitAttributes,
aidesVeloBenefitListGenerator?,
fslGenerator?,
apaGenerator?
) {
aidesVeloBenefitListGenerator?: typeof aidesVeloGenerator,
fslGenerator?: typeof buildFSL,
apaGenerator?: typeof buildAPA
): BenefitCatalog {
const institutions = transformInstitutions(collections.institutions.items)
collections.benefits_javascript.items.forEach((benefit) => {
benefit.source = "javascript"
Expand Down
8 changes: 8 additions & 0 deletions data/types/generator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { InstitutionsMap } from "./institutions.d.js"
import { StandardBenefit, BenefitsMap } from "./benefits.d.js"

export interface BenefitCatalog {
all: StandardBenefit[]
institutionsMap: InstitutionsMap
benefitsMap: BenefitsMap
}
13 changes: 7 additions & 6 deletions lib/benefits/compute-aides-velo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import aidesVelo from "aides-velo"
import { generator } from "../dates.js"
import { VeloCategory } from "../enums/velo.js"

const veloTypes = {
velo_mecanique: "mécanique simple",
velo_electrique: "électrique",
velo_cargo: "cargo",
velo_cargo_electrique: "cargo électrique",
velo_pliant: "pliant",
velo_motorisation: "motorisation",
[VeloCategory.veloMecanique]: "mécanique simple",
[VeloCategory.veloElectrique]: "électrique",
[VeloCategory.veloCargo]: "cargo",
[VeloCategory.veloCargoElectrique]: "cargo électrique",
[VeloCategory.veloPliant]: "pliant",
[VeloCategory.veloMotorisation]: "motorisation",
}

function canComputeAidesVeloBenefits(situation) {
Expand Down
3 changes: 2 additions & 1 deletion lib/benefits/compute-javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ActiviteType } from "../enums/activite.js"
import { ScolariteType } from "../enums/scolarite.js"
import { situationsLayout } from "../types/situations.js"
import { ConditionsLayout } from "../types/benefits.js"
import { BenefitCatalog } from "../../data/types/generator.d.js"

const testRSARecipient = ({ openfiscaResponse, periods }): boolean => {
const rsa = openfiscaResponse.familles._.rsa[periods.thisMonth.id]
Expand Down Expand Up @@ -294,7 +295,7 @@ export function testProfileEligibility(benefit, data) {
}

export function computeJavascriptBenefits(
benefits,
benefits: BenefitCatalog,
situation: situationsLayout,
openfiscaResponse
) {
Expand Down
19 changes: 14 additions & 5 deletions lib/benefits/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { merge, sortBy, assign, sumBy, some, filter } from "lodash-es"
import determineCustomizationIds from "./customization.js"
import { computeJavascriptBenefits } from "./compute-javascript.js"
import { computeAidesVeloBenefits } from "./compute-aides-velo.js"
import { situationsLayout } from "../../lib/types/situations.d.js"
import { BenefitCatalog } from "../../data/types/generator.d.js"
import { Resultats } from "@lib/types/store.js"

import { generator } from "../dates.js"
export const datesGenerator = generator
Expand Down Expand Up @@ -55,22 +58,28 @@ export function round(amount, aide) {
}
}

export function computeAides(situation, id, openfiscaResponse, showPrivate?) {
export function computeAides(
this: BenefitCatalog,
situation: situationsLayout,
id: string,
openfiscaResponse,
showPrivate?: boolean
) {
const periods = generator(situation.dateDeValeur)

computeJavascriptBenefits(this, situation, openfiscaResponse)

const customizationIds = determineCustomizationIds(situation)
const computedRessources = normalizeOpenfiscaRessources(openfiscaResponse)

const result = {
const result: Resultats = {
droitsEligibles: [],
droitsInjectes: [], // declared by the user
_id: undefined,
}

const individus = filter(
[].concat(
([] as any).concat(
situation.demandeur,
situation.conjoint,
...(situation.enfants || [])
Expand All @@ -91,7 +100,7 @@ export function computeAides(situation, id, openfiscaResponse, showPrivate?) {
}) ||
valueAt(benefit.id, situation.famille, period) !== undefined
) {
return result.droitsInjectes.push(
return result.droitsInjectes!.push(
// @ts-ignore
assign({}, benefit, {
montant: sumBy(individus, (i) =>
Expand Down Expand Up @@ -124,7 +133,7 @@ export function computeAides(situation, id, openfiscaResponse, showPrivate?) {
}
: benefit.institution

result.droitsEligibles.push(
result.droitsEligibles!.push(
// @ts-ignore
assign({}, benefit, customization, {
instructions:
Expand Down
10 changes: 10 additions & 0 deletions lib/enums/patrimoine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum PatrimoineTypes {
valeur_patrimoine_loue = "valeur_patrimoine_loue",
valeur_terrains_non_loues = "valeur_terrains_non_loues",
valeur_locative_terrains_non_loues = "valeur_locative_terrains_non_loues",
valeur_immo_non_loue = "valeur_immo_non_loue",
valeur_locative_immo_non_loue = "valeur_locative_immo_non_loue",
livret_a = "livret_a",
epargne_revenus_non_imposables = "epargne_revenus_non_imposables",
epargne_revenus_imposables = "epargne_revenus_imposables",
}
8 changes: 8 additions & 0 deletions lib/enums/velo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum VeloCategory {
veloMecanique = "velo_mecanique",
veloElectrique = "velo_electrique",
veloCargo = "velo_cargo",
veloCargoElectrique = "velo_cargo_electrique",
veloPliant = "velo_pliant",
veloMotorisation = "velo_motorisation",
}
19 changes: 13 additions & 6 deletions lib/properties/individu-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getAnswer } from "../answers.js"

import { ActiviteType } from "../enums/activite.js"
import { EtudiantType, ScolariteType } from "../enums/scolarite.js"
import { VeloCategory } from "../enums/velo.js"

export default {
aah_restriction_substantielle_durable_acces_emploi: new BooleanProperty({
Expand Down Expand Up @@ -618,12 +619,18 @@ export default {
_interetsAidesVelo: new MultipleProperty({
question: "Souhaitez-vous connaître les aides pour acheter un vélo ?",
items: [
{ value: "velo_mecanique", label: "Vélo mécanique simple" },
{ value: "velo_electrique", label: "Vélo électrique" },
{ value: "velo_cargo", label: "Vélo cargo" },
{ value: "velo_cargo_electrique", label: "Vélo cargo électrique" },
{ value: "velo_pliant", label: "Vélo pliant" },
{ value: "velo_motorisation", label: "Motorisation d'un vélo classique" },
{ value: VeloCategory.veloMecanique, label: "Vélo mécanique simple" },
{ value: VeloCategory.veloElectrique, label: "Vélo électrique" },
{ value: VeloCategory.veloCargo, label: "Vélo cargo" },
{
value: VeloCategory.veloCargoElectrique,
label: "Vélo cargo électrique",
},
{ value: VeloCategory.veloPliant, label: "Vélo pliant" },
{
value: VeloCategory.veloMotorisation,
label: "Motorisation d'un vélo classique",
},
],
}),

Expand Down
17 changes: 9 additions & 8 deletions lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generator } from "./dates.js"
import { situationsLayout } from "../lib/types/situations.js"
import { individuLayout } from "../lib/types/individu.js"
import { ActiviteType } from "./enums/activite.js"
import { PatrimoineTypes } from "./enums/patrimoine.js"
import { resourceLayout, resourceCategoryLayout } from "./types/resources.js"

export const ressourceCategories: resourceCategoryLayout[] = [
Expand Down Expand Up @@ -463,35 +464,35 @@ export const categoriesRnc = [

export const patrimoineTypes = [
{
id: "valeur_patrimoine_loue",
id: PatrimoineTypes.valeur_patrimoine_loue,
label: "Valeur de vos biens loués",
},
{
id: "valeur_terrains_non_loues",
id: PatrimoineTypes.valeur_terrains_non_loues,
label: "Valeur de vos terrains non loués",
},
{
id: "valeur_locative_terrains_non_loues",
id: PatrimoineTypes.valeur_locative_terrains_non_loues,
label: "Valeur locative terrains non loués",
},
{
id: "valeur_immo_non_loue",
id: PatrimoineTypes.valeur_immo_non_loue,
label: "Valeur de vos biens immobiliers non loués",
},
{
id: "valeur_locative_immo_non_loue",
id: PatrimoineTypes.valeur_locative_immo_non_loue,
label: "Valeur locative immobilier non loué",
},
{
id: "livret_a",
id: PatrimoineTypes.livret_a,
label: "Épargne sur livret A",
},
{
id: "epargne_revenus_non_imposables",
id: PatrimoineTypes.epargne_revenus_non_imposables,
label: "Épargne aux revenus non imposables",
},
{
id: "epargne_revenus_imposables",
id: PatrimoineTypes.epargne_revenus_imposables,
label: "Épargne aux revenus imposables",
},
]
Expand Down
2 changes: 2 additions & 0 deletions lib/types/individu.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VeloCategory } from "../enums/velo.js"
export interface individuLayout {
id: string
annee_etude?: string
Expand All @@ -16,6 +17,7 @@ export interface individuLayout {
mention_baccalaureat?: string
handicap?: boolean
taux_incapacite?: number
_interetsAidesVelo?: VeloCategory[]
}

export interface individuGeneratorLayout {
Expand Down
1 change: 1 addition & 0 deletions lib/types/situations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface situationsMenageLayout {
_primoAccedant?: boolean | any
_region?: string
statut_occupation_logement?: string
_codePostal?: string
}
9 changes: 6 additions & 3 deletions lib/types/store.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { StandardBenefit } from "@data/types/benefits.d.js"
import { PatrimoineTypes } from "@lib/enums/patrimoine.js"

export interface Resultats {
_id?: string
droitsEligibles: [] | null
droitsInjectes: [] | null
droitsEligibles: StandardBenefit[] | null
droitsInjectes: StandardBenefit[] | null
}

export interface Calculs {
Expand All @@ -25,7 +28,7 @@ export interface Answer {
}

export interface Patrimoine {
[key: string]: any
[key: PatrimoineTypes]: number
}

export interface RessourcesFiscales {
Expand Down
3 changes: 2 additions & 1 deletion src/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Answer,
Calculs,
PersistedStore,
Patrimoine,
Resultats,
Simulation,
Situation,
Expand Down Expand Up @@ -302,7 +303,7 @@ export const useStore = defineStore("store", {
}
this.setDirty()
},
setPatrimoine(patrimoine: any) {
setPatrimoine(patrimoine: Patrimoine | undefined) {
this.simulation.patrimoine = patrimoine
this.setDirty()
},
Expand Down
8 changes: 5 additions & 3 deletions src/views/simulation/Ressources/patrimoine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ import YesNoQuestion from "@/components/yes-no-question.vue"
import InputNumber from "@/components/input-number.vue"
import ActionButtons from "@/components/action-buttons.vue"
import { useStore } from "@/stores/index.js"
import { PatrimoineTypes } from "@lib/enums/patrimoine.js"
import { Patrimoine } from "@lib/types/store.d.js"
const mapping = {
hasTerrainsNonLoues: {
Expand Down Expand Up @@ -266,7 +268,7 @@ export default {
},
methods: {
onSubmit() {
const values = {}
const values: Patrimoine = {}
patrimoineTypes.forEach((patrimoinType) => {
values[patrimoinType.id] =
Expand All @@ -280,8 +282,8 @@ export default {
)
if (this.hasBiensLoues) {
values.valeur_patrimoine_loue =
this.demandeur.valeur_patrimoine_loue[this.periodKey]
values[PatrimoineTypes.valeur_patrimoine_loue] =
this.demandeur[PatrimoineTypes.valeur_patrimoine_loue][this.periodKey]
}
this.store.setPatrimoine(values)
this.$router.push("/simulation/resultats")
Expand Down
3 changes: 2 additions & 1 deletion tools/evaluate-benefits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { computeAides } from "../lib/benefits/compute.js"
import benefits from "../data/all.js"
import("../backend/lib/mongo-connector.js")
import Simulation from "../backend/models/simulation.js"
import { situationsLayout } from "../lib/types/situations.js"

function main() {
const simulationId = process.argv[2]
Expand All @@ -17,7 +18,7 @@ function main() {
Simulation.findById(simulationId, (err, simulation) => {
if (err) return process.exit(1)

const situation = generateSituation(simulation)
const situation = generateSituation(simulation) as situationsLayout

calculate(situation, function (err, result) {
const openfiscaResponse = Object.assign(
Expand Down

0 comments on commit 3df4b0c

Please sign in to comment.