Skip to content

Commit

Permalink
rewrite totalReceived query
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Sep 5, 2023
1 parent 2e384c3 commit 302f253
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export const updateUserTotalDonated = async (userId: number) => {

export const updateUserTotalReceived = async (userId: number) => {
try {
await User.query(
`
UPDATE "user"
SET "totalReceived" = (
SELECT COALESCE(SUM(p."totalDonations"),0)
FROM project as p
WHERE p."adminUserId" = $1
)
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 302f253

Please sign in to comment.