fix total portfolio calculation
This commit is contained in:
parent
bcac2584ed
commit
848eda70e4
1 changed files with 19 additions and 11 deletions
|
|
@ -53,16 +53,26 @@ export async function GET({ params }) {
|
||||||
.limit(10);
|
.limit(10);
|
||||||
|
|
||||||
// get portfolio value and holdings count
|
// get portfolio value and holdings count
|
||||||
const portfolioStats = await db
|
const portfolioHoldings = await db
|
||||||
.select({
|
.select({
|
||||||
holdingsCount: count(),
|
quantity: userPortfolio.quantity,
|
||||||
totalValue: sql<number>`COALESCE(SUM(CAST(${userPortfolio.quantity} AS NUMERIC) * CAST(${coin.currentPrice} AS NUMERIC)), 0)`
|
currentPrice: coin.currentPrice
|
||||||
})
|
})
|
||||||
.from(userPortfolio)
|
.from(userPortfolio)
|
||||||
.innerJoin(coin, eq(userPortfolio.coinId, coin.id))
|
.innerJoin(coin, eq(userPortfolio.coinId, coin.id))
|
||||||
.where(eq(userPortfolio.userId, actualUserId));
|
.where(eq(userPortfolio.userId, actualUserId));
|
||||||
|
|
||||||
// get recent transactions
|
const holdingsValue = portfolioHoldings.reduce((total, holding) => {
|
||||||
|
const quantity = Number(holding.quantity);
|
||||||
|
const price = Number(holding.currentPrice);
|
||||||
|
return total + (quantity * price);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const portfolioStats = {
|
||||||
|
holdingsCount: portfolioHoldings.length,
|
||||||
|
totalValue: holdingsValue
|
||||||
|
};
|
||||||
|
|
||||||
const recentTransactions = await db
|
const recentTransactions = await db
|
||||||
.select({
|
.select({
|
||||||
id: transaction.id,
|
id: transaction.id,
|
||||||
|
|
@ -81,10 +91,9 @@ export async function GET({ params }) {
|
||||||
.orderBy(desc(transaction.timestamp))
|
.orderBy(desc(transaction.timestamp))
|
||||||
.limit(10);
|
.limit(10);
|
||||||
|
|
||||||
// calc total portfolio value
|
|
||||||
const baseCurrencyBalance = parseFloat(userProfile.baseCurrencyBalance);
|
const baseCurrencyBalance = parseFloat(userProfile.baseCurrencyBalance);
|
||||||
const holdingsValue = portfolioStats[0]?.totalValue || 0;
|
const calculatedHoldingsValue = portfolioStats.totalValue || 0;
|
||||||
const totalPortfolioValue = baseCurrencyBalance + holdingsValue;
|
const totalPortfolioValue = baseCurrencyBalance + calculatedHoldingsValue;
|
||||||
|
|
||||||
// get all transaction statistics
|
// get all transaction statistics
|
||||||
const transactionStats = await db
|
const transactionStats = await db
|
||||||
|
|
@ -116,12 +125,11 @@ export async function GET({ params }) {
|
||||||
...userProfile,
|
...userProfile,
|
||||||
baseCurrencyBalance,
|
baseCurrencyBalance,
|
||||||
totalPortfolioValue,
|
totalPortfolioValue,
|
||||||
},
|
}, stats: {
|
||||||
stats: {
|
|
||||||
totalPortfolioValue,
|
totalPortfolioValue,
|
||||||
baseCurrencyBalance,
|
baseCurrencyBalance,
|
||||||
holdingsValue,
|
holdingsValue: calculatedHoldingsValue,
|
||||||
holdingsCount: portfolioStats[0]?.holdingsCount || 0,
|
holdingsCount: portfolioStats.holdingsCount || 0,
|
||||||
coinsCreated: createdCoins.length,
|
coinsCreated: createdCoins.length,
|
||||||
totalTransactions: transactionStats[0]?.totalTransactions || 0,
|
totalTransactions: transactionStats[0]?.totalTransactions || 0,
|
||||||
totalBuyVolume: transactionStats[0]?.totalBuyVolume || 0,
|
totalBuyVolume: transactionStats[0]?.totalBuyVolume || 0,
|
||||||
|
|
|
||||||
Reference in a new issue