Skip to content

Commit

Permalink
Merge pull request #1071 from Giveth/fix/deduplicate_user_instant_bal…
Browse files Browse the repository at this point in the history
…ance

Fixed duplicate power balance instance insert
  • Loading branch information
aminlatifi authored Jul 20, 2023
2 parents d1c1e7d + 488c862 commit 76cdbfd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/repositories/instantBoostingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ export const saveOrUpdateInstantPowerBalances = async (
'saveOrUpdateInstantPowerBalances ',
JSON.stringify({ instances }, null, 2),
);

const userIdInstanceDic = {};
instances.forEach(instance => {
if (!instance.userId) {
throw new Error('userId is required for InstantPowerBalance');
}
userIdInstanceDic[instance.userId!] = instance;
});

await InstantPowerBalance.createQueryBuilder<InstantPowerBalance>()
.insert()
.into(InstantPowerBalance)
.values(instances)
.values(Object.values(userIdInstanceDic))
.orUpdate(['balance', 'chainUpdatedAt'], ['userId'])
.execute();
} catch (e) {
logger.error('saveOrUpdateInstantPowerBalances error', e);
throw e;
}
};

Expand Down

0 comments on commit 76cdbfd

Please sign in to comment.