From 95590957282a0ffaeead752da30594ffb0144da2 Mon Sep 17 00:00:00 2001 From: Nicolas KREMER Date: Fri, 19 Jan 2024 17:31:27 +0100 Subject: [PATCH] fix: ajout de cache sur l'appelle API getPostalCode (#3519) --- .../common/apis/apiTablesCorrespondances.ts | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/server/src/common/apis/apiTablesCorrespondances.ts b/server/src/common/apis/apiTablesCorrespondances.ts index b6121e1d2..8678ef987 100644 --- a/server/src/common/apis/apiTablesCorrespondances.ts +++ b/server/src/common/apis/apiTablesCorrespondances.ts @@ -3,6 +3,8 @@ import { captureException } from "@sentry/node"; import logger from "@/common/logger"; import config from "@/config"; +import { tryCachedExecution } from "../utils/cacheUtils"; + import TabCoCfdInfo from "./@types/TabCoCfdInfo"; import TabCoCodePostalInfo from "./@types/TabCoCodePostalInfo"; import getApiClient from "./client"; @@ -34,19 +36,24 @@ export const getCfdInfo = async (cfd: string): Promise => { export const getCodePostalInfo = async (codePostal: string | null | undefined): Promise => { if (!codePostal) return null; - try { - const { data } = await client.post("/code-postal", { codePostal }, { cache: { methods: ["post"] } }); - return data; - } catch (error: any) { - logger.error( - `getCodePostalInfo: something went wrong while requesting code postal "${codePostal}": ${error.message}`, - error.code || error.response?.status - ); - captureException( - new Error(`getCodePostalInfo: something went wrong while requesting code postal "${codePostal}"`, { - cause: error, - }) - ); - return null; - } + + const serviceFunc = async () => { + try { + const { data } = await client.post("/code-postal", { codePostal }, { cache: { methods: ["post"] } }); + return data; + } catch (error: any) { + logger.error( + `getCodePostalInfo: something went wrong while requesting code postal "${codePostal}": ${error.message}`, + error.code || error.response?.status + ); + captureException( + new Error(`getCodePostalInfo: something went wrong while requesting code postal "${codePostal}"`, { + cause: error, + }) + ); + return null; + } + }; + + return tryCachedExecution(`codePostalInfo-${codePostal}`, 3600_000, serviceFunc); };