manifold/functions/src/reset-betting-streaks.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

// check every day if the user has created a bet since 4pm UTC, and if not, reset their streak
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { User } from '../../common/user'
import { DAY_MS } from '../../common/util/time'
import { BETTING_STREAK_RESET_HOUR } from '../../common/economy'
const firestore = admin.firestore()
export const resetBettingStreaksForUsers = functions.pubsub
.schedule(`0 ${BETTING_STREAK_RESET_HOUR} * * *`)
.timeZone('Etc/UTC')
.onRun(async () => {
await resetBettingStreaksInternal()
})
const resetBettingStreaksInternal = async () => {
2022-08-31 14:03:51 +00:00
const usersSnap = await firestore
.collection('users')
.where('currentBettingStreak', '>', 0)
.get()
const users = usersSnap.docs.map((doc) => doc.data() as User)
for (const user of users) {
await resetBettingStreakForUser(user)
}
}
const resetBettingStreakForUser = async (user: User) => {
const betStreakResetTime = Date.now() - DAY_MS
// if they made a bet within the last day, don't reset their streak
if (
2022-08-29 19:47:24 +00:00
(user?.lastBetTime ?? 0) > betStreakResetTime ||
!user.currentBettingStreak ||
user.currentBettingStreak === 0
)
return
await firestore.collection('users').doc(user.id).update({
currentBettingStreak: 0,
})
}