Increment follower count as well
This commit is contained in:
parent
285ce1633a
commit
39d5e56c50
|
@ -1,5 +1,4 @@
|
|||
import * as admin from 'firebase-admin'
|
||||
import { FieldValue } from 'firebase-admin/firestore'
|
||||
|
||||
const firestore = admin.firestore()
|
||||
|
||||
|
@ -19,12 +18,6 @@ export const addUserToContractFollowers = async (
|
|||
id: userId,
|
||||
createdTime: Date.now(),
|
||||
})
|
||||
await firestore
|
||||
.collection(`contracts`)
|
||||
.doc(contractId)
|
||||
.update({
|
||||
followerCount: FieldValue.increment(1),
|
||||
})
|
||||
}
|
||||
|
||||
export const removeUserFromContractFollowers = async (
|
||||
|
@ -40,10 +33,4 @@ export const removeUserFromContractFollowers = async (
|
|||
.collection(`contracts/${contractId}/follows`)
|
||||
.doc(userId)
|
||||
.delete()
|
||||
await firestore
|
||||
.collection(`contracts`)
|
||||
.doc(contractId)
|
||||
.update({
|
||||
followerCount: FieldValue.increment(-1),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ export * from './score-contracts'
|
|||
export * from './weekly-markets-emails'
|
||||
export * from './reset-betting-streaks'
|
||||
export * from './reset-weekly-emails-flag'
|
||||
export * from './on-delete-contract-follow'
|
||||
export * from './on-update-contract-follow'
|
||||
|
||||
// v2
|
||||
export * from './health'
|
||||
|
|
|
@ -22,3 +22,24 @@ export const onDeleteContractFollow = functions.firestore
|
|||
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),
|
||||
})
|
||||
})
|
|
@ -55,11 +55,12 @@ async function backfillContractFollowers() {
|
|||
.doc(followerId)
|
||||
.set({ id: followerId, createdTime: Date.now() })
|
||||
}
|
||||
const followerCount = followerIds.length
|
||||
await firestore
|
||||
.collection(`contracts`)
|
||||
.doc(contract.id)
|
||||
.update({ followerCount: followerCount })
|
||||
// Perhaps handled by the trigger?
|
||||
// const followerCount = followerIds.length
|
||||
// await firestore
|
||||
// .collection(`contracts`)
|
||||
// .doc(contract.id)
|
||||
// .update({ followerCount: followerCount })
|
||||
count += 1
|
||||
if (count % 100 === 0) {
|
||||
console.log(`${count} contracts processed`)
|
||||
|
|
Loading…
Reference in New Issue
Block a user