Skip to content

Commit

Permalink
feat: improve query
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthh committed Aug 2, 2023
1 parent 290de93 commit 5b6b369
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
21 changes: 13 additions & 8 deletions src/services/apecoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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');

Expand Down
18 changes: 9 additions & 9 deletions src/services/bulk-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5b6b369

Please sign in to comment.