Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 898f99049d | |||
| 7367e4d7c6 |
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -136,7 +136,7 @@ export default function InvestPanel({ fundSlug, nav, userBalance, userShares, us
|
||||
</div>
|
||||
{amountNum >= 1 && (
|
||||
<p className="text-xs text-slate-400">
|
||||
You'll receive ≈ <span className="text-white font-medium">{previewShares.toFixed(4)} shares</span>
|
||||
You'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>
|
||||
|
||||
Reference in New Issue
Block a user