diff --git a/scripts/fill-user-etablissement-field.tsx b/scripts/fill-user-etablissement-field.tsx index 1c6b00c7..296600c7 100644 --- a/scripts/fill-user-etablissement-field.tsx +++ b/scripts/fill-user-etablissement-field.tsx @@ -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();