2022-06-22 16:35:50 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import { Group } from '../../common/group'
|
2022-08-02 03:15:09 +00:00
|
|
|
import { getContract } from './utils'
|
|
|
|
import { uniq } from 'lodash'
|
2022-06-22 16:35:50 +00:00
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
export const onUpdateGroup = functions.firestore
|
|
|
|
.document('groups/{groupId}')
|
|
|
|
.onUpdate(async (change) => {
|
|
|
|
const prevGroup = change.before.data() as Group
|
|
|
|
const group = change.after.data() as Group
|
|
|
|
|
2022-08-02 03:15:09 +00:00
|
|
|
// Ignore the activity update we just made
|
2022-06-22 16:35:50 +00:00
|
|
|
if (prevGroup.mostRecentActivityTime !== group.mostRecentActivityTime)
|
|
|
|
return
|
2022-07-15 12:52:08 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
await firestore
|
|
|
|
.collection('groups')
|
|
|
|
.doc(group.id)
|
|
|
|
.update({ mostRecentActivityTime: Date.now() })
|
|
|
|
})
|
2022-08-02 03:15:09 +00:00
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export const onCreateGroupContract = functions.firestore
|
|
|
|
.document('groups/{groupId}/groupContracts/{contractId}')
|
|
|
|
.onCreate(async (change) => {
|
|
|
|
const groupId = change.ref.parent.parent?.id
|
|
|
|
if (groupId)
|
|
|
|
await firestore
|
|
|
|
.collection('groups')
|
|
|
|
.doc(groupId)
|
|
|
|
.update({
|
|
|
|
mostRecentContractAddedTime: Date.now(),
|
|
|
|
totalContracts: admin.firestore.FieldValue.increment(1),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
export const onDeleteGroupContract = functions.firestore
|
|
|
|
.document('groups/{groupId}/groupContracts/{contractId}')
|
|
|
|
.onDelete(async (change) => {
|
|
|
|
const groupId = change.ref.parent.parent?.id
|
|
|
|
if (groupId)
|
|
|
|
await firestore
|
|
|
|
.collection('groups')
|
|
|
|
.doc(groupId)
|
|
|
|
.update({
|
|
|
|
mostRecentContractAddedTime: Date.now(),
|
|
|
|
totalContracts: admin.firestore.FieldValue.increment(-1),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
export const onCreateGroupMember = functions.firestore
|
|
|
|
.document('groups/{groupId}/groupMembers/{memberId}')
|
|
|
|
.onCreate(async (change) => {
|
|
|
|
const groupId = change.ref.parent.parent?.id
|
|
|
|
if (groupId)
|
|
|
|
await firestore
|
|
|
|
.collection('groups')
|
|
|
|
.doc(groupId)
|
|
|
|
.update({
|
|
|
|
mostRecentActivityTime: Date.now(),
|
|
|
|
totalMembers: admin.firestore.FieldValue.increment(1),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
export const onDeleteGroupMember = functions.firestore
|
|
|
|
.document('groups/{groupId}/groupMembers/{memberId}')
|
|
|
|
.onDelete(async (change) => {
|
|
|
|
const groupId = change.ref.parent.parent?.id
|
|
|
|
if (groupId)
|
|
|
|
await firestore
|
|
|
|
.collection('groups')
|
|
|
|
.doc(groupId)
|
|
|
|
.update({
|
|
|
|
mostRecentActivityTime: Date.now(),
|
|
|
|
totalMembers: admin.firestore.FieldValue.increment(-1),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-08-02 03:15:09 +00:00
|
|
|
export async function removeGroupLinks(group: Group, contractIds: string[]) {
|
|
|
|
for (const contractId of contractIds) {
|
|
|
|
const contract = await getContract(contractId)
|
|
|
|
await firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(contractId)
|
|
|
|
.update({
|
|
|
|
groupSlugs: uniq([
|
|
|
|
...(contract?.groupSlugs?.filter((slug) => slug !== group.slug) ??
|
|
|
|
[]),
|
|
|
|
]),
|
|
|
|
groupLinks: [
|
|
|
|
...(contract?.groupLinks?.filter(
|
|
|
|
(link) => link.groupId !== group.id
|
|
|
|
) ?? []),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|