fix: update fund investment logic to decrement sharesOutstanding and withdraw cash from fund
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m24s
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m24s
This commit is contained in:
@@ -36,6 +36,7 @@ export async function POST(
|
|||||||
shares: true,
|
shares: true,
|
||||||
fund: {
|
fund: {
|
||||||
select: {
|
select: {
|
||||||
|
userId: true,
|
||||||
sharesOutstanding: true,
|
sharesOutstanding: true,
|
||||||
user: {
|
user: {
|
||||||
select: {
|
select: {
|
||||||
@@ -92,15 +93,31 @@ export async function POST(
|
|||||||
}, 0)
|
}, 0)
|
||||||
const totalValue = user.balance + portfolioValue + fundInvestmentValue
|
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
|
const fundUpdates = user.fundInvestments
|
||||||
.filter((inv) => inv.shares > 0)
|
.filter((inv) => inv.shares > 0)
|
||||||
.map((inv) =>
|
.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({
|
prisma.hedgeFund.update({
|
||||||
where: { id: inv.fundId },
|
where: { id: inv.fundId },
|
||||||
data: { sharesOutstanding: { decrement: inv.shares } },
|
data: { sharesOutstanding: { decrement: inv.shares } },
|
||||||
}),
|
}),
|
||||||
)
|
prisma.user.update({
|
||||||
|
where: { id: inv.fund.userId },
|
||||||
|
data: { balance: { decrement: payout } },
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
const tradeOps = keepHistory
|
const tradeOps = keepHistory
|
||||||
? [
|
? [
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export async function POST(req: NextRequest) {
|
|||||||
shares: true,
|
shares: true,
|
||||||
fund: {
|
fund: {
|
||||||
select: {
|
select: {
|
||||||
|
userId: true,
|
||||||
sharesOutstanding: true,
|
sharesOutstanding: true,
|
||||||
user: {
|
user: {
|
||||||
select: {
|
select: {
|
||||||
@@ -94,15 +95,31 @@ export async function POST(req: NextRequest) {
|
|||||||
}, 0)
|
}, 0)
|
||||||
const totalValue = user.balance + portfolioValue + fundInvestmentValue
|
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
|
const fundUpdates = user.fundInvestments
|
||||||
.filter((inv) => inv.shares > 0)
|
.filter((inv) => inv.shares > 0)
|
||||||
.map((inv) =>
|
.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({
|
prisma.hedgeFund.update({
|
||||||
where: { id: inv.fundId },
|
where: { id: inv.fundId },
|
||||||
data: { sharesOutstanding: { decrement: inv.shares } },
|
data: { sharesOutstanding: { decrement: inv.shares } },
|
||||||
}),
|
}),
|
||||||
)
|
prisma.user.update({
|
||||||
|
where: { id: inv.fund.userId },
|
||||||
|
data: { balance: { decrement: payout } },
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
const tradeOps = keepHistory
|
const tradeOps = keepHistory
|
||||||
? [
|
? [
|
||||||
|
|||||||
Reference in New Issue
Block a user