diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index 17feb127..44ced6a8 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -14,7 +14,7 @@ import { import { slugify } from '../../common/util/slugify' import { randomString } from '../../common/util/random' -import { chargeUser } from './utils' +import { chargeUser, getContract } from './utils' import { APIError, newEndpoint, validate, zTimestamp } from './api' import { @@ -28,12 +28,11 @@ import { Answer, getNoneAnswer } from '../../common/answer' import { getNewContract } from '../../common/new-contract' import { NUMERIC_BUCKET_COUNT } from '../../common/numeric-constants' import { User } from '../../common/user' -import { Group, MAX_ID_LENGTH } from '../../common/group' +import { Group, GroupLink, MAX_ID_LENGTH } from '../../common/group' import { getPseudoProbability } from '../../common/pseudo-numeric' import { JSONContent } from '@tiptap/core' import { uniq, zip } from 'lodash' import { Bet } from '../../common/bet' -import { createGroupLinks } from './on-update-group' const descScehma: z.ZodType = z.lazy(() => z.intersection( @@ -291,3 +290,38 @@ export async function getContractFromSlug(slug: string) { return snap.empty ? undefined : (snap.docs[0].data() as Contract) } + +async function createGroupLinks( + group: Group, + contractIds: string[], + userId: string +) { + for (const contractId of contractIds) { + const contract = await getContract(contractId) + if (!contract?.groupSlugs?.includes(group.slug)) { + await firestore + .collection('contracts') + .doc(contractId) + .update({ + groupSlugs: uniq([group.slug, ...(contract?.groupSlugs ?? [])]), + }) + } + if (!contract?.groupLinks?.map((gl) => gl.groupId).includes(group.id)) { + await firestore + .collection('contracts') + .doc(contractId) + .update({ + groupLinks: [ + { + groupId: group.id, + name: group.name, + slug: group.slug, + userId, + createdTime: Date.now(), + } as GroupLink, + ...(contract?.groupLinks ?? []), + ], + }) + } + } +} diff --git a/functions/src/on-update-group.ts b/functions/src/on-update-group.ts index ad608cb8..7e6a5697 100644 --- a/functions/src/on-update-group.ts +++ b/functions/src/on-update-group.ts @@ -1,9 +1,8 @@ import * as functions from 'firebase-functions' import * as admin from 'firebase-admin' -import { Group, GroupLink } from '../../common/group' -import { getContract, log } from './utils' +import { Group } from '../../common/group' +import { getContract } from './utils' import { uniq } from 'lodash' -import { removeUndefinedProps } from '../../common/util/object' const firestore = admin.firestore() export const onUpdateGroup = functions.firestore @@ -31,41 +30,6 @@ export const onUpdateGroup = functions.firestore .update({ mostRecentActivityTime: Date.now() }) }) -export async function createGroupLinks( - group: Group, - contractIds: string[], - userId?: string -) { - for (const contractId of contractIds) { - const contract = await getContract(contractId) - if (!contract?.groupSlugs?.includes(group.slug)) { - await firestore - .collection('contracts') - .doc(contractId) - .update({ - groupSlugs: uniq([group.slug, ...(contract?.groupSlugs ?? [])]), - }) - } - if (!contract?.groupLinks?.map((gl) => gl.groupId).includes(group.id)) { - await firestore - .collection('contracts') - .doc(contractId) - .update({ - groupLinks: [ - removeUndefinedProps({ - groupId: group.id, - name: group.name, - slug: group.slug, - userId, - createdTime: Date.now(), - }) as GroupLink, - ...(contract?.groupLinks ?? []), - ], - }) - } - } -} - export async function removeGroupLinks(group: Group, contractIds: string[]) { for (const contractId of contractIds) { const contract = await getContract(contractId)