Use getAll
Firestore technology to improve some code (#612)
This commit is contained in:
parent
90d7f55c6d
commit
7dea9cbfa8
|
@ -41,10 +41,7 @@ export const placebet = newEndpoint({}, async (req, auth) => {
|
||||||
log('Inside main transaction.')
|
log('Inside main transaction.')
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const userDoc = firestore.doc(`users/${auth.uid}`)
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
||||||
const [contractSnap, userSnap] = await Promise.all([
|
const [contractSnap, userSnap] = await trans.getAll(contractDoc, userDoc)
|
||||||
trans.get(contractDoc),
|
|
||||||
trans.get(userDoc),
|
|
||||||
])
|
|
||||||
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
||||||
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
||||||
log('Loaded user and contract snapshots.')
|
log('Loaded user and contract snapshots.')
|
||||||
|
|
|
@ -21,11 +21,11 @@ export const sellbet = newEndpoint({}, async (req, auth) => {
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const userDoc = firestore.doc(`users/${auth.uid}`)
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
||||||
const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`)
|
const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`)
|
||||||
const [contractSnap, userSnap, betSnap] = await Promise.all([
|
const [contractSnap, userSnap, betSnap] = await transaction.getAll(
|
||||||
transaction.get(contractDoc),
|
contractDoc,
|
||||||
transaction.get(userDoc),
|
userDoc,
|
||||||
transaction.get(betDoc),
|
betDoc
|
||||||
])
|
)
|
||||||
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
||||||
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
||||||
if (!betSnap.exists) throw new APIError(400, 'Bet not found.')
|
if (!betSnap.exists) throw new APIError(400, 'Bet not found.')
|
||||||
|
|
|
@ -24,9 +24,8 @@ export const sellshares = newEndpoint({}, async (req, auth) => {
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const userDoc = firestore.doc(`users/${auth.uid}`)
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
||||||
const betsQ = contractDoc.collection('bets').where('userId', '==', auth.uid)
|
const betsQ = contractDoc.collection('bets').where('userId', '==', auth.uid)
|
||||||
const [contractSnap, userSnap, userBets] = await Promise.all([
|
const [[contractSnap, userSnap], userBets] = await Promise.all([
|
||||||
transaction.get(contractDoc),
|
transaction.getAll(contractDoc, userDoc),
|
||||||
transaction.get(userDoc),
|
|
||||||
getValues<Bet>(betsQ), // TODO: why is this not in the transaction??
|
getValues<Bet>(betsQ), // TODO: why is this not in the transaction??
|
||||||
])
|
])
|
||||||
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user