Find old contracts to decrement score on

This commit is contained in:
Ian Philips 2022-07-19 17:08:51 -06:00
parent 4aface583d
commit 1f0983a145

View File

@ -3,6 +3,7 @@ import * as admin from 'firebase-admin'
import { Bet } from 'common/bet'
import { uniq } from 'lodash'
import { Contract } from 'common/contract'
import { log } from './utils'
export const scoreContracts = functions.pubsub
.schedule('every 1 hours')
@ -15,17 +16,29 @@ async function scoreContractsInternal() {
const now = Date.now()
const lastHour = now - 3600000
const last3Days = now - 2592000000
const contracts = await firestore
const activeContractsSnap = await firestore
.collection('contracts')
.where('lastUpdatedTime', '>', lastHour)
.get()
const activeContracts = activeContractsSnap.docs.map(
(doc) => doc.data() as Contract
)
// We have to downgrade previously active contracts to allow the new ones to bubble up
const previouslyActiveContractsSnap = await firestore
.collection('contracts')
.where('popularityScore', '>', 0)
.get()
const activeContractIds = activeContracts.map((c) => c.id)
const previouslyActiveContracts = previouslyActiveContractsSnap.docs
.map((doc) => doc.data() as Contract)
.filter((c) => !activeContractIds.includes(c.id))
for (const contractSnap of contracts.docs) {
const contract = contractSnap.data() as Contract
const contractId = contractSnap.id
const contracts = activeContracts.concat(previouslyActiveContracts)
log(`Found ${contracts.length} contracts to score`)
for (const contract of contracts) {
const bets = await firestore
.collection(`contracts/${contractId}/bets`)
.collection(`contracts/${contract.id}/bets`)
.where('createdTime', '>', last3Days)
.get()
const bettors = bets.docs
@ -35,7 +48,7 @@ async function scoreContractsInternal() {
if (contract.popularityScore !== score)
await firestore
.collection('contracts')
.doc(contractId)
.doc(contract.id)
.update({ popularityScore: score })
}
}