From c5076e330c35d0cf8f470da9a80329539e5c1d52 Mon Sep 17 00:00:00 2001 From: Mike Johnston Date: Fri, 20 Mar 2026 15:28:59 -0400 Subject: [PATCH] fix: update fund investment logic to decrement sharesOutstanding and withdraw cash from fund --- .../api/admin/users/[userId]/reset/route.ts | 31 ++++++++++++++----- src/app/api/user/me/reset/route.ts | 31 ++++++++++++++----- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/app/api/admin/users/[userId]/reset/route.ts b/src/app/api/admin/users/[userId]/reset/route.ts index 792cbc7..9a49b13 100644 --- a/src/app/api/admin/users/[userId]/reset/route.ts +++ b/src/app/api/admin/users/[userId]/reset/route.ts @@ -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 ? [ diff --git a/src/app/api/user/me/reset/route.ts b/src/app/api/user/me/reset/route.ts index 9d6ad00..b77e9d2 100644 --- a/src/app/api/user/me/reset/route.ts +++ b/src/app/api/user/me/reset/route.ts @@ -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 ? [