Add user to followers in place-bet and sell-shares

This commit is contained in:
Ian Philips 2022-08-24 08:59:40 -06:00
parent 60ddc595a0
commit 8402ceb916
8 changed files with 42 additions and 16 deletions

View File

@ -1,30 +1,49 @@
import { Contract } from '../../common/lib/contract'
import { User } from '../../common/lib/user'
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { FieldValue } from 'firebase-admin/firestore' import { FieldValue } from 'firebase-admin/firestore'
const firestore = admin.firestore() const firestore = admin.firestore()
export const addUserToContractFollowers = async ( export const addUserToContractFollowers = async (
contract: Contract, contractId: string,
user: User userId: string
) => { ) => {
const followerDoc = await firestore const followerDoc = await firestore
.collection(`contracts/${contract.id}/follows`) .collection(`contracts/${contractId}/follows`)
.doc(user.id) .doc(userId)
.get() .get()
if (followerDoc.exists) return if (followerDoc.exists) return
await firestore await firestore
.collection(`contracts/${contract.id}/follows`) .collection(`contracts/${contractId}/follows`)
.doc(user.id) .doc(userId)
.set({ .set({
id: user.id, id: userId,
createdTime: Date.now(), createdTime: Date.now(),
}) })
await firestore await firestore
.collection(`contracts`) .collection(`contracts`)
.doc(contract.id) .doc(contractId)
.update({ .update({
followerCount: FieldValue.increment(1), followerCount: FieldValue.increment(1),
}) })
} }
export const removeUserFromContractFollowers = async (
contractId: string,
userId: string
) => {
const followerDoc = await firestore
.collection(`contracts/${contractId}/follows`)
.doc(userId)
.get()
if (!followerDoc.exists) return
await firestore
.collection(`contracts/${contractId}/follows`)
.doc(userId)
.delete()
await firestore
.collection(`contracts`)
.doc(contractId)
.update({
followerCount: FieldValue.increment(-1),
})
}

View File

@ -21,7 +21,7 @@ export const onCreateAnswer = functions.firestore
const answerCreator = await getUser(answer.userId) const answerCreator = await getUser(answer.userId)
if (!answerCreator) throw new Error('Could not find answer creator') if (!answerCreator) throw new Error('Could not find answer creator')
await addUserToContractFollowers(contract, answerCreator) await addUserToContractFollowers(contract.id, answerCreator.id)
await createCommentOrAnswerOrUpdatedContractNotification( await createCommentOrAnswerOrUpdatedContractNotification(
answer.id, answer.id,
'answer', 'answer',

View File

@ -58,7 +58,7 @@ export const onCreateBet = functions.firestore
const bettor = await getUser(bet.userId) const bettor = await getUser(bet.userId)
if (!bettor) return if (!bettor) return
await addUserToContractFollowers(contract, bettor) await addUserToContractFollowers(contract.id, bettor.id)
await updateUniqueBettorsAndGiveCreatorBonus(contract, eventId, bet.userId) await updateUniqueBettorsAndGiveCreatorBonus(contract, eventId, bet.userId)
await notifyFills(bet, contract, eventId, bettor) await notifyFills(bet, contract, eventId, bettor)
await updateBettingStreak(bettor, bet, contract, eventId) await updateBettingStreak(bettor, bet, contract, eventId)

View File

@ -36,7 +36,7 @@ export const onCreateCommentOnContract = functions
const commentCreator = await getUser(comment.userId) const commentCreator = await getUser(comment.userId)
if (!commentCreator) throw new Error('Could not find comment creator') if (!commentCreator) throw new Error('Could not find comment creator')
await addUserToContractFollowers(contract, commentCreator) await addUserToContractFollowers(contract.id, commentCreator.id)
await firestore await firestore
.collection('contracts') .collection('contracts')

View File

@ -19,7 +19,7 @@ export const onCreateContract = functions
const desc = contract.description as JSONContent const desc = contract.description as JSONContent
const mentioned = parseMentions(desc) const mentioned = parseMentions(desc)
await addUserToContractFollowers(contract, contractCreator) await addUserToContractFollowers(contract.id, contractCreator.id)
await createNotification( await createNotification(
contract.id, contract.id,

View File

@ -19,7 +19,7 @@ export const onCreateLiquidityProvision = functions.firestore
const liquidityProvider = await getUser(liquidity.userId) const liquidityProvider = await getUser(liquidity.userId)
if (!liquidityProvider) throw new Error('Could not find liquidity provider') if (!liquidityProvider) throw new Error('Could not find liquidity provider')
await addUserToContractFollowers(contract, liquidityProvider) await addUserToContractFollowers(contract.id, liquidityProvider.id)
await createNotification( await createNotification(
contract.id, contract.id,

View File

@ -22,6 +22,7 @@ import { LimitBet } from '../../common/bet'
import { floatingEqual } from '../../common/util/math' import { floatingEqual } from '../../common/util/math'
import { redeemShares } from './redeem-shares' import { redeemShares } from './redeem-shares'
import { log } from './utils' import { log } from './utils'
import { addUserToContractFollowers } from 'functions/src/follow-market'
const bodySchema = z.object({ const bodySchema = z.object({
contractId: z.string(), contractId: z.string(),
@ -167,6 +168,8 @@ export const placebet = newEndpoint({}, async (req, auth) => {
return { betId: betDoc.id, makers, newBet } return { betId: betDoc.id, makers, newBet }
}) })
await addUserToContractFollowers(contractId, auth.uid)
log('Main transaction finished.') log('Main transaction finished.')
if (result.newBet.amount !== 0) { if (result.newBet.amount !== 0) {

View File

@ -13,6 +13,7 @@ import { floatingEqual, floatingLesserEqual } from '../../common/util/math'
import { getUnfilledBetsQuery, updateMakers } from './place-bet' import { getUnfilledBetsQuery, updateMakers } from './place-bet'
import { FieldValue } from 'firebase-admin/firestore' import { FieldValue } from 'firebase-admin/firestore'
import { redeemShares } from './redeem-shares' import { redeemShares } from './redeem-shares'
import { removeUserFromContractFollowers } from 'functions/src/follow-market'
const bodySchema = z.object({ const bodySchema = z.object({
contractId: z.string(), contractId: z.string(),
@ -123,9 +124,12 @@ export const sellshares = newEndpoint({}, async (req, auth) => {
}) })
) )
return { newBet, makers } return { newBet, makers, maxShares, soldShares }
}) })
if (result.maxShares === result.soldShares) {
await removeUserFromContractFollowers(contractId, auth.uid)
}
const userIds = uniq(result.makers.map((maker) => maker.bet.userId)) const userIds = uniq(result.makers.map((maker) => maker.bet.userId))
await Promise.all(userIds.map((userId) => redeemShares(userId, contractId))) await Promise.all(userIds.map((userId) => redeemShares(userId, contractId)))
log('Share redemption transaction finished.') log('Share redemption transaction finished.')