From 7367e4d7c6219a74a5db476db3974eef73fb1588 Mon Sep 17 00:00:00 2001 From: Mike Johnston Date: Fri, 20 Mar 2026 15:16:22 -0400 Subject: [PATCH] fix: enhance fund investment value calculation and adjust portfolio total value logic --- .../api/admin/users/[userId]/reset/route.ts | 44 +++++++++++++++++-- src/app/api/user/me/reset/route.ts | 44 +++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/app/api/admin/users/[userId]/reset/route.ts b/src/app/api/admin/users/[userId]/reset/route.ts index 84a3709..792cbc7 100644 --- a/src/app/api/admin/users/[userId]/reset/route.ts +++ b/src/app/api/admin/users/[userId]/reset/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { prisma } from '@/lib/prisma' +import { calcFundNav } from '@/lib/pricing' const STARTING_BALANCE = 2000 @@ -28,7 +29,32 @@ export async function POST( select: { balance: true, isFund: true, - fundInvestments: { select: { fundId: true, shares: true } }, + fundInvestments: { + where: { shares: { gt: 0 } }, + select: { + fundId: true, + shares: true, + fund: { + select: { + sharesOutstanding: true, + user: { + select: { + balance: true, + positions: { + where: { shares: { gt: 0 } }, + select: { + shares: true, + avgBuyPrice: true, + positionType: true, + hashtag: { select: { currentPrice: true } }, + }, + }, + }, + }, + }, + }, + }, + }, positions: { where: { shares: { gt: 0 } }, select: { @@ -49,10 +75,22 @@ export async function POST( const val = p.positionType === 'LONG' ? p.shares * p.hashtag.currentPrice - : p.avgBuyPrice * p.shares - (p.hashtag.currentPrice - p.avgBuyPrice) * p.shares + : (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares return sum + val }, 0) - const totalValue = user.balance + portfolioValue + const fundInvestmentValue = user.fundInvestments.reduce((sum, 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) + return sum + inv.shares * nav + }, 0) + const totalValue = user.balance + portfolioValue + fundInvestmentValue // Forfeit all fund investments — decrement each fund's sharesOutstanding const fundUpdates = user.fundInvestments diff --git a/src/app/api/user/me/reset/route.ts b/src/app/api/user/me/reset/route.ts index c1a4214..9d6ad00 100644 --- a/src/app/api/user/me/reset/route.ts +++ b/src/app/api/user/me/reset/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { prisma } from '@/lib/prisma' +import { calcFundNav } from '@/lib/pricing' const STARTING_BALANCE = 2000 @@ -30,7 +31,32 @@ export async function POST(req: NextRequest) { select: { balance: true, isFund: true, - fundInvestments: { select: { fundId: true, shares: true } }, + fundInvestments: { + where: { shares: { gt: 0 } }, + select: { + fundId: true, + shares: true, + fund: { + select: { + sharesOutstanding: true, + user: { + select: { + balance: true, + positions: { + where: { shares: { gt: 0 } }, + select: { + shares: true, + avgBuyPrice: true, + positionType: true, + hashtag: { select: { currentPrice: true } }, + }, + }, + }, + }, + }, + }, + }, + }, positions: { where: { shares: { gt: 0 } }, select: { @@ -51,10 +77,22 @@ export async function POST(req: NextRequest) { const val = p.positionType === 'LONG' ? p.shares * p.hashtag.currentPrice - : p.avgBuyPrice * p.shares - (p.hashtag.currentPrice - p.avgBuyPrice) * p.shares + : (2 * p.avgBuyPrice - p.hashtag.currentPrice) * p.shares return sum + val }, 0) - const totalValue = user.balance + portfolioValue + const fundInvestmentValue = user.fundInvestments.reduce((sum, 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) + return sum + inv.shares * nav + }, 0) + const totalValue = user.balance + portfolioValue + fundInvestmentValue // Forfeit all fund investments — decrement each fund's sharesOutstanding const fundUpdates = user.fundInvestments