diff --git a/functions/src/follow-market.ts b/functions/src/follow-market.ts index 996aa135..1dc2941b 100644 --- a/functions/src/follow-market.ts +++ b/functions/src/follow-market.ts @@ -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) diff --git a/functions/src/index.ts b/functions/src/index.ts index 26a1ddf6..e6d6977c 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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' diff --git a/functions/src/on-delete-contract-follow.ts b/functions/src/on-delete-contract-follow.ts new file mode 100644 index 00000000..4b9a9646 --- /dev/null +++ b/functions/src/on-delete-contract-follow.ts @@ -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), + }) + })