Skip to content

Commit

Permalink
refactor(insee): extract insee connector to individual pkg (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Jan 7, 2025
1 parent ebd23fb commit 7e528cb
Show file tree
Hide file tree
Showing 48 changed files with 1,023 additions and 539 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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/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/identite/package.json,target=packages/identite/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
Expand All @@ -17,6 +18,7 @@ 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/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/identite/package.json,target=packages/identite/package.json \
--mount=type=bind,source=packages/insee/package.json,target=packages/insee/package.json \
--mount=type=cache,target=/root/.npm \
npm ci
Expand Down
62 changes: 56 additions & 6 deletions package-lock.json

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

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
},
"main": "src/index.js",
"workspaces": [
"packages/*"
"packages/core",
"packages/email",
"packages/insee",
"packages/identite"
],
"scripts": {
"build": "run-s build:**",
Expand Down Expand Up @@ -39,6 +42,7 @@
"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:identite": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.identite",
"watch:workspaces:insee": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.insee"
},
"prettier": {
Expand All @@ -52,6 +56,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.identite": "workspace:*",
"@gouvfr-lasuite/proconnect.insee": "workspace:*",
"@gouvfr/dsfr": "^1.12.1",
"@kitajs/html": "^4.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"scripts": {
"build": "tsc --project tsconfig.lib.json",
"build": "tsc --build tsconfig.lib.json",
"check": "npm run build -- --noEmit",
"dev": "npm run build -- --watch --preserveWatchOutput",
"test": "mocha"
Expand Down
2 changes: 1 addition & 1 deletion packages/email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"scripts": {
"build": "tsc --project ./tsconfig.lib.json",
"build": "tsc --build ./tsconfig.lib.json",
"dev": "npm run build -- --watch --preserveWatchOutput",
"storybook": "vite",
"test": "tsc --noEmit"
Expand Down
69 changes: 69 additions & 0 deletions packages/identite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@gouvfr-lasuite/proconnect.identite",
"version": "0.2.0",
"homepage": "https://github.com/numerique-gouv/moncomptepro/tree/master/packages/identite#readme",
"bugs": "https://github.com/numerique-gouv/moncomptepro/issues",
"repository": {
"type": "git",
"url": "git+https://github.com/numerique-gouv/moncomptepro.git",
"directory": "packages/identite"
},
"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 --build tsconfig.lib.json",
"clean": "rm -rf dist tsconfig*.tsbuildinfo",
"check": "npm run build -- --noEmit",
"dev": "npm run build -- --watch --preserveWatchOutput",
"test": "mocha"
},
"mocha": {
"reporter": "spec",
"require": [
"tsx"
],
"spec": "src/**/*.test.ts"
},
"dependencies": {
"sql-template-tag": "^5.2.1"
},
"devDependencies": {
"@electric-sql/pglite": "^0.2.15",
"@gouvfr-lasuite/proconnect.core": "^0.2.0",
"@gouvfr-lasuite/proconnect.insee": "^0.2.0",
"@tsconfig/node22": "^22.0.0",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"await-to-js": "^3.0.0",
"chai": "^5.1.2",
"mocha": "^11.0.1",
"node-pg-migrate": "^7.6.1",
"pg": "^8.13.0",
"tsx": "^4.19.2"
},
"publishConfig": {
"access": "public",
"provenance": true
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { InseeNotFoundError } from "@gouvfr-lasuite/proconnect.insee/errors";
import type { InseeEtablissement } from "@gouvfr-lasuite/proconnect.insee/types";
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
import nock from "nock";
import { InseeNotFoundError } from "../src/config/errors";
import { getOrganizationInfo } from "../src/connectors/api-sirene";
import diffusible from "./api-sirene-data/diffusible.json";
import partiallyNonDiffusible from "./api-sirene-data/partially-non-diffusible.json";
import searchBySiren from "./api-sirene-data/search-by-siren.json";
import diffusible from "./__mocks__/diffusible.json" with { type: "json" };
import partiallyNonDiffusible from "./__mocks__/partially-non-diffusible.json" with { type: "json" };
import searchBySiren from "./__mocks__/search-by-siren.json" with { type: "json" };
import { getOrganizationInfoFactory } from "./get-organization-info.js";

chai.use(chaiAsPromised);
const assert = chai.assert;
Expand Down Expand Up @@ -42,31 +43,39 @@ describe("getOrganizationInfo", () => {
};

it("should return valid payload for diffusible établissement", async () => {
nock("https://api.insee.fr")
.get("/entreprises/sirene/siret/20007184300060")
.reply(200, diffusible);
const getOrganizationInfo = getOrganizationInfoFactory({
findBySiren: () => Promise.reject(),
findBySiret: () =>
Promise.resolve(diffusible.etablissement as any as InseeEtablissement),
});
await assert.eventually.deepEqual(
getOrganizationInfo("20007184300060"),
diffusibleOrganizationInfo,
);
});

it("should return valid payload for diffusible établissement", async () => {
nock("https://api.insee.fr")
.get(
"/entreprises/sirene/siret?q=siren:200071843 AND etablissementSiege:true",
)
.reply(200, searchBySiren);
const getOrganizationInfo = getOrganizationInfoFactory({
findBySiren: () =>
Promise.resolve(
searchBySiren.etablissements[0] as any as InseeEtablissement,
),
findBySiret: () => Promise.reject(),
});
await assert.eventually.deepEqual(
getOrganizationInfo("200071843"),
diffusibleOrganizationInfo,
);
});

it("should show partial data for partially non diffusible établissement", async () => {
nock("https://api.insee.fr")
.get("/entreprises/sirene/siret/94957325700019")
.reply(200, partiallyNonDiffusible);
const getOrganizationInfo = getOrganizationInfoFactory({
findBySiren: () => Promise.reject(),
findBySiret: () =>
Promise.resolve(
partiallyNonDiffusible.etablissement as any as InseeEtablissement,
),
});

await assert.eventually.deepEqual(getOrganizationInfo("94957325700019"), {
siret: "94957325700019",
Expand All @@ -92,14 +101,13 @@ describe("getOrganizationInfo", () => {
});

it("should throw for totally non diffusible établissement", async () => {
nock("https://api.insee.fr")
.get("/entreprises/sirene/siret/53512638700013")
.reply(403, {
header: {
statut: 403,
message: "Établissement non diffusable (53512638700013)",
},
});
const getOrganizationInfo = getOrganizationInfoFactory({
findBySiren: () => Promise.reject(),
findBySiret: () =>
Promise.resolve({
statutDiffusionEtablissement: "N",
} as InseeEtablissement),
});
await assert.isRejected(
getOrganizationInfo("53512638700013"),
InseeNotFoundError,
Expand Down
Loading

0 comments on commit 7e528cb

Please sign in to comment.