fix: update fund investment logic to decrement sharesOutstanding and withdraw cash from fund
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m24s

This commit is contained in:
2026-03-20 15:28:59 -04:00
parent 898f99049d
commit c5076e330c
2 changed files with 48 additions and 14 deletions
@@ -36,6 +36,7 @@ export async function POST(
shares: true,
fund: {
select: {
userId: true,
sharesOutstanding: true,
user: {
select: {
@@ -92,15 +93,31 @@ export async function POST(
}, 0)
const totalValue = user.balance + portfolioValue + fundInvestmentValue
// Forfeit all fund investments — decrement each fund's sharesOutstanding
// Forfeit all fund investments — decrement sharesOutstanding and withdraw cash from fund
const fundUpdates = user.fundInvestments
.filter((inv) => inv.shares > 0)
.map((inv) =>
prisma.hedgeFund.update({
where: { id: inv.fundId },
data: { sharesOutstanding: { decrement: inv.shares } },
}),
)
.flatMap((inv) => {
const fundPortfolioValue = inv.fund.user.positions.reduce((psum, p) => {
const val =
p.positionType === 'LONG'
? p.shares * p.hashtag.currentPrice
: (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares
return psum + val
}, 0)
const fundTotalValue = inv.fund.user.balance + fundPortfolioValue
const nav = calcFundNav(fundTotalValue, inv.fund.sharesOutstanding)
const payout = inv.shares * nav
return [
prisma.hedgeFund.update({
where: { id: inv.fundId },
data: { sharesOutstanding: { decrement: inv.shares } },
}),
prisma.user.update({
where: { id: inv.fund.userId },
data: { balance: { decrement: payout } },
}),
]
})
const tradeOps = keepHistory
? [
+24 -7
View File
@@ -38,6 +38,7 @@ export async function POST(req: NextRequest) {
shares: true,
fund: {
select: {
userId: true,
sharesOutstanding: true,
user: {
select: {
@@ -94,15 +95,31 @@ export async function POST(req: NextRequest) {
}, 0)
const totalValue = user.balance + portfolioValue + fundInvestmentValue
// Forfeit all fund investments — decrement each fund's sharesOutstanding
// Forfeit all fund investments — decrement sharesOutstanding and withdraw cash from fund
const fundUpdates = user.fundInvestments
.filter((inv) => inv.shares > 0)
.map((inv) =>
prisma.hedgeFund.update({
where: { id: inv.fundId },
data: { sharesOutstanding: { decrement: inv.shares } },
}),
)
.flatMap((inv) => {
const fundPortfolioValue = inv.fund.user.positions.reduce((psum, p) => {
const val =
p.positionType === 'LONG'
? p.shares * p.hashtag.currentPrice
: (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares
return psum + val
}, 0)
const fundTotalValue = inv.fund.user.balance + fundPortfolioValue
const nav = calcFundNav(fundTotalValue, inv.fund.sharesOutstanding)
const payout = inv.shares * nav
return [
prisma.hedgeFund.update({
where: { id: inv.fundId },
data: { sharesOutstanding: { decrement: inv.shares } },
}),
prisma.user.update({
where: { id: inv.fund.userId },
data: { balance: { decrement: payout } },
}),
]
})
const tradeOps = keepHistory
? [