Increment follower count as well

This commit is contained in:
Ian Philips 2022-08-24 10:22:39 -06:00
parent 285ce1633a
commit 39d5e56c50
4 changed files with 28 additions and 19 deletions

View File

@ -1,5 +1,4 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { FieldValue } from 'firebase-admin/firestore'
const firestore = admin.firestore() const firestore = admin.firestore()
@ -19,12 +18,6 @@ export const addUserToContractFollowers = async (
id: userId, id: userId,
createdTime: Date.now(), createdTime: Date.now(),
}) })
await firestore
.collection(`contracts`)
.doc(contractId)
.update({
followerCount: FieldValue.increment(1),
})
} }
export const removeUserFromContractFollowers = async ( export const removeUserFromContractFollowers = async (
@ -40,10 +33,4 @@ export const removeUserFromContractFollowers = async (
.collection(`contracts/${contractId}/follows`) .collection(`contracts/${contractId}/follows`)
.doc(userId) .doc(userId)
.delete() .delete()
await firestore
.collection(`contracts`)
.doc(contractId)
.update({
followerCount: FieldValue.increment(-1),
})
} }

View File

@ -30,7 +30,7 @@ export * from './score-contracts'
export * from './weekly-markets-emails' export * from './weekly-markets-emails'
export * from './reset-betting-streaks' export * from './reset-betting-streaks'
export * from './reset-weekly-emails-flag' export * from './reset-weekly-emails-flag'
export * from './on-delete-contract-follow' export * from './on-update-contract-follow'
// v2 // v2
export * from './health' export * from './health'

View File

@ -22,3 +22,24 @@ export const onDeleteContractFollow = functions.firestore
followerCount: FieldValue.increment(-1), followerCount: FieldValue.increment(-1),
}) })
}) })
export const onCreateContractFollow = functions.firestore
.document('contracts/{contractId}/follows/{userId}')
.onCreate(async (change, context) => {
const { contractId } = context.params as {
contractId: string
}
const firestore = admin.firestore()
const contract = await firestore
.collection(`contracts`)
.doc(contractId)
.get()
if (!contract.exists) throw new Error('Could not find contract')
await firestore
.collection(`contracts`)
.doc(contractId)
.update({
followerCount: FieldValue.increment(1),
})
})

View File

@ -55,11 +55,12 @@ async function backfillContractFollowers() {
.doc(followerId) .doc(followerId)
.set({ id: followerId, createdTime: Date.now() }) .set({ id: followerId, createdTime: Date.now() })
} }
const followerCount = followerIds.length // Perhaps handled by the trigger?
await firestore // const followerCount = followerIds.length
.collection(`contracts`) // await firestore
.doc(contract.id) // .collection(`contracts`)
.update({ followerCount: followerCount }) // .doc(contract.id)
// .update({ followerCount: followerCount })
count += 1 count += 1
if (count % 100 === 0) { if (count % 100 === 0) {
console.log(`${count} contracts processed`) console.log(`${count} contracts processed`)