Skip to content

Commit

Permalink
Fix point computation
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneIRL committed Oct 7, 2023
1 parent c499b10 commit 8b578ec
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/lib/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,15 @@ export async function computePoints(
user: User | JsonUser | undefined | null,
): Promise<number> {
const client = await clientPromise
const [events, adjustments] = [user?.attendedEvents, user?.pointAdjustments]
const [{ points }] = events?.length
? await client.db()
.collection<Event>('events')
.aggregate([
{ $match: { title: { $in: events } } },
{ $replaceWith: { points: { $sum: '$pointValue' } } },
])
.toArray() as { points: number }[]
: [{ points: 0 }]
const events = user?.attendedEvents?.length
? await client.db().collection<Event>('events').find({
title: { $in: user.attendedEvents },
}).toArray()
: []
const eventPoints = events.reduce((p, c) => p + c.pointValue, 0)
const checkedInBonus = user?.checkedIn ? 50 : 0
return points + checkedInBonus + sumPointAdjustments(adjustments)
return eventPoints + checkedInBonus
+ sumPointAdjustments(user?.pointAdjustments)
}

export async function updatePoints(
Expand Down

0 comments on commit 8b578ec

Please sign in to comment.