import { collection, deleteDoc, doc, query, updateDoc, where, } from 'firebase/firestore' import { sortBy } from 'lodash' import { Group } from 'common/group' import { getContractFromId } from './contracts' import { db } from './init' import { getValue, getValues, listenForValue, listenForValues } from './utils' import { filterDefined } from 'common/util/array' const groupCollection = collection(db, 'groups') export function groupPath( groupSlug: string, subpath?: 'edit' | 'questions' | 'about' | 'chat' ) { return `/group/${groupSlug}${subpath ? `/${subpath}` : ''}` } export function updateGroup(group: Group, updates: Partial) { return updateDoc(doc(groupCollection, group.id), updates) } export function deleteGroup(group: Group) { return deleteDoc(doc(groupCollection, group.id)) } export async function listAllGroups() { return getValues(groupCollection) } export function listenForGroups(setGroups: (groups: Group[]) => void) { return listenForValues(groupCollection, setGroups) } export function getGroup(groupId: string) { return getValue(doc(groupCollection, groupId)) } export async function getGroupBySlug(slug: string) { const q = query(groupCollection, where('slug', '==', slug)) const groups = await getValues(q) return groups.length === 0 ? null : groups[0] } export async function getGroupContracts(group: Group) { const { contractIds } = group const contracts = filterDefined( await Promise.all( contractIds.map(async (contractId) => { return await getContractFromId(contractId) }) ) ) ?? [] return [...contracts] } export function listenForGroup( groupId: string, setGroup: (group: Group | null) => void ) { return listenForValue(doc(groupCollection, groupId), setGroup) } export function listenForMemberGroups( userId: string, setGroups: (groups: Group[]) => void ) { const q = query(groupCollection, where('memberIds', 'array-contains', userId)) return listenForValues(q, (groups) => { const sorted = sortBy(groups, [(group) => -group.mostRecentActivityTime]) setGroups(sorted) }) } export async function getGroupsWithContractId( contractId: string, setGroups: (groups: Group[]) => void ) { const q = query( groupCollection, where('contractIds', 'array-contains', contractId) ) const groups = await getValues(q) setGroups(groups) }