Skip to content

Commit

Permalink
Add pointHistory to rendering context
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneIRL committed Oct 7, 2023
1 parent 64e0152 commit bf17add
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
46 changes: 46 additions & 0 deletions scripts/update-points.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MongoClient, ServerApiVersion } from 'mongodb'
import { env } from 'process'

const clientPromise = new MongoClient(env.MONGODB_URI, {
serverApi: ServerApiVersion.v1,
}).connect()
const client = await clientPromise
const users = client.db().collection('users')

export function sumPointAdjustments(adjustments) {
return adjustments?.reduce((p, c) => p + c.delta, 0) ?? 0
}

export async function computePoints(user) {
const events = user?.attendedEvents?.length
? await client.db().collection('events').find({
title: { $in: user.attendedEvents },
}).toArray()
: []
const eventPoints = events.reduce((p, c) => p + c.pointValue, 0)
const checkedInBonus = user?.checkedIn ? 50 : 0
return eventPoints + checkedInBonus
+ sumPointAdjustments(user?.pointAdjustments)
}

let counter = 0

export async function updateUser(user) {
const newPoints = await computePoints(user)
await users.updateOne({ _id: user._id }, { $set: { points: newPoints } })
if (user.points !== newPoints) {
console.log(`updated ${user.email} from ${user.points} to ${newPoints}`)
}
if (++counter % 20 === 0) {
console.log(counter)
}
return { ...user, points: newPoints }
}

export async function updateAll() {
const all = await users.find({}, { projection: { 'application.resume': 0 } })
.toArray()
return await Promise.all(all.map(updateUser))
}

await updateAll()
29 changes: 26 additions & 3 deletions src/lib/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import '@/node-only'

import {
Condition,
Document as MongoDocument,
MongoClient,
Document as MongoDocument,
ObjectId,
WithId,
} from 'mongodb'
Expand All @@ -19,7 +19,7 @@ import logger from '@/lib/logger'
import { BuiltInRoles, EnhancedSession, hasPermission } from '../auth/shared'
import clientPromise from '../db'
import Event from '../db/models/Event'
import { getGroupName, RenderContext, sumPointAdjustments } from './shared'
import { RenderContext, getGroupName, sumPointAdjustments } from './shared'

export * from './shared'

Expand Down Expand Up @@ -180,8 +180,31 @@ export async function createTemplateRenderContext(): Promise<RenderContext> {

return {
user,
group: user?.hexId && getGroupName(user.hexId),
applicationWaived: hasPermission(perms, { applicationWaived: true }),
group: user?.hexId && getGroupName(user.hexId),
pointHistory: createPointHistory(),
}

function createPointHistory(): string {
let ans = ''
const events = user?.attendedEvents
if (user?.checkedIn || events?.length) {
ans += '**Attended Events**\n\n'
if (user?.checkedIn) {
ans += `* Checked In\n`
}
for (const event of events ?? []) {
ans += `* ${event}\n`
}
}
const adjustments = user?.pointAdjustments
if (adjustments?.length) {
ans += '\n**Adjustments**\n\n'
for (const { reason, delta } of adjustments) {
ans += `* ${reason}: **${delta >= 0 ? `+${delta}` : delta}**\n`
}
}
return ans
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type ToJsonValue<T> = T extends Date | ObjectId ? string
export interface RenderContext {
user: JsonUser | null
applicationWaived: boolean
pointHistory: string
group?: string | undefined
}

Expand Down

0 comments on commit bf17add

Please sign in to comment.