f533d9bfcb
* Fetch balance of users with open limit orders & cancel orders with insufficient balance
* Fix imports
* Fix bugs
* Fix a bug
* Remove redundant cast
* buttons overlaying content fix (#1005)
* buttons overlaying content fix
* stats: round DAU number
* made set width for portfolio/profit fields (#1006)
* tournaments: included resolved markets
* made delete red, moved button for regular posts (#1008)
* Fix localstorage saved user being overwritten on every page load
* Market page: Show no right panel while user loading
* Don't flash sign in button if user is loading
* election map coloring
* market group modal scroll fix (#1009)
* midterms: posititoning, make mobile friendly
* Un-daisy share buttons (#1010)
* Make embed and challenge buttons non-daisyui
* Allow link Buttons. Change tweet, dupe buttons.
* lint
* don't insert extra lines when upload photos
* Map fixes (#1011)
* usa map: fix sizing
* useSetIframeBackbroundColor
* preload contracts
* seo
* remove hook
* turn off sprig on dev
* Render timestamp only on client to prevent error of server not matching client
* Make sized container have default height so graph doesn't jump
* midterms: use null in static props
* Create common card component (#1012)
* Create common card component
* lint
* add key prop to pills
* redirect to /home after login
* create market: use transaction
* card: reduce border size
* Update groupContracts in db trigger
* Default sort to best
* Save comment sort per user rather than per contract
* Refactor Pinned Items into a reusable component
* Revert "create market: use transaction"
This reverts commit e1f24f24a9
.
* Mark @v with a (Bot) label
* fix padding on daily movers
* fix type errors
* Wrap sprig init in check for window
* unindex date-docs from search engines
* Auto-prettification
* compute elasticity
* change dpm elasticity
* Fix google lighthouse issues (#1013)
* don't hide free response panel on open resolve
* liquidity sort
* Limit order trade log: '/' to 'of'. Remove 'of' in 'of YES'.
* Date doc: Toggle to disable creating a prediction market
* Listen for date doc changes
* Fix merge error
* Don't cancel all a users limit orders if they go negative
Co-authored-by: ingawei <46611122+ingawei@users.noreply.github.com>
Co-authored-by: mantikoros <sgrugett@gmail.com>
Co-authored-by: Sinclair Chen <abc.sinclair@gmail.com>
Co-authored-by: mantikoros <95266179+mantikoros@users.noreply.github.com>
Co-authored-by: Ian Philips <iansphilips@gmail.com>
Co-authored-by: Pico2x <pico2x@gmail.com>
Co-authored-by: Austin Chen <akrolsmir@gmail.com>
Co-authored-by: sipec <sipec@users.noreply.github.com>
120 lines
4.1 KiB
TypeScript
120 lines
4.1 KiB
TypeScript
import * as functions from 'firebase-functions'
|
|
import * as admin from 'firebase-admin'
|
|
import { User } from '../../common/user'
|
|
import { HOUSE_LIQUIDITY_PROVIDER_ID } from '../../common/antes'
|
|
import { createReferralNotification } from './create-notification'
|
|
import { ReferralTxn } from '../../common/txn'
|
|
import { Contract } from '../../common/contract'
|
|
import { Group } from '../../common/group'
|
|
import { REFERRAL_AMOUNT } from '../../common/economy'
|
|
const firestore = admin.firestore()
|
|
|
|
export const onUpdateUser = functions.firestore
|
|
.document('users/{userId}')
|
|
.onUpdate(async (change, context) => {
|
|
const prevUser = change.before.data() as User
|
|
const user = change.after.data() as User
|
|
const { eventId } = context
|
|
|
|
if (prevUser.referredByUserId !== user.referredByUserId) {
|
|
await handleUserUpdatedReferral(user, eventId)
|
|
}
|
|
})
|
|
|
|
async function handleUserUpdatedReferral(user: User, eventId: string) {
|
|
// Only create a referral txn if the user has a referredByUserId
|
|
if (!user.referredByUserId) {
|
|
console.log(`Not set: referredByUserId ${user.referredByUserId}`)
|
|
return
|
|
}
|
|
const referredByUserId = user.referredByUserId
|
|
|
|
await firestore.runTransaction(async (transaction) => {
|
|
// get user that referred this user
|
|
const referredByUserDoc = firestore.doc(`users/${referredByUserId}`)
|
|
const referredByUserSnap = await transaction.get(referredByUserDoc)
|
|
if (!referredByUserSnap.exists) {
|
|
console.log(`User ${referredByUserId} not found`)
|
|
return
|
|
}
|
|
const referredByUser = referredByUserSnap.data() as User
|
|
|
|
let referredByContract: Contract | undefined = undefined
|
|
if (user.referredByContractId) {
|
|
const referredByContractDoc = firestore.doc(
|
|
`contracts/${user.referredByContractId}`
|
|
)
|
|
referredByContract = await transaction
|
|
.get(referredByContractDoc)
|
|
.then((snap) => snap.data() as Contract)
|
|
}
|
|
console.log(`referredByContract: ${referredByContract}`)
|
|
|
|
let referredByGroup: Group | undefined = undefined
|
|
if (user.referredByGroupId) {
|
|
const referredByGroupDoc = firestore.doc(
|
|
`groups/${user.referredByGroupId}`
|
|
)
|
|
referredByGroup = await transaction
|
|
.get(referredByGroupDoc)
|
|
.then((snap) => snap.data() as Group)
|
|
}
|
|
console.log(`referredByGroup: ${referredByGroup}`)
|
|
|
|
const txns = (
|
|
await firestore
|
|
.collection('txns')
|
|
.where('toId', '==', referredByUserId)
|
|
.where('category', '==', 'REFERRAL')
|
|
.get()
|
|
).docs.map((txn) => txn.ref)
|
|
if (txns.length > 0) {
|
|
const referralTxns = await transaction.getAll(...txns).catch((err) => {
|
|
console.error('error getting txns:', err)
|
|
throw err
|
|
})
|
|
// If the referring user already has a referral txn due to referring this user, halt
|
|
if (
|
|
referralTxns.map((txn) => txn.data()?.description).includes(user.id)
|
|
) {
|
|
console.log('found referral txn with the same details, aborting')
|
|
return
|
|
}
|
|
}
|
|
console.log('creating referral txns')
|
|
const fromId = HOUSE_LIQUIDITY_PROVIDER_ID
|
|
|
|
// if they're updating their referredId, create a txn for both
|
|
const txn: ReferralTxn = {
|
|
id: eventId,
|
|
createdTime: Date.now(),
|
|
fromId,
|
|
fromType: 'BANK',
|
|
toId: referredByUserId,
|
|
toType: 'USER',
|
|
amount: REFERRAL_AMOUNT,
|
|
token: 'M$',
|
|
category: 'REFERRAL',
|
|
description: `Referred new user id: ${user.id} for ${REFERRAL_AMOUNT}`,
|
|
}
|
|
|
|
const txnDoc = firestore.collection(`txns/`).doc(txn.id)
|
|
transaction.set(txnDoc, txn)
|
|
console.log('created referral with txn id:', txn.id)
|
|
// We're currently not subtracting M$ from the house, not sure if we want to for accounting purposes.
|
|
transaction.update(referredByUserDoc, {
|
|
balance: referredByUser.balance + REFERRAL_AMOUNT,
|
|
totalDeposits: referredByUser.totalDeposits + REFERRAL_AMOUNT,
|
|
})
|
|
|
|
await createReferralNotification(
|
|
referredByUser,
|
|
user,
|
|
eventId,
|
|
txn.amount.toString(),
|
|
referredByContract,
|
|
referredByGroup
|
|
)
|
|
})
|
|
}
|