Skip to content

Commit

Permalink
#494 - remove year scope (#495)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* fix

* fix

* fix

* cs fix
  • Loading branch information
Baakoma authored Oct 22, 2024
1 parent f17c2cd commit 0f96068
Show file tree
Hide file tree
Showing 120 changed files with 1,153 additions and 1,989 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ google-credentials.json
.composer
.deployment
/.phpunit.cache
.php-cs-fixer.cache
14 changes: 0 additions & 14 deletions app/Actions/CreateUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Toby\Actions;

use Toby\Models\User;
use Toby\Models\YearPeriod;

class CreateUserAction
{
Expand All @@ -17,19 +16,6 @@ public function execute(array $userData, array $profileData): User

$user->profile()->create($profileData);

$this->createVacationLimitsFor($user);

return $user;
}

protected function createVacationLimitsFor(User $user): void
{
$yearPeriods = YearPeriod::all();

foreach ($yearPeriods as $yearPeriod) {
$user->vacationLimits()->create([
"year_period_id" => $yearPeriod->id,
]);
}
}
}
53 changes: 0 additions & 53 deletions app/Actions/CreateYearPeriodAction.php

This file was deleted.

1 change: 0 additions & 1 deletion app/Actions/VacationRequest/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ protected function createVacationRequest(array $data, User $creator): VacationRe
$vacationRequest->vacations()->create([
"date" => $day,
"user_id" => $vacationRequest->user->id,
"year_period_id" => $vacationRequest->yearPeriod->id,
]);
}

Expand Down
31 changes: 31 additions & 0 deletions app/Console/Commands/GenerateHolidays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Toby\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Toby\Domain\PolishHolidaysRetriever;
use Toby\Models\Holiday;

class GenerateHolidays extends Command
{
protected $signature = "toby:holidays:generate {year?}";
protected $description = "Generate default holidays for year";

public function handle(PolishHolidaysRetriever $polishHolidaysRetriever): void
{
$year = (int)$this->argument("year") ?? Carbon::now()->year;

$holidays = $polishHolidaysRetriever->getForYear($year);

foreach ($holidays as $holiday) {
Holiday::query()
->updateOrCreate([
"name" => $holiday["name"],
"date" => $holiday["date"],
]);
}
}
}
31 changes: 31 additions & 0 deletions app/Console/Commands/MigrateYearPeriodYearToVacationLimits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Toby\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class MigrateYearPeriodYearToVacationLimits extends Command
{
protected $signature = "toby:migrate-year-period-year-to-vacation-limits";
protected $description = "Migrate year period to vacation limits";

public function handle(): void
{
if (!Schema::hasTable("year_periods")) {
$this->error("Year periods don't exist");

return;
}

DB::statement("
UPDATE vacation_limits
SET year = year_periods.year
FROM year_periods
WHERE year_periods.id = vacation_limits.year_period_id
");
}
}
17 changes: 10 additions & 7 deletions app/Console/Commands/RebuildDocumentNumberingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Toby\Console\Commands;

use Illuminate\Console\Command;
use Toby\Models\YearPeriod;
use Illuminate\Support\Facades\DB;
use Toby\Models\VacationRequest;

class RebuildDocumentNumberingSystem extends Command
{
Expand All @@ -14,18 +15,20 @@ class RebuildDocumentNumberingSystem extends Command

public function handle(): void
{
$yearPeriods = YearPeriod::all();
$years = DB::table(VacationRequest::class)
->select([DB::raw("YEAR(from) as year")])
->groupBy("year")
->value("year");

foreach ($yearPeriods as $yearPeriod) {
foreach ($years as $year) {
$number = 1;

$vacationRequests = $yearPeriod
->vacationRequests()
->oldest()
$vacationRequests = VacationRequest::query()
->whereYear("date", $year)
->get();

foreach ($vacationRequests as $vacationRequest) {
$vacationRequest->update(["name" => "{$number}/{$yearPeriod->year}"]);
$vacationRequest->update(["name" => "{$number}/{$year}"]);

$number++;
}
Expand Down
22 changes: 4 additions & 18 deletions app/Domain/CalendarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
use Carbon\CarbonPeriod;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Http\Resources\SimpleVacationRequestResource;
use Toby\Models\Holiday;
use Toby\Models\Vacation;
use Toby\Models\YearPeriod;

class CalendarGenerator
{
public function __construct(
protected YearPeriodRetriever $yearPeriodRetriever,
) {}

public function generate(Carbon $month): array
{
$period = CarbonPeriod::create($month->copy()->startOfMonth(), $month->copy()->endOfMonth());
$yearPeriod = YearPeriod::findByYear($month->year);

$holidays = $yearPeriod->holidays()->pluck("date");
$holidays = Holiday::query()
->whereYear("date", $month->year)
->pluck("date");

return $this->generateCalendar($period, $holidays);
}
Expand Down Expand Up @@ -63,14 +59,4 @@ protected function getVacationsForPeriod(CarbonPeriod $period): Collection
->get()
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
}

protected function getPendingVacationsForPeriod(CarbonPeriod $period): Collection
{
return Vacation::query()
->whereBetween("date", [$period->start, $period->end])
->pending()
->with("vacationRequest")
->get()
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
}
}
57 changes: 29 additions & 28 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Toby\Models\Holiday;
use Toby\Models\User;
use Toby\Models\Vacation;
use Toby\Models\YearPeriod;
use Toby\Models\VacationRequest;

class DashboardAggregator
{
Expand All @@ -24,14 +24,16 @@ public function __construct(
protected UserBenefitsRetriever $benefitsRetriever,
) {}

public function aggregateStats(User $user, YearPeriod $yearPeriod): array
public function aggregateStats(User $user, ?int $year = null): array
{
$limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
$hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $yearPeriod);
$used = $this->vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
$pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
$remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $yearPeriod);
$other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
$year ??= Carbon::now()->year;

$limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $year);
$hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $year);
$used = $this->vacationStatsRetriever->getUsedVacationDays($user, $year);
$pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $year);
$remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $year);
$other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $year);
$remaining = $limit - $used - $pending;

return [
Expand All @@ -45,12 +47,12 @@ public function aggregateStats(User $user, YearPeriod $yearPeriod): array
];
}

public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
public function aggregateCalendarData(User $user, ?int $year = null): array
{
$approvedVacations = $user
->vacations()
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->whereYear("date", $year ?? Carbon::now()->year)
->cache("vacations:{$user->id}")
->approved()
->get()
Expand All @@ -63,7 +65,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
$pendingVacations = $user
->vacations()
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->whereYear("date", $year ?? Carbon::now()->year)
->cache("vacations:{$user->id}")
->pending()
->get()
Expand All @@ -73,8 +75,9 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
],
);

$holidays = $yearPeriod
->holidays
$holidays = Holiday::query()
->whereYear("date", $year)
->get()
->mapWithKeys(fn(Holiday $holiday): array => [$holiday->date->toDateString() => $holiday->name]);

return [
Expand All @@ -84,23 +87,21 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
];
}

public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): JsonResource
public function aggregateVacationRequests(User $user, ?int $year = null): JsonResource
{
if ($user->can("listAllRequests")) {
$vacationRequests = $yearPeriod->vacationRequests()
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
$year ??= Carbon::now()->year;

$query = $user->can("listAllRequests")
? VacationRequest::query()
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->latest("updated_at")
->limit(3)
->get();
} else {
$vacationRequests = $user->vacationRequests()
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->latest("updated_at")
->limit(3)
->get();
}
: $user->vacationRequests();

$vacationRequests = $query
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereYear("from", $year)
->latest("updated_at")
->limit(3)
->get();

return VacationRequestResource::collection($vacationRequests);
}
Expand Down
5 changes: 2 additions & 3 deletions app/Domain/PolishHolidaysRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Models\YearPeriod;
use Yasumi\Holiday;
use Yasumi\Yasumi;

Expand All @@ -15,9 +14,9 @@ class PolishHolidaysRetriever
protected const string PROVIDER_KEY = "Poland";
protected const string LANG_KEY = "pl";

public function getForYearPeriod(YearPeriod $yearPeriod): Collection
public function getForYear(int $year): Collection
{
$polishProvider = Yasumi::create(static::PROVIDER_KEY, $yearPeriod->year);
$polishProvider = Yasumi::create(static::PROVIDER_KEY, $year);

$holidays = $polishProvider->getHolidays();

Expand Down
Loading

0 comments on commit 0f96068

Please sign in to comment.