Skip to content

Commit

Permalink
fix: add set timeout for siren api batch
Browse files Browse the repository at this point in the history
  • Loading branch information
rtaieb committed Dec 9, 2024
1 parent aa84bf8 commit 4eaa87e
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions scripts/fill-user-etablissement-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ async function main() {
nom_etablissement: null,
},
});

usersToProcess.map(async (userToProcess) => {
// @ts-ignore
const siret = userToProcess.agentconnect_info?.siret;
if (siret) {
await new Promise((r) => setTimeout(r, 300));
const entityFromSiret = await fetchEntrepriseFromSirenApi(siret);
if (entityFromSiret?.etablissement) {
await updateUserEtablissementInfo(
userToProcess.id,
entityFromSiret.etablissement?.uniteLegale?.denominationUniteLegale,
entityFromSiret.etablissement,
);
const interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
usersToProcess.forEach(function (userToProcess, index) {
setTimeout(async function () {
// @ts-ignore
const siret = userToProcess.agentconnect_info?.siret;
if (siret) {
const entityFromSiret = await fetchEntrepriseFromSirenApi(siret);
if (entityFromSiret?.etablissement) {
await updateUserEtablissementInfo(
userToProcess.id,
entityFromSiret.etablissement?.uniteLegale?.denominationUniteLegale,
entityFromSiret.etablissement,
);
}
console.log(`L'utilisateur ${userToProcess.email} a été mis à jour`);
} else {
console.log(`L'utilisateur ${userToProcess.email} n'a pas de siret`);
}
console.log(`L'utilisateur ${userToProcess.email} a été mis à jour`);
} else {
console.log(`L'utilisateur ${userToProcess.email} n'a pas de siret`);
}
}, index * interval);
});
console.log("Loop finished.");
}

main();

0 comments on commit 4eaa87e

Please sign in to comment.