Skip to content

Commit

Permalink
feat(api): add onlyWithConvention flag (#53)
Browse files Browse the repository at this point in the history
* feat(api): add onlyWithConvention flag

* tests: update tests and snaps

* chore(ci): add github tests
  • Loading branch information
Julien Bouquillon authored Jun 9, 2021
1 parent 533eac3 commit 530ae5d
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 64 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
push:
branches: ["*"]
pull_request:
branches: ["*"]

name: Tests
jobs:
tests:
runs-on: ubuntu-latest
name: Tests
steps:
- uses: actions/checkout@v2
- shell: bash
env:
ELASTICSEARCH_URL: ${{ secrets.ELASTICSEARCH_URL }}
ELASTICSEARCH_API_KEY: ${{ secrets.ELASTICSEARCH_API_KEY }}
run: |
cd api
yarn
yarn test
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"typescript": "^4.1.5"
},
"jest": {
"testTimeout": 3000,
"testTimeout": 10000,
"roots": [
"<rootDir>/src"
],
Expand Down
46 changes: 15 additions & 31 deletions api/src/__tests__/__snapshots__/api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`Test entreprise search correct siren 1`] = `
Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques",
"activitePrincipale": "Traitement de données, hébergement et activités connexes",
"conventions": Array [
Object {
"etat": "VIGUEUR_ETEN",
Expand All @@ -15,23 +15,17 @@ Object {
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597",
},
Object {
"etat": "VIGUEUR_ETEN",
"id": "KALICONT000005635173",
"idcc": 1486,
"mtime": 1556652289,
"shortTitle": "Bureaux d'études techniques, cabinets d'ingénieurs-conseils et sociétés de conseils",
"texte_de_base": "KALITEXT000005679895",
"title": "Convention collective nationale des bureaux d'études techniques, des cabinets d'ingénieurs-conseils et des sociétés de conseils du 15 décembre 1987. ",
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635173",
"idcc": 9999,
"shortTitle": "",
},
],
"etablissements": 28,
"etablissements": 36,
"highlightLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 26,
"matchingEtablissement": Object {
"address": "16 Rue de Toutlemonde 49300 Cholet",
"siret": "85520050700710",
"address": "31 Rue de Cuire 69004 Lyon",
"siret": "85520050703151",
},
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507",
Expand All @@ -40,8 +34,8 @@ Object {

exports[`Test etablissement search correct siret 1`] = `
Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques",
"address": "16 Rue de Toutlemonde 49300 Cholet",
"activitePrincipale": "Traitement de données, hébergement et activités connexes",
"address": "107 Rue Servient 69003 Lyon",
"convention": Object {
"etat": "VIGUEUR_ETEN",
"id": "KALICONT000005635597",
Expand All @@ -52,19 +46,19 @@ Object {
"title": "Convention collective nationale du caoutchouc du 6 mars 1953. Étendue par arrêté du 29 mai 1969 JORF 18 juin 1969 ",
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597",
},
"etablissements": 28,
"etablissements": 36,
"highlightLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 1,
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507",
"siret": "85520050700710",
"siret": "85520050703169",
}
`;

exports[`Test search generic search 1`] = `
Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques",
"activitePrincipale": "Traitement de données, hébergement et activités connexes",
"conventions": Array [
Object {
"etat": "VIGUEUR_ETEN",
Expand All @@ -76,24 +70,14 @@ Object {
"title": "Convention collective nationale du caoutchouc du 6 mars 1953. Étendue par arrêté du 29 mai 1969 JORF 18 juin 1969 ",
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597",
},
Object {
"etat": "VIGUEUR_ETEN",
"id": "KALICONT000005635173",
"idcc": 1486,
"mtime": 1556652289,
"shortTitle": "Bureaux d'études techniques, cabinets d'ingénieurs-conseils et sociétés de conseils",
"texte_de_base": "KALITEXT000005679895",
"title": "Convention collective nationale des bureaux d'études techniques, des cabinets d'ingénieurs-conseils et des sociétés de conseils du 15 décembre 1987. ",
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635173",
},
],
"etablissements": 28,
"etablissements": 36,
"highlightLabel": "MANUF FRANC PNEUMATIQ <b><u>MICHELIN</b></u>",
"label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 18,
"matching": 14,
"matchingEtablissement": Object {
"address": "16 Rue de Toutlemonde 49300 Cholet",
"siret": "85520050700710",
"address": "107 Rue Servient 69003 Lyon",
"siret": "85520050703169",
},
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507",
Expand Down
8 changes: 5 additions & 3 deletions api/src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ const searchCall = (
const addressQP = address ? `&a=${address}` : "";
const limitQP = limit ? `&l=${limit}` : "";

return apptest.get(`${API_PREFIX}/search?q=${query}${addressQP}${limitQP}`);
return apptest.get(
`${API_PREFIX}/search?onlyWithConvention=true&q=${query}${addressQP}${limitQP}`
);
};

const michelinSiren = "855200507";
const michelinSiret = `${michelinSiren}00710`;
const michelinSiret = `${michelinSiren}03169`;

describe("Test search", () => {
test("generic search", async () => {
Expand All @@ -47,7 +49,7 @@ describe("Test search", () => {
const { body: b1 } = await searchCall("michelin", undefined, undefined);
expect(
b1.entreprises[0].matchingEtablissement.address
).toMatchInlineSnapshot(`"16 Rue de Toutlemonde 49300 Cholet"`);
).toMatchInlineSnapshot(`"107 Rue Servient 69003 Lyon"`);

const { body: b2 } = await searchCall("michelin", "63 000", undefined);
expect(
Expand Down
24 changes: 16 additions & 8 deletions api/src/elastic/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,23 @@ const addressFilter = (address: string | undefined) =>
]
: [{ match_all: {} }];

export const entrepriseSearchBody = (
query: string,
address: string | undefined,
export type SearchArgs = {
query: string;
address?: string | undefined;
// return convention of every etablissements associated to the main company
addAllConventions: boolean,
addAllConventions?: boolean;
// only search for etablissements with convention attached
onlyWithConvention: boolean,
size: number | undefined = defaultLimit
) => ({
onlyWithConvention?: boolean;
limit?: number | undefined;
};

export const entrepriseSearchBody = ({
query,
address,
addAllConventions = true,
onlyWithConvention = true,
limit = defaultLimit,
}: SearchArgs) => ({
collapse: collapse(addAllConventions),
highlight: {
fields: {
Expand Down Expand Up @@ -189,5 +197,5 @@ export const entrepriseSearchBody = (
],
},
},
size: size ? size : defaultLimit,
size: limit ? limit : defaultLimit,
});
37 changes: 24 additions & 13 deletions api/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { elasticsearchClient, ELASTICSEARCH_INDEX } from "../elastic";
import { entrepriseSearchBody, mapHit } from "../elastic/queries";

export const search = async (
query: string,
address: string | undefined,
limit: number | undefined,
addAllConventions: boolean,
onlyWithConvention: boolean
) => {
const body = entrepriseSearchBody(
import type { SearchArgs } from "../elastic/queries";

export const search = async ({
query,
address,
addAllConventions,
onlyWithConvention,
limit,
}: SearchArgs) => {
const body = entrepriseSearchBody({
query,
address,
addAllConventions,
onlyWithConvention,
limit
);
limit,
});

const response = await elasticsearchClient.search({
body,
Expand All @@ -31,7 +32,12 @@ export const search = async (
};

export const searchEntreprise = async (siren: string) => {
const body = entrepriseSearchBody(siren, undefined, true, false, 1);
const body = entrepriseSearchBody({
query: siren,
addAllConventions: true,
onlyWithConvention: false,
limit: 1,
});

const response = await elasticsearchClient.search({
body,
Expand All @@ -48,7 +54,12 @@ export const searchEntreprise = async (siren: string) => {
};

export const searchEtablissement = async (siret: string) => {
const body = entrepriseSearchBody(siret, undefined, false, false, 1);
const body = entrepriseSearchBody({
query: siret,
addAllConventions: false,
onlyWithConvention: false,
limit: 1,
});

const response = await elasticsearchClient.search({
body,
Expand Down
16 changes: 8 additions & 8 deletions api/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ export const router = new Router();
export const API_PREFIX = "/api/v1";

router.get(`${API_PREFIX}/search`, async (ctx) => {
const { q: query, a: address, l: limit } = ctx.query;
const { q: query, a: address, l: limit, onlyWithConvention } = ctx.query;

if (!query) {
ctx.throw(400, `query parameter q is required`);
}

try {
const entreprises = await search(
query as string,
address as string,
parseInt(limit as string),
true,
true
);
const entreprises = await search({
query: query as string,
address: address as string,
limit: parseInt(limit as string),
onlyWithConvention: !!onlyWithConvention,
addAllConventions: true
});
ctx.body = { entreprises };
} catch (err) {
console.log(JSON.stringify(err));
Expand Down

0 comments on commit 530ae5d

Please sign in to comment.