Decrement follow count on unfollow

This commit is contained in:
Ian Philips 2022-08-23 16:50:25 -06:00
parent 7008164d25
commit f6b357cbc4
3 changed files with 25 additions and 1 deletions

View File

@ -21,7 +21,6 @@ export const addUserToContractFollowers = async (
id: user.id,
createdTime: Date.now(),
})
// TODO: decrement for unfollows
await firestore
.collection(`contracts`)
.doc(contract.id)

View File

@ -30,6 +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'
// v2
export * from './health'

View File

@ -0,0 +1,24 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { FieldValue } from 'firebase-admin/firestore'
export const onDeleteContractFollow = functions.firestore
.document('contracts/{contractId}/follows/{userId}')
.onDelete(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),
})
})