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