diff --git a/src/services/apecoin/index.ts b/src/services/apecoin/index.ts index 9a5232b..3aaebba 100644 --- a/src/services/apecoin/index.ts +++ b/src/services/apecoin/index.ts @@ -94,15 +94,21 @@ async function manageRoles(client: Client, guildId: string, handles: any[], acti const role = guild.roles.cache.find((role) => role.name === ROLE_NAME); if (!role) throw new Error('Role not found'); + const sucessHandles = []; for (const handle of handles) { - const member = await guild.members.fetch(handle.discordHandle).catch(console.error); - - if (member) { - await member.roles[action](role).catch(console.error); - console.log( - `Role: ${role.name} | action: ${action} | user: ${member.user.tag} | address: ${handle.publicAddress} | offChain: ${handle.offChainVotesPct} | balance: ${handle.balance} | forumLevel: ${handle.trustLevel} | hasCriterea: ${handle.hasPermission}` - ); + try { + const member = await guild.members.fetch(handle.discordHandle); + + if (member) { + await member.roles[action](role); + console.log( + `Role: ${role.name} | action: ${action} | user: ${member.user.tag} | address: ${handle.publicAddress} | offChain: ${handle.offChainVotesPct} | balance: ${handle.balance} | forumLevel: ${handle.trustLevel} | hasCriterea: ${handle.hasPermission}` + ); + sucessHandles.push(handle); + } + } catch (err) { + console.log(err); } } @@ -113,7 +119,6 @@ async function manageRoles(client: Client, guildId: string, handles: any[], acti export async function discordRoleManager(_?: string, publicAddress?: string) { const dao = await fetchDaoData(); const delegates = await fetchDelegates(dao.name, publicAddress); - console.log(delegates); if (!delegates?.length) return console.log('No delegates found'); diff --git a/src/services/bulk-writer.ts b/src/services/bulk-writer.ts index 1899791..fbdc92e 100644 --- a/src/services/bulk-writer.ts +++ b/src/services/bulk-writer.ts @@ -56,24 +56,24 @@ export class BulkWriter { async updateRolesLogs(action: string, delegates: any[], role: string) { const integration = 'discord'; const description = 'discord role'; + const dateNow = Date.now(); const insertValues = delegates .map( (d) => - `('${ - d.id - }', '${integration}', '${description}', '${role.toLowerCase()}', '${Date.now()}')` + `('${d.id}', '${integration}', '${description}', '${role.toLowerCase()}', '${dateNow}')` ) .join(','); - const sql = `INSERT INTO "DelegateIntegrations" ("delegateId", "integration", "description", "attribute", "issuedAt") VALUES ${insertValues} + const sql = + action === 'add' + ? `INSERT INTO "DelegateIntegrations" ("delegateId", "integration", "description", "attribute", "issuedAt") VALUES ${insertValues} ON CONFLICT ("delegateId", "integration", "attribute") - ${ - action === 'add' - ? `DO UPDATE SET "issuedAt" = CASE WHEN "DelegateIntegrations"."issuedAt" IS NULL THEN '${Date.now()}' + DO UPDATE SET "issuedAt" = CASE WHEN "DelegateIntegrations"."issuedAt" IS NULL THEN '${dateNow}' ELSE "DelegateIntegrations"."issuedAt" END` - : 'DO UPDATE SET "issuedAt" = null' - }`; + : `UPDATE "DelegateIntegrations" SET "issuedAt" = null + WHERE "delegateId" IN (${delegates.map((d) => d.id as number).join(', ')}) + AND "attribute" = '${role.toLowerCase()}' `; await pool.query(sql); }