2022-07-05 23:18:37 +00:00
|
|
|
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'
|
2022-07-15 01:51:38 +00:00
|
|
|
import { createNotification } from './create-notification'
|
2022-07-05 23:18:37 +00:00
|
|
|
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({
|
2022-07-15 01:51:38 +00:00
|
|
|
mostRecentActivityTime: comment.createdTime,
|
2022-07-05 23:18:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
group.memberIds.map(async (memberId) => {
|
2022-07-15 01:51:38 +00:00
|
|
|
return await createNotification(
|
|
|
|
comment.id,
|
|
|
|
'comment',
|
|
|
|
'created',
|
2022-07-05 23:18:37 +00:00
|
|
|
creatorSnapshot.data() as User,
|
2022-07-15 01:51:38 +00:00
|
|
|
eventId,
|
|
|
|
comment.text,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
2022-07-05 23:18:37 +00:00
|
|
|
memberId,
|
2022-07-15 01:51:38 +00:00
|
|
|
`/group/${group.slug}`,
|
|
|
|
`${group.name}`
|
2022-07-05 23:18:37 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|