Store view counts & last viewed time

This commit is contained in:
James Grugett 2022-04-27 17:26:33 -04:00
parent 53c79f41a3
commit d639e73284
2 changed files with 25 additions and 0 deletions

View File

@ -15,6 +15,7 @@ export * from './create-answer'
export * from './on-create-comment'
export * from './on-fold-follow'
export * from './on-fold-delete'
export * from './on-view'
export * from './unsubscribe'
export * from './update-contract-metrics'
export * from './update-user-metrics'

24
functions/src/on-view.ts Normal file
View File

@ -0,0 +1,24 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { View } from '../../common/tracking'
const firestore = admin.firestore()
export const onView = functions.firestore
.document('private-users/{userId}/views/{viewId}')
.onCreate(async (snapshot, context) => {
const { userId } = context.params
const { contractId, timestamp } = snapshot.data() as View
await firestore
.doc(`private-users/${userId}/cached/viewCounts`)
.set(
{ [contractId]: admin.firestore.FieldValue.increment(1) },
{ merge: true }
)
await firestore
.doc(`private-users/${userId}/cached/lastViewed`)
.set({ [contractId]: timestamp }, { merge: true })
})