Refactor: Optimize category management in CreateDefaultCategories and ResetUserCategories

This commit message follows the specified format and provides a concise description of the changes made in the code diff. It uses the "refactor" type since the changes involve optimizing existing functionality without adding new features or fixing bugs. The message is under 72 characters as required.
This commit is contained in:
Víctor Falcón 2025-12-01 15:37:53 +01:00
parent 0b9075f401
commit 050963d3f6
3 changed files with 9 additions and 10 deletions

View File

@ -11,14 +11,13 @@ class CreateDefaultCategories
*/
public function handle(User $user): void
{
if ($user->categories()->exists()) {
return;
}
$defaultCategories = self::getDefaultCategories();
foreach ($defaultCategories as $category) {
$user->categories()->create($category);
$user->categories()->firstOrCreate(
['name' => $category['name']],
$category
);
}
}

View File

@ -44,7 +44,7 @@ class ResetUserCategories extends Command
$categoriesCount = $user->categories()->count();
if ($categoriesCount > 0) {
$user->categories()->delete();
$user->categories()->forceDelete();
$this->info("Deleted {$categoriesCount} existing categories.");
} else {
$this->info('No existing categories found.');

View File

@ -19,10 +19,10 @@ class PeriodComparator
if ($this->isFullMonthRange()) {
$months = $this->from->diffInMonths($this->to->copy()->addDay());
return new self(
$this->from->copy()->subMonths($months),
$this->to->copy()->subMonths($months)->endOfMonth()
);
$previousFrom = $this->from->copy()->subMonths($months);
$previousTo = $previousFrom->copy()->endOfMonth();
return new self($previousFrom, $previousTo);
}
return new self(