No nested queries :(
This commit is contained in:
parent
1b2c0c6134
commit
7578cb24ad
|
@ -46,6 +46,7 @@ export type Contract<T extends AnyContractType = AnyContractType> = {
|
|||
|
||||
collectedFees: Fees
|
||||
|
||||
groupSlugs?: string[]
|
||||
groupLinks?: GroupLink[]
|
||||
uniqueBettorIds?: string[]
|
||||
uniqueBettorCount?: number
|
||||
|
|
|
@ -74,7 +74,7 @@ service cloud.firestore {
|
|||
match /contracts/{contractId} {
|
||||
allow read;
|
||||
allow update: if request.resource.data.diff(resource.data).affectedKeys()
|
||||
.hasOnly(['tags', 'lowercaseTags', 'groupLinks']);
|
||||
.hasOnly(['tags', 'lowercaseTags', 'groupSlugs', 'groupLinks']);
|
||||
allow update: if request.resource.data.diff(resource.data).affectedKeys()
|
||||
.hasOnly(['description', 'closeTime', 'question'])
|
||||
&& resource.data.creatorId == request.auth.uid;
|
||||
|
|
|
@ -14,8 +14,9 @@ export const onDeleteGroup = functions.firestore
|
|||
// get all contracts with this group's slug
|
||||
const contracts = await firestore
|
||||
.collection('contracts')
|
||||
.where('groupLinks.slug', '==', group.slug)
|
||||
.where('groupSlugs', 'array-contains', group.slug)
|
||||
.get()
|
||||
console.log("contracts with group's slug:", contracts)
|
||||
|
||||
for (const doc of contracts.docs) {
|
||||
const contract = doc.data() as Contract
|
||||
|
@ -24,8 +25,12 @@ export const onDeleteGroup = functions.firestore
|
|||
)
|
||||
|
||||
// remove the group from the contract
|
||||
await firestore.collection('contracts').doc(contract.id).update({
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
await firestore
|
||||
.collection('contracts')
|
||||
.doc(contract.id)
|
||||
.update({
|
||||
groupSlugs: contract.groupSlugs?.filter((s) => s !== group.slug),
|
||||
groupLinks: newGroupLinks ?? [],
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
@ -84,9 +84,13 @@ const convertCategoriesToGroupsInternal = async (categories: string[]) => {
|
|||
name: newGroup.name,
|
||||
} as GroupLink,
|
||||
]
|
||||
await adminFirestore.collection('contracts').doc(market.id).update({
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
await adminFirestore
|
||||
.collection('contracts')
|
||||
.doc(market.id)
|
||||
.update({
|
||||
groupSlugs: uniq([...(market.groupSlugs ?? []), newGroup.slug]),
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import { Contract } from 'common/contract'
|
|||
import { initAdmin } from 'functions/src/scripts/script-init'
|
||||
import * as admin from 'firebase-admin'
|
||||
import { filterDefined } from 'common/util/array'
|
||||
import { uniq } from 'lodash'
|
||||
|
||||
initAdmin()
|
||||
|
||||
|
@ -19,9 +20,11 @@ const addGroupIdToContracts = async () => {
|
|||
group.contractIds.includes(contract.id)
|
||||
)
|
||||
for (const contract of groupContracts) {
|
||||
const oldGroupLinks = contract.groupLinks ?? []
|
||||
const oldGroupLinks = contract.groupLinks?.filter(
|
||||
(l) => l.slug === group.slug
|
||||
)
|
||||
const newGroupLinks = filterDefined([
|
||||
...oldGroupLinks,
|
||||
...(oldGroupLinks ?? []),
|
||||
group.id
|
||||
? {
|
||||
slug: group.slug,
|
||||
|
@ -31,9 +34,13 @@ const addGroupIdToContracts = async () => {
|
|||
}
|
||||
: undefined,
|
||||
])
|
||||
await adminFirestore.collection('contracts').doc(contract.id).update({
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
await adminFirestore
|
||||
.collection('contracts')
|
||||
.doc(contract.id)
|
||||
.update({
|
||||
groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,11 +109,11 @@ export const useGroupsWithContract = (contract: Contract) => {
|
|||
const [groups, setGroups] = useState<Group[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
if (contract.groupLinks)
|
||||
listGroups(uniq(contract.groupLinks.map((g) => g.slug))).then((groups) =>
|
||||
if (contract.groupSlugs)
|
||||
listGroups(uniq(contract.groupSlugs)).then((groups) =>
|
||||
setGroups(filterDefined(groups))
|
||||
)
|
||||
}, [contract.groupLinks])
|
||||
}, [contract.groupSlugs])
|
||||
|
||||
return groups
|
||||
}
|
||||
|
|
|
@ -127,8 +127,9 @@ export async function listContracts(creatorId: string): Promise<Contract[]> {
|
|||
export async function listContractsByGroupSlug(
|
||||
slug: string
|
||||
): Promise<Contract[]> {
|
||||
const q = query(contracts, where('groupLinks.slug', '==', slug))
|
||||
const q = query(contracts, where('groupSlugs', 'array-contains', slug))
|
||||
const snapshot = await getDocs(q)
|
||||
console.log(snapshot.docs.map((doc) => doc.data()))
|
||||
return snapshot.docs.map((doc) => doc.data())
|
||||
}
|
||||
|
||||
|
|
|
@ -143,6 +143,7 @@ export async function addContractToGroup(
|
|||
]
|
||||
|
||||
await updateContract(contract.id, {
|
||||
groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
return await updateGroup(group, {
|
||||
|
@ -164,6 +165,7 @@ export async function removeContractFromGroup(
|
|||
const newGroupLinks =
|
||||
contract.groupLinks?.filter((link) => link.slug !== group.slug) ?? []
|
||||
await updateContract(contract.id, {
|
||||
groupSlugs: contract.groupSlugs?.filter((slug) => slug !== group.slug),
|
||||
groupLinks: newGroupLinks,
|
||||
})
|
||||
const newContractIds = group.contractIds.filter((id) => id !== contract.id)
|
||||
|
|
Loading…
Reference in New Issue
Block a user