Skip to content

Commit

Permalink
add cache to lbb resuls
Browse files Browse the repository at this point in the history
  • Loading branch information
celineung committed Feb 12, 2025
1 parent 577fad8 commit 3f02652
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 56 deletions.
2 changes: 2 additions & 0 deletions back/src/config/bootstrap/createGateways.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ export const createGateways = async (
}),
franceTravailGateway,
config.franceTravailClientId,
withCache,
createLbbRoutes(config.ftApiUrl),
)
: new InMemoryLaBonneBoiteGateway(),
subscribersGateway:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { createFtFetchSharedClient } from "../../../../config/helpers/createFetchSharedClients";
import { HttpFranceTravailGateway } from "../../../convention/adapters/france-travail-gateway/HttpFranceTravailGateway";
import { InMemoryCachingGateway } from "../../../core/caching-gateway/adapters/InMemoryCachingGateway";
import { withNoCache } from "../../../core/caching-gateway/adapters/withNoCache";
import { noRetries } from "../../../core/retry-strategy/ports/RetryStrategy";
import { RealTimeGateway } from "../../../core/time-gateway/adapters/RealTimeGateway";
import { SearchCompaniesParams } from "../../ports/LaBonneBoiteGateway";
Expand Down Expand Up @@ -38,6 +39,8 @@ describe("HttpLaBonneBoiteGateway", () => {
noRetries,
),
config.franceTravailClientId,
withNoCache,
createLbbRoutes(config.ftApiUrl),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RomeDto, SearchResultDto, SiretDto, castError } from "shared";
import { HttpClient } from "shared-routes";
import { createLogger } from "../../../../utils/logger";
import { FranceTravailGateway } from "../../../convention/ports/FranceTravailGateway";
import { WithCache } from "../../../core/caching-gateway/port/WithCache";
import {
LaBonneBoiteGateway,
SearchCompaniesParams,
Expand Down Expand Up @@ -33,65 +34,92 @@ export class HttpLaBonneBoiteGateway implements LaBonneBoiteGateway {
private readonly httpClient: HttpClient<LaBonneBoiteRoutes>,
private readonly franceTravailGateway: FranceTravailGateway,
private readonly franceTravailClientId: string,
private readonly withCache: WithCache,
private readonly lbbRoute: LaBonneBoiteRoutes,
) {}

public async searchCompanies({
distanceKm,
lat,
lon,
romeCode,
romeLabel,
nafCodes,
}: SearchCompaniesParams): Promise<SearchResultDto[]> {
return this.#limiter
.schedule(async () =>
this.httpClient.getCompanies({
headers: {
authorization: await this.#makeAutorization(),
},
queryParams: {
distance: MAX_DISTANCE_IN_KM,
longitude: lon,
latitude: lat,
page: 1,
page_size: MAX_PAGE_SIZE,
rome: [romeCode],
...(nafCodes ? { naf: nafCodes } : {}),
},
}),
)
.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,
);
public async searchCompanies(
searchCompaniesParams: SearchCompaniesParams,
): Promise<SearchResultDto[]> {
const cachedLbbResults = this.withCache<
SearchResultDto[],
SearchCompaniesParams
>({
overrideCacheDurationInHours: 24,
logParams: {
partner: "laBonneBoite",
route: this.lbbRoute.getCompanies,
},
getCacheKey: (query) =>
`laBonneBoite_${query.romeCode}_${query.lat.toFixed(
3,
)}_${query.lon.toFixed(3)}_${query.distanceKm}${
query.nafCodes ? `_${query.nafCodes.join("_")}` : ""
}`,
cb: async ({
lon,
lat,
romeCode,
nafCodes,
romeLabel,
distanceKm,
}): Promise<SearchResultDto[]> => {
return this.httpClient
.getCompanies({
headers: {
authorization: await this.#makeAutorization(),
},
queryParams: {
distance: MAX_DISTANCE_IN_KM,
longitude: lon,
latitude: lat,
page: 1,
page_size: MAX_PAGE_SIZE,
rome: [romeCode],
...(nafCodes ? { naf: nafCodes } : {}),
},
})
.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 items ?? [];
})
.catch((error) => {
logger.error({
error: castError(error),
message: "searchCompanies_error",
searchLBB: {
distanceKm,
lat,
lon,
romeCode,
romeLabel,
nafCodes,
},
});
return [];
});
},
});

return this.#limiter.schedule(() =>
cachedLbbResults(searchCompaniesParams),
);
}

public async fetchCompanyBySiret(
Expand Down

0 comments on commit 3f02652

Please sign in to comment.