Skip to content

Commit

Permalink
refactor(insee): extract insee connector to individual pkg (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Jan 7, 2025
1 parent c406e75 commit ebd23fb
Show file tree
Hide file tree
Showing 49 changed files with 816 additions and 432 deletions.
7 changes: 7 additions & 0 deletions .changeset/hip-houses-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@gouvfr-lasuite/proconnect.insee": minor
---

♻️ Prélevement d'un partie du connecteur INSEE

Dans le cadres la migration du script d'import de comptes coop, une partie de l'API INSEE est déplacées dans le package `@gouvfr-lasuite/proconnect.insee` pour permettre leur réutilisation dans Hyyypertool.
4 changes: 2 additions & 2 deletions .github/workflows/end-to-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ jobs:
with:
cache: "npm"
node-version-file: package.json
- run: npm ci --include=dev
- run: npm ci
- run: npm run build:workspaces
- run: npm run migrate up
- run: npm run fixtures:load-ci -- cypress/e2e/${{ matrix.e2e_test }}/fixtures.sql
- run: npm run update-organization-info -- 500
- run: npm run build:workspaces
- name: Cypress run
uses: cypress-io/[email protected]
with:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ jobs:
with:
cache: "npm"
node-version-file: package.json
- run: npm ci --omit=dev # omit dev dependencies to simulate deployed environment
- run: npm ci
env:
CYPRESS_INSTALL_BINARY: 0
- run: npm run build:workspaces
- run: npm run migrate up
- run: npm run fixtures:load-ci -- scripts/fixtures.sql
- run: npm run update-organization-info -- 500
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ jobs:
cache: "npm"
node-version-file: package.json

- run: npm ci --include=dev
- run: npm ci
env:
CYPRESS_INSTALL_BINARY: 0

- run: npm run build:workspaces

- name: Create Release Pull Request
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
with:
cache: "npm"
node-version-file: package.json
- run: CYPRESS_INSTALL_BINARY=0 npm ci --include=dev
- run: npm ci
env:
CYPRESS_INSTALL_BINARY: 0
- run: npm run build:workspaces
- run: npm run test:lint
- run: npm run test:workspaces
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ WORKDIR /app
FROM base AS prod-deps
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \
--mount=type=bind,source=packages/core/package.json,target=packages/core/package.json \
--mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \
--mount=type=bind,source=packages/insee/package.json,target=packages/insee/package.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev

FROM base AS build
ENV CYPRESS_INSTALL_BINARY=0
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \
--mount=type=bind,source=packages/core/package.json,target=packages/core/package.json \
--mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \
--mount=type=bind,source=packages/insee/package.json,target=packages/insee/package.json \
--mount=type=cache,target=/root/.npm \
npm ci
COPY tsconfig.json vite.config.mjs ./
Expand Down
31 changes: 27 additions & 4 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"watch:js": "tsc --watch --preserveWatchOutput",
"watch:node": "tsx --watch src/index.ts",
"watch:workspaces:core": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.core",
"watch:workspaces:email": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.email"
"watch:workspaces:email": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.email",
"watch:workspaces:insee": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.insee"
},
"prettier": {
"plugins": [
Expand All @@ -51,6 +52,7 @@
"@gouvfr-lasuite/crisp": "https://github.com/douglasduteil/crisp/releases/download/v1.6.1/douglasduteil-crisp-1.6.1.tgz",
"@gouvfr-lasuite/proconnect.core": "workspace:*",
"@gouvfr-lasuite/proconnect.email": "workspace:*",
"@gouvfr-lasuite/proconnect.insee": "workspace:*",
"@gouvfr/dsfr": "^1.12.1",
"@kitajs/html": "^4.2.4",
"@kitajs/ts-html-plugin": "^4.1.0",
Expand Down Expand Up @@ -130,7 +132,7 @@
"cypress-axe": "^1.5.0",
"cypress-maildev": "^1.3.2",
"mocha": "^11.0.1",
"nock": "^13.5.4",
"nock": "^13.5.6",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0"
},
Expand Down
63 changes: 63 additions & 0 deletions packages/insee/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@gouvfr-lasuite/proconnect.insee",
"version": "0.2.0",
"homepage": "https://github.com/numerique-gouv/moncomptepro/tree/master/packages/insee#readme",
"bugs": "https://github.com/numerique-gouv/moncomptepro/issues",
"repository": {
"type": "git",
"url": "git+https://github.com/numerique-gouv/moncomptepro.git",
"directory": "packages/insee"
},
"license": "MIT",
"sideEffects": false,
"type": "module",
"imports": {
"#src/*": {
"types": "./dist/*/index.d.ts",
"default": "./dist/*/index.js"
}
},
"exports": {
"./*": {
"require": {
"types": "./dist/*/index.d.ts",
"default": "./dist/*/index.js"
},
"import": {
"types": "./dist/*/index.d.ts",
"default": "./dist/*/index.js"
},
"types": "./dist/*/index.d.ts",
"default": "./dist/*/index.js"
}
},
"scripts": {
"build": "tsc --project tsconfig.lib.json",
"check": "npm run build -- --noEmit",
"dev": "npm run build -- --watch --preserveWatchOutput",
"test": "mocha"
},
"mocha": {
"reporter": "spec",
"require": [
"tsx"
],
"spec": "src/**/*.test.ts"
},
"dependencies": {
"axios": "^1.7.7"
},
"devDependencies": {
"@tsconfig/node22": "^22.0.0",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"chai": "^5.1.2",
"mocha": "^11.0.1",
"nock": "^13.5.6",
"tsx": "^4.19.2"
},
"publishConfig": {
"access": "public",
"provenance": true
}
}
25 changes: 25 additions & 0 deletions packages/insee/src/api/find-by-siren.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//

import { expect } from "chai";
import { describe, it } from "mocha";
import nock from "nock";
import { findBySirenFactory } from "./find-by-siren.js";

//

const findBySiren = findBySirenFactory({
getInseeAccessToken: async () => "SECRET_INSEE_TOKEN",
});

describe("findBySiren", () => {
it("should return an establishment", async () => {
nock("https://api.insee.fr")
.get(
"/entreprises/sirene/siret?q=siren:200071843 AND etablissementSiege:true",
)
.reply(200, { etablissements: [{ siren: "🦄" }] });

const establishment = await findBySiren("200071843");
expect(establishment).to.be.deep.equal({ siren: "🦄" });
});
});
49 changes: 49 additions & 0 deletions packages/insee/src/api/find-by-siren.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//

import type { GetInseeAccessTokenHandler } from "#src/api";
import { InvalidSirenError } from "#src/errors";
import type { InseeEtablissement } from "#src/types";
import axios, { type AxiosRequestConfig, type AxiosResponse } from "axios";

//

type FactoryDependencies = {
getInseeAccessToken: GetInseeAccessTokenHandler;
config?: AxiosRequestConfig;
};

type EtablissementSearchResponse = {
header: {
total: number;
debut: number;
nombre: number;
};
etablissements: InseeEtablissement[];
};

export function findBySirenFactory({
getInseeAccessToken,
config,
}: FactoryDependencies) {
return async function findBySiren(siren: string) {
const token = await getInseeAccessToken();
const { data }: AxiosResponse<EtablissementSearchResponse> =
await axios.get(
`https://api.insee.fr/entreprises/sirene/siret?q=siren:${siren} AND etablissementSiege:true`,
{
headers: { Authorization: `Bearer ${token}` },
...config,
},
);

const establishment = data.etablissements.at(0);

if (!establishment) {
throw new InvalidSirenError();
}

return establishment;
};
}

export type FindBySirenHandler = ReturnType<typeof findBySirenFactory>;
23 changes: 23 additions & 0 deletions packages/insee/src/api/find-by-siret.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//

import { expect } from "chai";
import { describe, it } from "mocha";
import nock from "nock";
import { findBySiretFactory } from "./find-by-siret.js";

//

const findBySiret = findBySiretFactory({
getInseeAccessToken: async () => "SECRET_INSEE_TOKEN",
});

describe("findBySiret", () => {
it("should return an establishment", async () => {
nock("https://api.insee.fr")
.get("/entreprises/sirene/siret/20007184300060")
.reply(200, { etablissement: { siren: "🦄" } });

const establishment = await findBySiret("20007184300060");
expect(establishment).to.be.deep.equal({ siren: "🦄" });
});
});
37 changes: 37 additions & 0 deletions packages/insee/src/api/find-by-siret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//

import type { GetInseeAccessTokenHandler } from "#src/api";
import type { InseeEtablissement } from "#src/types";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
import axios from "axios";

//

type FactoryDependencies = {
getInseeAccessToken: GetInseeAccessTokenHandler;
config?: AxiosRequestConfig;
};
type EtablissementSearchBySiretResponse = {
etablissement: InseeEtablissement;
};

export function findBySiretFactory({
getInseeAccessToken,
config,
}: FactoryDependencies) {
return async function findBySiret(siret: string) {
const token = await getInseeAccessToken();
const { data }: AxiosResponse<EtablissementSearchBySiretResponse> =
await axios.get(
`https://api.insee.fr/entreprises/sirene/siret/${siret}`,
{
headers: { Authorization: `Bearer ${token}` },
...config,
},
);

return data.etablissement;
};
}

export type FindBySiretHandler = ReturnType<typeof findBySiretFactory>;
Loading

0 comments on commit ebd23fb

Please sign in to comment.