Store view counts & last viewed time
This commit is contained in:
		
							parent
							
								
									53c79f41a3
								
							
						
					
					
						commit
						d639e73284
					
				|  | @ -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
									
								
							
							
						
						
									
										24
									
								
								functions/src/on-view.ts
									
									
									
									
									
										Normal 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 }) | ||||
|   }) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user