2022-08-24 16:49:53 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import { FieldValue } from 'firebase-admin/firestore'
|
|
|
|
|
2022-08-30 15:38:59 +00:00
|
|
|
// TODO: should cache the follower user ids in the contract as these triggers aren't idempotent
|
2022-08-24 16:49:53 +00:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
})
|