Skip to content

Commit

Permalink
Revert updateUserTotalReceived() funciton
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed Nov 28, 2023
1 parent b04821a commit a3ed597
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ export const updateUserTotalDonated = async (userId: number) => {

export const updateUserTotalReceived = async (userId: number) => {
try {
await Donation.query(
`
WITH total_received AS (
SELECT COALESCE(SUM(p."totalDonations"), 0) + COALESCE(SUM(q."matchingFund"), 0) AS total
FROM "user" u
LEFT JOIN "project" p ON p."adminUserId" = u."id"
LEFT JOIN "qf_round_history" q ON q."projectId" = p."id"
WHERE u."id" = $1
GROUP BY u."id"
)
UPDATE "user"
SET "totalReceived" = (SELECT total FROM total_received)
WHERE "id" = $1;
`,
[userId],
);
const totalReceived = await User.createQueryBuilder('user')
.select('COALESCE(SUM(project.totalDonations), 0)', 'totalReceived')
.leftJoin('project', 'project', 'project.adminUserId = user.id')
.where('user.id = :userId', { userId })
.addGroupBy('user.id')
.getRawOne();

await User.createQueryBuilder()
.update(User)
.set({ totalReceived: totalReceived.totalReceived })
.where('id = :userId', { userId })
.execute();
} catch (e) {
logger.error('updateUserTotalReceived() error', e);
}
Expand Down

0 comments on commit a3ed597

Please sign in to comment.