2022-06-22 16:35:50 +00:00
|
|
|
import {
|
2022-09-03 00:06:48 +00:00
|
|
|
collection,
|
|
|
|
collectionGroup,
|
2022-06-22 16:35:50 +00:00
|
|
|
deleteDoc,
|
2022-08-30 12:39:10 +00:00
|
|
|
deleteField,
|
2022-06-22 16:35:50 +00:00
|
|
|
doc,
|
2022-06-29 19:21:40 +00:00
|
|
|
getDocs,
|
2022-09-03 00:06:48 +00:00
|
|
|
onSnapshot,
|
2022-06-22 16:35:50 +00:00
|
|
|
query,
|
2022-09-03 00:06:48 +00:00
|
|
|
setDoc,
|
2022-06-22 16:35:50 +00:00
|
|
|
updateDoc,
|
|
|
|
where,
|
|
|
|
} from 'firebase/firestore'
|
2022-09-03 00:06:48 +00:00
|
|
|
import { uniq } from 'lodash'
|
2022-07-22 22:28:53 +00:00
|
|
|
import { Group, GROUP_CHAT_SLUG, GroupLink } from 'common/group'
|
2022-06-29 19:21:40 +00:00
|
|
|
import {
|
|
|
|
coll,
|
|
|
|
getValue,
|
|
|
|
getValues,
|
|
|
|
listenForValue,
|
|
|
|
listenForValues,
|
|
|
|
} from './utils'
|
2022-07-13 21:11:22 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-08-02 03:15:09 +00:00
|
|
|
import { updateContract } from 'web/lib/firebase/contracts'
|
2022-09-03 00:06:48 +00:00
|
|
|
import { db } from 'web/lib/firebase/init'
|
|
|
|
import { filterDefined } from 'common/util/array'
|
|
|
|
import { getUser } from 'web/lib/firebase/users'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-06-29 19:21:40 +00:00
|
|
|
export const groups = coll<Group>('groups')
|
2022-09-03 00:06:48 +00:00
|
|
|
export const groupMembers = (groupId: string) =>
|
|
|
|
collection(groups, groupId, 'groupMembers')
|
|
|
|
export const groupContracts = (groupId: string) =>
|
|
|
|
collection(groups, groupId, 'groupContracts')
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export function groupPath(
|
|
|
|
groupSlug: string,
|
2022-07-20 19:35:04 +00:00
|
|
|
subpath?:
|
|
|
|
| 'edit'
|
2022-07-21 23:17:02 +00:00
|
|
|
| 'markets'
|
2022-07-20 19:35:04 +00:00
|
|
|
| 'about'
|
|
|
|
| typeof GROUP_CHAT_SLUG
|
|
|
|
| 'leaderboards'
|
2022-06-22 16:35:50 +00:00
|
|
|
) {
|
|
|
|
return `/group/${groupSlug}${subpath ? `/${subpath}` : ''}`
|
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export type GroupContractDoc = { contractId: string; createdTime: number }
|
|
|
|
export type GroupMemberDoc = { userId: string; createdTime: number }
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function updateGroup(group: Group, updates: Partial<Group>) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return updateDoc(doc(groups, group.id), updates)
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 12:39:10 +00:00
|
|
|
export function deleteFieldFromGroup(group: Group, field: string) {
|
|
|
|
return updateDoc(doc(groups, group.id), { [field]: deleteField() })
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function deleteGroup(group: Group) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return deleteDoc(doc(groups, group.id))
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function listAllGroups() {
|
2022-06-29 19:21:40 +00:00
|
|
|
return getValues<Group>(groups)
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:34:10 +00:00
|
|
|
export async function listGroups(groupSlugs: string[]) {
|
|
|
|
return Promise.all(groupSlugs.map(getGroupBySlug))
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function listenForGroups(setGroups: (groups: Group[]) => void) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return listenForValues(groups, setGroups)
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function listenForGroupContractDocs(
|
|
|
|
groupId: string,
|
|
|
|
setContractDocs: (docs: GroupContractDoc[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues(groupContracts(groupId), setContractDocs)
|
|
|
|
}
|
|
|
|
|
2022-08-02 03:15:09 +00:00
|
|
|
export function listenForOpenGroups(setGroups: (groups: Group[]) => void) {
|
|
|
|
return listenForValues(
|
|
|
|
query(groups, where('anyoneCanJoin', '==', true)),
|
|
|
|
setGroups
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function getGroup(groupId: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return getValue<Group>(doc(groups, groupId))
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function getGroupContracts(groupId: string) {
|
|
|
|
return getValues<{ contractId: string; createdTime: number }>(
|
|
|
|
groupContracts(groupId)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export async function getGroupBySlug(slug: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const q = query(groups, where('slug', '==', slug))
|
|
|
|
const docs = (await getDocs(q)).docs
|
|
|
|
return docs.length === 0 ? null : docs[0].data()
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForGroup(
|
|
|
|
groupId: string,
|
|
|
|
setGroup: (group: Group | null) => void
|
|
|
|
) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return listenForValue(doc(groups, groupId), setGroup)
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function listenForMemberGroupIds(
|
2022-06-22 16:35:50 +00:00
|
|
|
userId: string,
|
2022-09-03 00:06:48 +00:00
|
|
|
setGroupIds: (groupIds: string[]) => void
|
2022-06-22 16:35:50 +00:00
|
|
|
) {
|
2022-09-03 00:06:48 +00:00
|
|
|
const q = query(
|
|
|
|
collectionGroup(db, 'groupMembers'),
|
|
|
|
where('userId', '==', userId)
|
|
|
|
)
|
|
|
|
return onSnapshot(q, { includeMetadataChanges: true }, (snapshot) => {
|
|
|
|
if (snapshot.metadata.fromCache) return
|
|
|
|
|
|
|
|
const values = snapshot.docs.map((doc) => doc.ref.parent.parent?.id)
|
|
|
|
|
|
|
|
setGroupIds(filterDefined(values))
|
2022-06-22 16:35:50 +00:00
|
|
|
})
|
|
|
|
}
|
2022-06-22 22:19:17 +00:00
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function listenForMemberGroups(
|
|
|
|
userId: string,
|
2022-06-22 22:19:17 +00:00
|
|
|
setGroups: (groups: Group[]) => void
|
|
|
|
) {
|
2022-09-03 00:06:48 +00:00
|
|
|
return listenForMemberGroupIds(userId, (groupIds) => {
|
|
|
|
return Promise.all(groupIds.map(getGroup)).then((groups) => {
|
|
|
|
setGroups(filterDefined(groups))
|
|
|
|
})
|
|
|
|
})
|
2022-06-22 22:19:17 +00:00
|
|
|
}
|
2022-06-29 16:00:43 +00:00
|
|
|
|
2022-07-18 16:40:44 +00:00
|
|
|
export async function addUserToGroupViaId(groupId: string, userId: string) {
|
2022-07-01 13:47:19 +00:00
|
|
|
// get group to get the member ids
|
2022-07-18 16:40:44 +00:00
|
|
|
const group = await getGroup(groupId)
|
2022-07-01 13:47:19 +00:00
|
|
|
if (!group) {
|
2022-07-18 16:40:44 +00:00
|
|
|
console.error(`Group not found: ${groupId}`)
|
2022-07-01 13:47:19 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-14 17:09:28 +00:00
|
|
|
return await joinGroup(group, userId)
|
2022-07-01 13:47:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 17:09:28 +00:00
|
|
|
export async function joinGroup(group: Group, userId: string): Promise<void> {
|
2022-09-03 00:06:48 +00:00
|
|
|
// create a new member document in grouoMembers collection
|
|
|
|
const memberDoc = doc(groupMembers(group.id), userId)
|
|
|
|
return await setDoc(memberDoc, {
|
|
|
|
userId,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
})
|
2022-06-29 16:00:43 +00:00
|
|
|
}
|
2022-07-14 13:53:41 +00:00
|
|
|
|
2022-07-14 17:09:28 +00:00
|
|
|
export async function leaveGroup(group: Group, userId: string): Promise<void> {
|
2022-09-03 00:06:48 +00:00
|
|
|
// delete the member document in groupMembers collection
|
|
|
|
const memberDoc = doc(groupMembers(group.id), userId)
|
|
|
|
return await deleteDoc(memberDoc)
|
2022-06-29 16:00:43 +00:00
|
|
|
}
|
2022-07-01 22:37:30 +00:00
|
|
|
|
2022-07-22 22:28:53 +00:00
|
|
|
export async function addContractToGroup(
|
|
|
|
group: Group,
|
|
|
|
contract: Contract,
|
|
|
|
userId: string
|
|
|
|
) {
|
2022-08-02 03:15:09 +00:00
|
|
|
const newGroupLinks = [
|
|
|
|
...(contract.groupLinks ?? []),
|
|
|
|
{
|
|
|
|
groupId: group.id,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
slug: group.slug,
|
|
|
|
userId,
|
|
|
|
name: group.name,
|
|
|
|
} as GroupLink,
|
|
|
|
]
|
|
|
|
// It's good to update the contract first, so the on-update-group trigger doesn't re-add them
|
|
|
|
await updateContract(contract.id, {
|
|
|
|
groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
|
|
|
|
groupLinks: newGroupLinks,
|
|
|
|
})
|
2022-07-26 01:27:43 +00:00
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
// create new contract document in groupContracts collection
|
|
|
|
const contractDoc = doc(groupContracts(group.id), contract.id)
|
|
|
|
await setDoc(contractDoc, {
|
|
|
|
contractId: contract.id,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
})
|
2022-07-13 21:11:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:34:10 +00:00
|
|
|
export async function removeContractFromGroup(
|
|
|
|
group: Group,
|
2022-09-03 00:06:48 +00:00
|
|
|
contract: Contract
|
2022-07-22 17:34:10 +00:00
|
|
|
) {
|
2022-07-26 01:27:43 +00:00
|
|
|
if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
|
|
|
|
const newGroupLinks = contract.groupLinks?.filter(
|
|
|
|
(link) => link.slug !== group.slug
|
|
|
|
)
|
|
|
|
await updateContract(contract.id, {
|
|
|
|
groupSlugs:
|
|
|
|
contract.groupSlugs?.filter((slug) => slug !== group.slug) ?? [],
|
|
|
|
groupLinks: newGroupLinks ?? [],
|
2022-07-22 17:34:10 +00:00
|
|
|
})
|
2022-07-26 01:27:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
// delete the contract document in groupContracts collection
|
|
|
|
const contractDoc = doc(groupContracts(group.id), contract.id)
|
|
|
|
await deleteDoc(contractDoc)
|
2022-07-01 22:37:30 +00:00
|
|
|
}
|
2022-09-01 14:29:56 +00:00
|
|
|
|
|
|
|
export function getGroupLinkToDisplay(contract: Contract) {
|
|
|
|
const { groupLinks } = contract
|
|
|
|
const sortedGroupLinks = groupLinks?.sort(
|
|
|
|
(a, b) => b.createdTime - a.createdTime
|
|
|
|
)
|
|
|
|
const groupCreatorAdded = sortedGroupLinks?.find(
|
|
|
|
(g) => g.userId === contract.creatorId
|
|
|
|
)
|
|
|
|
const groupToDisplay = groupCreatorAdded
|
|
|
|
? groupCreatorAdded
|
|
|
|
: sortedGroupLinks?.[0] ?? null
|
|
|
|
return groupToDisplay
|
|
|
|
}
|
2022-09-03 00:06:48 +00:00
|
|
|
|
|
|
|
export async function listMembers(group: Group) {
|
|
|
|
const members = await getValues<GroupMemberDoc>(groupMembers(group.id))
|
|
|
|
return await Promise.all(members.map((m) => m.userId).map(getUser))
|
|
|
|
}
|