Compare commits

..

2 Commits

Author SHA1 Message Date
ThaMunsta 898f99049d fix: increase precision for shares display and input step in InvestPanel
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m26s
2026-03-20 15:17:46 -04:00
ThaMunsta 7367e4d7c6 fix: enhance fund investment value calculation and adjust portfolio total value logic 2026-03-20 15:16:22 -04:00
3 changed files with 85 additions and 9 deletions
@@ -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
+41 -3
View File
@@ -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
+3 -3
View File
@@ -136,7 +136,7 @@ export default function InvestPanel({ fundSlug, nav, userBalance, userShares, us
</div>
{amountNum >= 1 && (
<p className="text-xs text-slate-400">
You&apos;ll receive <span className="text-white font-medium">{previewShares.toFixed(4)} shares</span>
You&apos;ll receive <span className="text-white font-medium">{previewShares.toFixed(6)} shares</span>
</p>
)}
<button
@@ -154,7 +154,7 @@ export default function InvestPanel({ fundSlug, nav, userBalance, userShares, us
<input
type="number"
min="0"
step="0.0001"
step="0.000001"
max={userShares}
value={shares}
onChange={(e) => setShares(e.target.value)}
@@ -167,7 +167,7 @@ export default function InvestPanel({ fundSlug, nav, userBalance, userShares, us
onClick={() => setShares(String(userShares))}
className="text-xs text-indigo-400 hover:text-indigo-300 mt-1"
>
Max ({userShares.toFixed(4)})
Max ({userShares.toFixed(6)})
</button>
)}
</div>