Skip to content

Commit

Permalink
fix: ajout de cache sur l'appelle API getPostalCode (#3519)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrmr authored Jan 19, 2024
1 parent ef54571 commit 9559095
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions server/src/common/apis/apiTablesCorrespondances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -34,19 +36,24 @@ export const getCfdInfo = async (cfd: string): Promise<TabCoCfdInfo | null> => {

export const getCodePostalInfo = async (codePostal: string | null | undefined): Promise<TabCoCodePostalInfo | null> => {
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);
};

0 comments on commit 9559095

Please sign in to comment.