From 39d5e56c50e4675ee0c4d53b89edb2c34d6b6c1b Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 24 Aug 2022 10:22:39 -0600 Subject: [PATCH] Increment follower count as well --- functions/src/follow-market.ts | 13 ------------ functions/src/index.ts | 2 +- ...follow.ts => on-update-contract-follow.ts} | 21 +++++++++++++++++++ .../scripts/backfill-contract-followers.ts | 11 +++++----- 4 files changed, 28 insertions(+), 19 deletions(-) rename functions/src/{on-delete-contract-follow.ts => on-update-contract-follow.ts} (55%) diff --git a/functions/src/follow-market.ts b/functions/src/follow-market.ts index 597b7d71..3fc05120 100644 --- a/functions/src/follow-market.ts +++ b/functions/src/follow-market.ts @@ -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), - }) } diff --git a/functions/src/index.ts b/functions/src/index.ts index e6d6977c..012ba241 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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' diff --git a/functions/src/on-delete-contract-follow.ts b/functions/src/on-update-contract-follow.ts similarity index 55% rename from functions/src/on-delete-contract-follow.ts rename to functions/src/on-update-contract-follow.ts index 4b9a9646..f7d54fe8 100644 --- a/functions/src/on-delete-contract-follow.ts +++ b/functions/src/on-update-contract-follow.ts @@ -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), + }) + }) diff --git a/functions/src/scripts/backfill-contract-followers.ts b/functions/src/scripts/backfill-contract-followers.ts index 65ea9a2c..9b936654 100644 --- a/functions/src/scripts/backfill-contract-followers.ts +++ b/functions/src/scripts/backfill-contract-followers.ts @@ -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`)