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 @@
"typescript": "^4.1.5"
},
"jest": {
"testTimeout": 3000,
"testTimeout": 10000,
"roots": [
"<rootDir>/src"
],
......
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......
......@@ -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 () => {
......@@ -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(
......
......@@ -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: {
......@@ -189,5 +197,5 @@ export const entrepriseSearchBody = (
],
},
},
size: size ? size : defaultLimit,
size: limit ? limit : defaultLimit,
});
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,
......@@ -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,
......@@ -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,
......
......@@ -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));
......
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