Update contract's lastBetTime and lastCommentTime on new bets and comments.
This commit is contained in:
		
							parent
							
								
									57fb9baed6
								
							
						
					
					
						commit
						f357b6e92e
					
				|  | @ -21,6 +21,8 @@ export type FullContract< | |||
| 
 | ||||
|   createdTime: number // Milliseconds since epoch
 | ||||
|   lastUpdatedTime: number // If the question or description was changed
 | ||||
|   lastBetTime?: number | ||||
|   lastCommentTime?: number | ||||
|   closeTime?: number // When no more trading is allowed
 | ||||
| 
 | ||||
|   isResolved: boolean | ||||
|  |  | |||
|  | @ -12,6 +12,7 @@ export * from './create-contract' | |||
| export * from './create-user' | ||||
| export * from './create-fold' | ||||
| export * from './create-answer' | ||||
| export * from './on-create-bet' | ||||
| export * from './on-create-comment' | ||||
| export * from './on-fold-follow' | ||||
| export * from './on-fold-delete' | ||||
|  |  | |||
							
								
								
									
										27
									
								
								functions/src/on-create-bet.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								functions/src/on-create-bet.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,27 @@ | |||
| import * as functions from 'firebase-functions' | ||||
| import * as admin from 'firebase-admin' | ||||
| import * as _ from 'lodash' | ||||
| 
 | ||||
| import { getContract } from './utils' | ||||
| import { Bet } from '../../common/bet' | ||||
| 
 | ||||
| const firestore = admin.firestore() | ||||
| 
 | ||||
| export const onCreateBet = functions.firestore | ||||
|   .document('contracts/{contractId}/bets/{betId}') | ||||
|   .onCreate(async (change, context) => { | ||||
|     const { contractId } = context.params as { | ||||
|       contractId: string | ||||
|     } | ||||
| 
 | ||||
|     const contract = await getContract(contractId) | ||||
|     if (!contract) | ||||
|       throw new Error('Could not find contract corresponding with bet') | ||||
| 
 | ||||
|     const bet = change.data() as Bet | ||||
| 
 | ||||
|     await firestore | ||||
|       .collection('contracts') | ||||
|       .doc(contract.id) | ||||
|       .update({ lastBetTime: bet.createdTime }) | ||||
|   }) | ||||
|  | @ -18,12 +18,18 @@ export const onCreateComment = functions.firestore | |||
|     } | ||||
| 
 | ||||
|     const contract = await getContract(contractId) | ||||
|     if (!contract) return | ||||
|     if (!contract) | ||||
|       throw new Error('Could not find contract corresponding with comment') | ||||
| 
 | ||||
|     const comment = change.data() as Comment | ||||
| 
 | ||||
|     const commentCreator = await getUser(comment.userId) | ||||
|     if (!commentCreator) return | ||||
|     if (!commentCreator) throw new Error('Could not find contract creator') | ||||
| 
 | ||||
|     await firestore | ||||
|       .collection('contracts') | ||||
|       .doc(contract.id) | ||||
|       .update({ lastCommentTime: comment.createdTime }) | ||||
| 
 | ||||
|     let bet: Bet | undefined | ||||
|     let answer: Answer | undefined | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user