312b244e2a
* Reuse DAY_MS in update-metrics job * More concise transaction in cancelbet * Remove some meaningless awaits * Do less work in onCreateLiquidityProvision * Do less work in onCreateAnswer
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import * as admin from 'firebase-admin'
|
|
import { z } from 'zod'
|
|
import { APIError, newEndpoint, validate } from './api'
|
|
import { LimitBet } from '../../common/bet'
|
|
|
|
const bodySchema = z.object({
|
|
betId: z.string(),
|
|
})
|
|
|
|
export const cancelbet = newEndpoint({}, async (req, auth) => {
|
|
const { betId } = validate(bodySchema, req.body)
|
|
|
|
return await firestore.runTransaction(async (trans) => {
|
|
const snap = await trans.get(
|
|
firestore.collectionGroup('bets').where('id', '==', betId)
|
|
)
|
|
const betDoc = snap.docs[0]
|
|
if (!betDoc?.exists) throw new APIError(400, 'Bet not found.')
|
|
|
|
const bet = betDoc.data() as LimitBet
|
|
if (bet.userId !== auth.uid)
|
|
throw new APIError(400, 'Not authorized to cancel bet.')
|
|
if (bet.limitProb === undefined)
|
|
throw new APIError(400, 'Not a limit order: Cannot cancel.')
|
|
if (bet.isCancelled) throw new APIError(400, 'Bet already cancelled.')
|
|
|
|
trans.update(betDoc.ref, { isCancelled: true })
|
|
|
|
return { ...bet, isCancelled: true }
|
|
})
|
|
})
|
|
|
|
const firestore = admin.firestore()
|