53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
|
import * as functions from 'firebase-functions'
|
||
|
import { Comment } from '../../common/comment'
|
||
|
import * as admin from 'firebase-admin'
|
||
|
import { Group } from '../../common/group'
|
||
|
import { User } from '../../common/user'
|
||
|
import { createNotification } from './create-notification'
|
||
|
const firestore = admin.firestore()
|
||
|
|
||
|
export const onCreateCommentOnGroup = functions.firestore
|
||
|
.document('groups/{groupId}/comments/{commentId}')
|
||
|
.onCreate(async (change, context) => {
|
||
|
const { eventId } = context
|
||
|
const { groupId } = context.params as {
|
||
|
groupId: string
|
||
|
}
|
||
|
|
||
|
const comment = change.data() as Comment
|
||
|
const creatorSnapshot = await firestore
|
||
|
.collection('users')
|
||
|
.doc(comment.userId)
|
||
|
.get()
|
||
|
if (!creatorSnapshot.exists) throw new Error('Could not find user')
|
||
|
|
||
|
const groupSnapshot = await firestore
|
||
|
.collection('groups')
|
||
|
.doc(groupId)
|
||
|
.get()
|
||
|
if (!groupSnapshot.exists) throw new Error('Could not find group')
|
||
|
|
||
|
const group = groupSnapshot.data() as Group
|
||
|
await firestore.collection('groups').doc(groupId).update({
|
||
|
mostRecentActivityTime: comment.createdTime,
|
||
|
})
|
||
|
|
||
|
await Promise.all(
|
||
|
group.memberIds.map(async (memberId) => {
|
||
|
return await createNotification(
|
||
|
comment.id,
|
||
|
'comment',
|
||
|
'created',
|
||
|
creatorSnapshot.data() as User,
|
||
|
eventId,
|
||
|
comment.text,
|
||
|
undefined,
|
||
|
undefined,
|
||
|
memberId,
|
||
|
`/group/${group.slug}`,
|
||
|
`${group.name}`
|
||
|
)
|
||
|
})
|
||
|
)
|
||
|
})
|