fix: enhance fund investment value calculation and adjust portfolio total value logic
This commit is contained in:
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
|||||||
import { getServerSession } from 'next-auth'
|
import { getServerSession } from 'next-auth'
|
||||||
import { authOptions } from '@/lib/auth'
|
import { authOptions } from '@/lib/auth'
|
||||||
import { prisma } from '@/lib/prisma'
|
import { prisma } from '@/lib/prisma'
|
||||||
|
import { calcFundNav } from '@/lib/pricing'
|
||||||
|
|
||||||
const STARTING_BALANCE = 2000
|
const STARTING_BALANCE = 2000
|
||||||
|
|
||||||
@@ -28,7 +29,32 @@ export async function POST(
|
|||||||
select: {
|
select: {
|
||||||
balance: true,
|
balance: true,
|
||||||
isFund: 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: {
|
positions: {
|
||||||
where: { shares: { gt: 0 } },
|
where: { shares: { gt: 0 } },
|
||||||
select: {
|
select: {
|
||||||
@@ -49,10 +75,22 @@ export async function POST(
|
|||||||
const val =
|
const val =
|
||||||
p.positionType === 'LONG'
|
p.positionType === 'LONG'
|
||||||
? p.shares * p.hashtag.currentPrice
|
? 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
|
return sum + val
|
||||||
}, 0)
|
}, 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
|
// Forfeit all fund investments — decrement each fund's sharesOutstanding
|
||||||
const fundUpdates = user.fundInvestments
|
const fundUpdates = user.fundInvestments
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
|||||||
import { getServerSession } from 'next-auth'
|
import { getServerSession } from 'next-auth'
|
||||||
import { authOptions } from '@/lib/auth'
|
import { authOptions } from '@/lib/auth'
|
||||||
import { prisma } from '@/lib/prisma'
|
import { prisma } from '@/lib/prisma'
|
||||||
|
import { calcFundNav } from '@/lib/pricing'
|
||||||
|
|
||||||
const STARTING_BALANCE = 2000
|
const STARTING_BALANCE = 2000
|
||||||
|
|
||||||
@@ -30,7 +31,32 @@ export async function POST(req: NextRequest) {
|
|||||||
select: {
|
select: {
|
||||||
balance: true,
|
balance: true,
|
||||||
isFund: 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: {
|
positions: {
|
||||||
where: { shares: { gt: 0 } },
|
where: { shares: { gt: 0 } },
|
||||||
select: {
|
select: {
|
||||||
@@ -51,10 +77,22 @@ export async function POST(req: NextRequest) {
|
|||||||
const val =
|
const val =
|
||||||
p.positionType === 'LONG'
|
p.positionType === 'LONG'
|
||||||
? p.shares * p.hashtag.currentPrice
|
? 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
|
return sum + val
|
||||||
}, 0)
|
}, 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
|
// Forfeit all fund investments — decrement each fund's sharesOutstanding
|
||||||
const fundUpdates = user.fundInvestments
|
const fundUpdates = user.fundInvestments
|
||||||
|
|||||||
Reference in New Issue
Block a user