Skip to content

Commit

Permalink
add cache to lbb get company by siret
Browse files Browse the repository at this point in the history
  • Loading branch information
celineung committed Feb 12, 2025
1 parent 3f02652 commit e8f3576
Showing 1 changed file with 93 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,25 @@ export class HttpLaBonneBoiteGateway implements LaBonneBoiteGateway {
public async searchCompanies(
searchCompaniesParams: SearchCompaniesParams,
): Promise<SearchResultDto[]> {
const cachedLbbResults = this.withCache<
SearchResultDto[],
const cachedGetLbbResults = this.withCache<
LaBonneBoiteApiResultV2Props[],
SearchCompaniesParams
>({
overrideCacheDurationInHours: 24,
overrideCacheDurationInHours: 24 * 3,
logParams: {
partner: "laBonneBoite",
route: this.lbbRoute.getCompanies,
},
getCacheKey: (query) =>
`laBonneBoite_${query.romeCode}_${query.lat.toFixed(
3,
)}_${query.lon.toFixed(3)}_${query.distanceKm}${
`lbb_${query.romeCode}_${query.lat.toFixed(3)}_${query.lon.toFixed(3)}${
query.nafCodes ? `_${query.nafCodes.join("_")}` : ""
}`,
cb: async ({
lon,
lat,
romeCode,
nafCodes,
romeLabel,
distanceKm,
}): Promise<SearchResultDto[]> => {
}): Promise<LaBonneBoiteApiResultV2Props[]> => {
return this.httpClient
.getCompanies({
headers: {
Expand All @@ -82,84 +78,108 @@ export class HttpLaBonneBoiteGateway implements LaBonneBoiteGateway {
.then((response) => {
if (response.status !== 200)
throw new Error(JSON.stringify(response));
const items = response.body?.items
?.map(
(props: LaBonneBoiteApiResultV2Props) =>
new LaBonneBoiteCompanyDto(props),
)
.filter((result) => result.isCompanyRelevant())
.map((result) =>
result.toSearchResult({ romeCode, romeLabel }, { lat, lon }),
)
.filter((result) =>
result.distance_m
? result.distance_m <= distanceKm * 1000
: true,
);

return items ?? [];
})
.catch((error) => {
logger.error({
error: castError(error),
message: "searchCompanies_error",
searchLBB: {
distanceKm,
lat,
lon,
romeCode,
romeLabel,
nafCodes,
},
});
return [];
return response.body?.items ?? [];
});
},
});

return this.#limiter.schedule(() =>
cachedLbbResults(searchCompaniesParams),
cachedGetLbbResults(searchCompaniesParams)
.then((results) => {
return results
.map(
(props: LaBonneBoiteApiResultV2Props) =>
new LaBonneBoiteCompanyDto(props),
)
.filter((result) => result.isCompanyRelevant())
.map((result) =>
result.toSearchResult(
{
romeCode: searchCompaniesParams.romeCode,
romeLabel: searchCompaniesParams.romeLabel,
},
{
lat: searchCompaniesParams.lat,
lon: searchCompaniesParams.lon,
},
),
)
.filter((result) =>
result.distance_m
? result.distance_m <= searchCompaniesParams.distanceKm * 1000
: true,
);
})
.catch((error) => {
logger.error({
error: castError(error),
message: "searchCompanies_error",
searchLBB: {
distanceKm: searchCompaniesParams.distanceKm,
lat: searchCompaniesParams.lat,
lon: searchCompaniesParams.lon,
romeCode: searchCompaniesParams.romeCode,
romeLabel: searchCompaniesParams.romeLabel,
nafCodes: searchCompaniesParams.nafCodes,
},
});
return [];
}),
);
}

public async fetchCompanyBySiret(
siret: SiretDto,
romeDto: RomeDto,
): Promise<SearchResultDto | null> {
return this.#limiter
.schedule(async () =>
this.httpClient.getCompany({
headers: {
authorization: await this.#makeAutorization(),
},
queryParams: {
const cachedGetLbbResult = this.withCache({
overrideCacheDurationInHours: 24 * 3,
logParams: {
partner: "laBonneBoite",
route: this.lbbRoute.getCompany,
},
getCacheKey: (siret) => `lbb_${siret}`,
cb: async (siret): Promise<LaBonneBoiteApiResultV2Props[]> => {
return this.httpClient
.getCompany({
headers: {
authorization: await this.#makeAutorization(),
},
queryParams: {
siret,
},
})
.then((response) => {
if (response.status !== 200)
throw new Error(JSON.stringify(response));
return response.body?.items ?? [];
});
},
});
return this.#limiter.schedule(async () =>
cachedGetLbbResult(siret)
.then((result) => {
const item = result
.map(
(props: LaBonneBoiteApiResultV2Props) =>
new LaBonneBoiteCompanyDto(props),
)
.filter((result) => result.isCompanyRelevant())
.map((result) => result.toSearchResult(romeDto))
.at(0);

return item ?? null;
})
.catch((error) => {
logger.error({
error: castError(error),
message: "fetchCompanyBySiret_error",
siret,
},
romeLabel: romeDto.romeLabel,
});
throw error;
}),
)
.then((response) => {
if (response.status !== 200) throw new Error(JSON.stringify(response));

const item = response.body?.items
?.map(
(props: LaBonneBoiteApiResultV2Props) =>
new LaBonneBoiteCompanyDto(props),
)
.filter((result) => result.isCompanyRelevant())
.map((result) => result.toSearchResult(romeDto))
.at(0);

return item ?? null;
})
.catch((error) => {
logger.error({
error: castError(error),
message: "fetchCompanyBySiret_error",
siret,
romeLabel: romeDto.romeLabel,
});
throw error;
});
);
}

async #makeAutorization(): Promise<string> {
Expand Down

0 comments on commit e8f3576

Please sign in to comment.