Various API updates.

This commit is contained in:
James Cole
2019-06-04 20:42:11 +02:00
parent 6a6d67f2b4
commit 9b7835c9ed
11 changed files with 527 additions and 265 deletions

View File

@@ -489,16 +489,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $return;
}
/**
* Returns all available budget objects.
*
* @return Collection
*/
public function getAvailableBudgets(): Collection
{
return $this->user->availableBudgets()->get();
}
/**
* Calculate the average amount in the budgets available in this period.
* Grouped by day.
@@ -955,48 +945,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $budget;
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleTriggers(string $oldName, string $newName): void
{
$types = ['budget_is',];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
}
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @param AvailableBudget $availableBudget
* @param array $data
@@ -1131,4 +1079,95 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit;
}
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
* @return Collection
*
*/
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection
{
$query = $this->user->availableBudgets();
if (null !== $start) {
$query->where('start_date', '>=', $start->format('Y-m-d H:i:s'));
}
if (null !== $end) {
$query->where('emd_date', '<=', $end->format('Y-m-d H:i:s'));
}
return $query->get();
}
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
* @return Collection
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection
{
return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get();
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection
{
return $this->getAllBudgetLimits($start, $end)->filter(
function (BudgetLimit $budgetLimit) use ($currency) {
return $budgetLimit->transaction_currency_id === $currency->id;
}
);
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleTriggers(string $oldName, string $newName): void
{
$types = ['budget_is',];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
}
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
}

View File

@@ -55,8 +55,8 @@ interface BudgetRepositoryInterface
* This method collects various info on budgets, used on the budget page and on the index.
*
* @param Collection $budgets
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -82,12 +82,12 @@ interface BudgetRepositoryInterface
public function destroyBudgetLimit(BudgetLimit $budgetLimit): void;
/**
* @param int|null $budgetId
* @param int|null $budgetId
* @param string|null $budgetName
*
* @return Budget|null
*/
public function findBudget( ?int $budgetId, ?string $budgetName): ?Budget;
public function findBudget(?int $budgetId, ?string $budgetName): ?Budget;
/**
* Find budget by name.
@@ -130,8 +130,17 @@ interface BudgetRepositoryInterface
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection;
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
@@ -148,9 +157,20 @@ interface BudgetRepositoryInterface
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
* @return Collection
*
*/
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection;
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
* @return Collection
*/
public function getAvailableBudgets(): Collection;
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
/**
* Calculate the average amount in the budgets available in this period.
@@ -175,8 +195,8 @@ interface BudgetRepositoryInterface
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -205,8 +225,8 @@ interface BudgetRepositoryInterface
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -221,9 +241,9 @@ interface BudgetRepositoryInterface
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return AvailableBudget
*/
@@ -231,7 +251,7 @@ interface BudgetRepositoryInterface
/**
* @param Budget $budget
* @param int $order
* @param int $order
*/
public function setBudgetOrder(Budget $budget, int $order): void;
@@ -245,8 +265,8 @@ interface BudgetRepositoryInterface
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
@@ -259,8 +279,8 @@ interface BudgetRepositoryInterface
*
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -268,8 +288,8 @@ interface BudgetRepositoryInterface
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
@@ -277,8 +297,8 @@ interface BudgetRepositoryInterface
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -301,7 +321,7 @@ interface BudgetRepositoryInterface
/**
* @param Budget $budget
* @param array $data
* @param array $data
*
* @return Budget
*/
@@ -309,7 +329,7 @@ interface BudgetRepositoryInterface
/**
* @param AvailableBudget $availableBudget
* @param array $data
* @param array $data
*
* @return AvailableBudget
*/
@@ -317,7 +337,7 @@ interface BudgetRepositoryInterface
/**
* @param BudgetLimit $budgetLimit
* @param array $data
* @param array $data
*
* @return BudgetLimit
*/