Unverified Commit 530ae5d9 authored by Julien Bouquillon's avatar Julien Bouquillon 🐫 Committed by GitHub
Browse files

feat(api): add onlyWithConvention flag (#53)

* feat(api): add onlyWithConvention flag

* tests: update tests and snaps

* chore(ci): add github tests
parent 533eac3a
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
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
"typescript": "^4.1.5" "typescript": "^4.1.5"
}, },
"jest": { "jest": {
"testTimeout": 3000, "testTimeout": 10000,
"roots": [ "roots": [
"<rootDir>/src" "<rootDir>/src"
], ],
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
exports[`Test entreprise search correct siren 1`] = ` exports[`Test entreprise search correct siren 1`] = `
Object { Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques", "activitePrincipale": "Traitement de données, hébergement et activités connexes",
"conventions": Array [ "conventions": Array [
Object { Object {
"etat": "VIGUEUR_ETEN", "etat": "VIGUEUR_ETEN",
...@@ -15,23 +15,17 @@ Object { ...@@ -15,23 +15,17 @@ Object {
"url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597", "url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597",
}, },
Object { Object {
"etat": "VIGUEUR_ETEN", "idcc": 9999,
"id": "KALICONT000005635173", "shortTitle": "",
"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 MICHELIN", "highlightLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"label": "MANUF FRANC PNEUMATIQ MICHELIN", "label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 26, "matching": 26,
"matchingEtablissement": Object { "matchingEtablissement": Object {
"address": "16 Rue de Toutlemonde 49300 Cholet", "address": "31 Rue de Cuire 69004 Lyon",
"siret": "85520050700710", "siret": "85520050703151",
}, },
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN", "simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507", "siren": "855200507",
...@@ -40,8 +34,8 @@ Object { ...@@ -40,8 +34,8 @@ Object {
exports[`Test etablissement search correct siret 1`] = ` exports[`Test etablissement search correct siret 1`] = `
Object { Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques", "activitePrincipale": "Traitement de données, hébergement et activités connexes",
"address": "16 Rue de Toutlemonde 49300 Cholet", "address": "107 Rue Servient 69003 Lyon",
"convention": Object { "convention": Object {
"etat": "VIGUEUR_ETEN", "etat": "VIGUEUR_ETEN",
"id": "KALICONT000005635597", "id": "KALICONT000005635597",
...@@ -52,19 +46,19 @@ Object { ...@@ -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 ", "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", "url": "https://www.legifrance.gouv.fr/affichIDCC.do?idConvention=KALICONT000005635597",
}, },
"etablissements": 28, "etablissements": 36,
"highlightLabel": "MANUF FRANC PNEUMATIQ MICHELIN", "highlightLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"label": "MANUF FRANC PNEUMATIQ MICHELIN", "label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 1, "matching": 1,
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN", "simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507", "siren": "855200507",
"siret": "85520050700710", "siret": "85520050703169",
} }
`; `;
exports[`Test search generic search 1`] = ` exports[`Test search generic search 1`] = `
Object { Object {
"activitePrincipale": "Fabrication et rechapage de pneumatiques", "activitePrincipale": "Traitement de données, hébergement et activités connexes",
"conventions": Array [ "conventions": Array [
Object { Object {
"etat": "VIGUEUR_ETEN", "etat": "VIGUEUR_ETEN",
...@@ -76,24 +70,14 @@ Object { ...@@ -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 ", "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", "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>", "highlightLabel": "MANUF FRANC PNEUMATIQ <b><u>MICHELIN</b></u>",
"label": "MANUF FRANC PNEUMATIQ MICHELIN", "label": "MANUF FRANC PNEUMATIQ MICHELIN",
"matching": 18, "matching": 14,
"matchingEtablissement": Object { "matchingEtablissement": Object {
"address": "16 Rue de Toutlemonde 49300 Cholet", "address": "107 Rue Servient 69003 Lyon",
"siret": "85520050700710", "siret": "85520050703169",
}, },
"simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN", "simpleLabel": "MANUF FRANC PNEUMATIQ MICHELIN",
"siren": "855200507", "siren": "855200507",
......
...@@ -19,11 +19,13 @@ const searchCall = ( ...@@ -19,11 +19,13 @@ const searchCall = (
const addressQP = address ? `&a=${address}` : ""; const addressQP = address ? `&a=${address}` : "";
const limitQP = limit ? `&l=${limit}` : ""; 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 michelinSiren = "855200507";
const michelinSiret = `${michelinSiren}00710`; const michelinSiret = `${michelinSiren}03169`;
describe("Test search", () => { describe("Test search", () => {
test("generic search", async () => { test("generic search", async () => {
...@@ -47,7 +49,7 @@ describe("Test search", () => { ...@@ -47,7 +49,7 @@ describe("Test search", () => {
const { body: b1 } = await searchCall("michelin", undefined, undefined); const { body: b1 } = await searchCall("michelin", undefined, undefined);
expect( expect(
b1.entreprises[0].matchingEtablissement.address 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); const { body: b2 } = await searchCall("michelin", "63 000", undefined);
expect( expect(
......
...@@ -141,15 +141,23 @@ const addressFilter = (address: string | undefined) => ...@@ -141,15 +141,23 @@ const addressFilter = (address: string | undefined) =>
] ]
: [{ match_all: {} }]; : [{ match_all: {} }];
export const entrepriseSearchBody = ( export type SearchArgs = {
query: string, query: string;
address: string | undefined, address?: string | undefined;
// return convention of every etablissements associated to the main company // return convention of every etablissements associated to the main company
addAllConventions: boolean, addAllConventions?: boolean;
// only search for etablissements with convention attached // only search for etablissements with convention attached
onlyWithConvention: boolean, onlyWithConvention?: boolean;
size: number | undefined = defaultLimit limit?: number | undefined;
) => ({ };
export const entrepriseSearchBody = ({
query,
address,
addAllConventions = true,
onlyWithConvention = true,
limit = defaultLimit,
}: SearchArgs) => ({
collapse: collapse(addAllConventions), collapse: collapse(addAllConventions),
highlight: { highlight: {
fields: { fields: {
...@@ -189,5 +197,5 @@ export const entrepriseSearchBody = ( ...@@ -189,5 +197,5 @@ export const entrepriseSearchBody = (
], ],
}, },
}, },
size: size ? size : defaultLimit, size: limit ? limit : defaultLimit,
}); });
import { elasticsearchClient, ELASTICSEARCH_INDEX } from "../elastic"; import { elasticsearchClient, ELASTICSEARCH_INDEX } from "../elastic";
import { entrepriseSearchBody, mapHit } from "../elastic/queries"; import { entrepriseSearchBody, mapHit } from "../elastic/queries";
import type { SearchArgs } from "../elastic/queries";
export const search = async (
query: string, export const search = async ({
address: string | undefined, query,
limit: number | undefined, address,
addAllConventions: boolean, addAllConventions,
onlyWithConvention: boolean onlyWithConvention,
) => { limit,
const body = entrepriseSearchBody( }: SearchArgs) => {
const body = entrepriseSearchBody({
query, query,
address, address,
addAllConventions, addAllConventions,
onlyWithConvention, onlyWithConvention,
limit limit,
); });
const response = await elasticsearchClient.search({ const response = await elasticsearchClient.search({
body, body,
...@@ -31,7 +32,12 @@ export const search = async ( ...@@ -31,7 +32,12 @@ export const search = async (
}; };
export const searchEntreprise = async (siren: string) => { 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({ const response = await elasticsearchClient.search({
body, body,
...@@ -48,7 +54,12 @@ export const searchEntreprise = async (siren: string) => { ...@@ -48,7 +54,12 @@ export const searchEntreprise = async (siren: string) => {
}; };
export const searchEtablissement = async (siret: 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({ const response = await elasticsearchClient.search({
body, body,
......
...@@ -8,20 +8,20 @@ export const router = new Router(); ...@@ -8,20 +8,20 @@ export const router = new Router();
export const API_PREFIX = "/api/v1"; export const API_PREFIX = "/api/v1";
router.get(`${API_PREFIX}/search`, async (ctx) => { 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) { if (!query) {
ctx.throw(400, `query parameter q is required`); ctx.throw(400, `query parameter q is required`);
} }
try { try {
const entreprises = await search( const entreprises = await search({
query as string, query: query as string,
address as string, address: address as string,
parseInt(limit as string), limit: parseInt(limit as string),
true, onlyWithConvention: !!onlyWithConvention,
true addAllConventions: true
); });
ctx.body = { entreprises }; ctx.body = { entreprises };
} catch (err) { } catch (err) {
console.log(JSON.stringify(err)); console.log(JSON.stringify(err));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment