improve import / export flow and clean up relationships
This commit is contained in:
hackerESQ
2024-08-28 22:06:47 -05:00
parent e684c1f4a3
commit 69c43dc41f
15 changed files with 104 additions and 74 deletions
+9
View File
@@ -37,6 +37,15 @@ class DailyChange extends Model
{
return $query->where('portfolio_id', $portfolio);
}
public function scopeMyDailyChanges()
{
return $this->whereHas('portfolio', function ($query) {
$query->whereHas('users', function ($query) {
$query->where('id', auth()->id());
});
});
}
public function portfolio()
{
+3 -2
View File
@@ -56,7 +56,8 @@ class Holding extends Model
public function transactions()
{
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
->where('transactions.portfolio_id', $this->portfolio_id);
->where('portfolio_id', $this->portfolio_id)
->withAggregate('portfolio', 'title');
}
/**
@@ -141,7 +142,7 @@ class Holding extends Model
public function scopeSymbol($query, $symbol)
{
return $query->where('symbol', $symbol);
return $query->where('holdings.symbol', $symbol);
}
public function scopeWithoutWishlists($query) {
+7
View File
@@ -62,6 +62,13 @@ class Portfolio extends Model
return $this->hasMany(DailyChange::class);
}
public function scopeMyPortfolios()
{
return $this->whereHas('users', function ($query) {
$query->where('user_id', auth()->user()->id);
});
}
public function scopeWithoutWishlists()
{
return $this->where(['wishlist' => false]);
+9 -1
View File
@@ -90,7 +90,6 @@ class Transaction extends Model
public function scopeWithMarketData($query)
{
$query->withAggregate('market_data', 'name')
->withAggregate('portfolio', 'title')
->withAggregate('market_data', 'market_value')
->withAggregate('market_data', 'fifty_two_week_low')
->withAggregate('market_data', 'fifty_two_week_high')
@@ -108,6 +107,15 @@ class Transaction extends Model
return $query->where('symbol', $symbol);
}
public function scopeMyTransactions()
{
return $this->whereHas('portfolio', function ($query) {
$query->whereHas('users', function ($query) {
$query->where('id', auth()->id());
});
});
}
public function refreshMarketData()
{
return MarketData::getMarketData($this->attributes['symbol']);
+1
View File
@@ -72,6 +72,7 @@ class User extends Authenticatable
{
return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class])
->withMarketData()
->withAggregate('portfolio', 'title')
->selectRaw('
CASE
WHEN transaction_type = \'SELL\'