diff --git a/packages/web-app/app/_server/idos/idos-grantee.ts b/packages/web-app/app/_server/idos/idos-grantee.ts index 90bf72bb..8c8d1e55 100644 --- a/packages/web-app/app/_server/idos/idos-grantee.ts +++ b/packages/web-app/app/_server/idos/idos-grantee.ts @@ -154,15 +154,23 @@ export class idOSGrantee { } async getSharedCredentialContentDecrypted(dataId: string): Promise { - const credentialCopy = await this.fetchSharedCredentialFromIdos<{ - content: string; - encryption_public_key: string; - }>(dataId); - - return await this.noncedBox.decrypt( - credentialCopy.content, - credentialCopy.encryption_public_key, - ); + try { + const credentialCopy = await this.fetchSharedCredentialFromIdos<{ + content: string; + encryption_public_key: string; + }>(dataId); + + return await this.noncedBox.decrypt( + credentialCopy.content, + credentialCopy.encryption_public_key, + ); + } catch (error) { + console.log( + 'Error fetching or decrypting shared credential from idos for dataId:', + dataId, + ); + throw error; + } } async isValidCredential(credential: any): Promise { diff --git a/packages/web-app/app/_server/idos/index.ts b/packages/web-app/app/_server/idos/index.ts index 119efe16..13335edf 100644 --- a/packages/web-app/app/_server/idos/index.ts +++ b/packages/web-app/app/_server/idos/index.ts @@ -63,53 +63,27 @@ const userFilter = async (grantee: idOSGrantee, userAddress: string) => { return null; }; -const populateLists = async (addresses: string[], grantee: idOSGrantee) => { +const filterApplicants = async (addresses: string[], grantee: idOSGrantee) => { const allowed: string[] = []; const notAllowed: string[] = []; - const batches = Math.ceil(addresses.length / 2); + const batches = Math.ceil(addresses.length / 10); + // run in batches of 10 for (let i = 0; i < batches; i++) { - console.log( - '%c==>BATCH', - 'color: green; background: yellow; font-size: 20px', - i, - ); + console.log('==>BATCH', i); - const batch = addresses.slice(i * 2, (i + 1) * 2); - console.log( - '%c==>', - 'color: green; background: yellow; font-size: 20px', - 'MANDOU', + let batch = addresses.slice(i * 10, (i + 1) * 10); + let promises = batch.map( + async (address): Promise => + new Promise((resolve) => resolve(userFilter(grantee, address))), ); - const promises = batch.map(async (address): Promise => { - return new Promise(() => userFilter(grantee, address)); - }); - - const results = await Promise.all(promises); - - console.log( - '%c==>', - 'color: green; background: yellow; font-size: 20px', - 'CHEGOU', - ); + let results = await Promise.all(promises); results.forEach((result, index) => { - console.log( - '%c==>RESULT', - 'color: green; background: yellow; font-size: 20px', - result, - ); - if (result !== null) { allowed.push(result); } else { - console.log( - '%c==>FAILED', - 'color: green; background: yellow; font-size: 20px', - result, - ); - notAllowed.push(batch[index]); } }); @@ -118,6 +92,21 @@ const populateLists = async (addresses: string[], grantee: idOSGrantee) => { return { allowed, notAllowed }; }; +const retryFailed = async ( + allowed: string[], + notAllowed: string[], + grantee: idOSGrantee, +) => { + for (const address of notAllowed) { + const result = await userFilter(grantee, address); + if (result !== null) { + allowed.push(result); + } + } + + notAllowed = notAllowed.filter((address) => !allowed.includes(address)); +}; + export const getAllowedProjectApplicants = async (projectAddress: string) => { try { const applicantsResult = await getProjectApplicants(projectAddress); @@ -131,43 +120,19 @@ export const getAllowedProjectApplicants = async (projectAddress: string) => { ? undefined : 'x44250024a9bf9599ad7c3fcdb220d2100357dbf263014485174a1ae3', }); - // number of batches of 5 addresses - let { allowed, notAllowed } = await populateLists(addresses, grantee); - - console.log( - '%c==>', - 'color: green; background: yellow; font-size: 20px', - 'DEU MERDA', - ); - - // for (const address of addresses) { - // const result = await userFilter(grantee, address); - // if (result !== null) { - // allowed.push(result); - // } else { - // notAllowed.push(address); - // } - // } - let retry = 5; + // first run to populate the lists + let { allowed, notAllowed } = await filterApplicants(addresses, grantee); + // retry 2 times failed addresses + let retry = 2; while (retry > 0 && notAllowed.length > 0) { - for (const address of notAllowed) { - const result = await userFilter(grantee, address); - if (result !== null) { - allowed.push(result); - } else { - console.log( - '%c==>Failed Retry', - 'color: green; background: yellow; font-size: 20px', - address, - ); - } - } - - notAllowed = notAllowed.filter((address) => !allowed.includes(address)); + retryFailed(allowed, notAllowed, grantee); retry--; } + console.log('==>', 'NOT ALLOWED:'); + console.log(notAllowed); + return allowed as string[]; } catch (error) { console.error(error);