Skip to content

Commit 0f96068

Browse files
authored
#494 - remove year scope (#495)
* wip * wip * wip * wip * fix * fix * fix * cs fix
1 parent f17c2cd commit 0f96068

File tree

120 files changed

+1153
-1989
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1153
-1989
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ google-credentials.json
2323
.composer
2424
.deployment
2525
/.phpunit.cache
26+
.php-cs-fixer.cache

app/Actions/CreateUserAction.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Toby\Actions;
66

77
use Toby\Models\User;
8-
use Toby\Models\YearPeriod;
98

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

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

20-
$this->createVacationLimitsFor($user);
21-
2219
return $user;
2320
}
24-
25-
protected function createVacationLimitsFor(User $user): void
26-
{
27-
$yearPeriods = YearPeriod::all();
28-
29-
foreach ($yearPeriods as $yearPeriod) {
30-
$user->vacationLimits()->create([
31-
"year_period_id" => $yearPeriod->id,
32-
]);
33-
}
34-
}
3521
}

app/Actions/CreateYearPeriodAction.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

app/Actions/VacationRequest/CreateAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ protected function createVacationRequest(array $data, User $creator): VacationRe
6363
$vacationRequest->vacations()->create([
6464
"date" => $day,
6565
"user_id" => $vacationRequest->user->id,
66-
"year_period_id" => $vacationRequest->yearPeriod->id,
6766
]);
6867
}
6968

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Toby\Console\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Carbon;
9+
use Toby\Domain\PolishHolidaysRetriever;
10+
use Toby\Models\Holiday;
11+
12+
class GenerateHolidays extends Command
13+
{
14+
protected $signature = "toby:holidays:generate {year?}";
15+
protected $description = "Generate default holidays for year";
16+
17+
public function handle(PolishHolidaysRetriever $polishHolidaysRetriever): void
18+
{
19+
$year = (int)$this->argument("year") ?? Carbon::now()->year;
20+
21+
$holidays = $polishHolidaysRetriever->getForYear($year);
22+
23+
foreach ($holidays as $holiday) {
24+
Holiday::query()
25+
->updateOrCreate([
26+
"name" => $holiday["name"],
27+
"date" => $holiday["date"],
28+
]);
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Toby\Console\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Schema;
10+
11+
class MigrateYearPeriodYearToVacationLimits extends Command
12+
{
13+
protected $signature = "toby:migrate-year-period-year-to-vacation-limits";
14+
protected $description = "Migrate year period to vacation limits";
15+
16+
public function handle(): void
17+
{
18+
if (!Schema::hasTable("year_periods")) {
19+
$this->error("Year periods don't exist");
20+
21+
return;
22+
}
23+
24+
DB::statement("
25+
UPDATE vacation_limits
26+
SET year = year_periods.year
27+
FROM year_periods
28+
WHERE year_periods.id = vacation_limits.year_period_id
29+
");
30+
}
31+
}

app/Console/Commands/RebuildDocumentNumberingSystem.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Toby\Console\Commands;
66

77
use Illuminate\Console\Command;
8-
use Toby\Models\YearPeriod;
8+
use Illuminate\Support\Facades\DB;
9+
use Toby\Models\VacationRequest;
910

1011
class RebuildDocumentNumberingSystem extends Command
1112
{
@@ -14,18 +15,20 @@ class RebuildDocumentNumberingSystem extends Command
1415

1516
public function handle(): void
1617
{
17-
$yearPeriods = YearPeriod::all();
18+
$years = DB::table(VacationRequest::class)
19+
->select([DB::raw("YEAR(from) as year")])
20+
->groupBy("year")
21+
->value("year");
1822

19-
foreach ($yearPeriods as $yearPeriod) {
23+
foreach ($years as $year) {
2024
$number = 1;
2125

22-
$vacationRequests = $yearPeriod
23-
->vacationRequests()
24-
->oldest()
26+
$vacationRequests = VacationRequest::query()
27+
->whereYear("date", $year)
2528
->get();
2629

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

3033
$number++;
3134
}

app/Domain/CalendarGenerator.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@
77
use Carbon\CarbonPeriod;
88
use Illuminate\Support\Carbon;
99
use Illuminate\Support\Collection;
10-
use Toby\Helpers\YearPeriodRetriever;
1110
use Toby\Http\Resources\SimpleVacationRequestResource;
11+
use Toby\Models\Holiday;
1212
use Toby\Models\Vacation;
13-
use Toby\Models\YearPeriod;
1413

1514
class CalendarGenerator
1615
{
17-
public function __construct(
18-
protected YearPeriodRetriever $yearPeriodRetriever,
19-
) {}
20-
2116
public function generate(Carbon $month): array
2217
{
2318
$period = CarbonPeriod::create($month->copy()->startOfMonth(), $month->copy()->endOfMonth());
24-
$yearPeriod = YearPeriod::findByYear($month->year);
2519

26-
$holidays = $yearPeriod->holidays()->pluck("date");
20+
$holidays = Holiday::query()
21+
->whereYear("date", $month->year)
22+
->pluck("date");
2723

2824
return $this->generateCalendar($period, $holidays);
2925
}
@@ -63,14 +59,4 @@ protected function getVacationsForPeriod(CarbonPeriod $period): Collection
6359
->get()
6460
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
6561
}
66-
67-
protected function getPendingVacationsForPeriod(CarbonPeriod $period): Collection
68-
{
69-
return Vacation::query()
70-
->whereBetween("date", [$period->start, $period->end])
71-
->pending()
72-
->with("vacationRequest")
73-
->get()
74-
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
75-
}
7662
}

app/Domain/DashboardAggregator.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Toby\Models\Holiday;
1515
use Toby\Models\User;
1616
use Toby\Models\Vacation;
17-
use Toby\Models\YearPeriod;
17+
use Toby\Models\VacationRequest;
1818

1919
class DashboardAggregator
2020
{
@@ -24,14 +24,16 @@ public function __construct(
2424
protected UserBenefitsRetriever $benefitsRetriever,
2525
) {}
2626

27-
public function aggregateStats(User $user, YearPeriod $yearPeriod): array
27+
public function aggregateStats(User $user, ?int $year = null): array
2828
{
29-
$limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
30-
$hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $yearPeriod);
31-
$used = $this->vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
32-
$pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
33-
$remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $yearPeriod);
34-
$other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
29+
$year ??= Carbon::now()->year;
30+
31+
$limit = $this->vacationStatsRetriever->getVacationDaysLimit($user, $year);
32+
$hasLimit = $this->vacationStatsRetriever->hasVacationDaysLimit($user, $year);
33+
$used = $this->vacationStatsRetriever->getUsedVacationDays($user, $year);
34+
$pending = $this->vacationStatsRetriever->getPendingVacationDays($user, $year);
35+
$remoteWork = $this->vacationStatsRetriever->getRemoteWorkDays($user, $year);
36+
$other = $this->vacationStatsRetriever->getOtherApprovedVacationDays($user, $year);
3537
$remaining = $limit - $used - $pending;
3638

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

48-
public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
50+
public function aggregateCalendarData(User $user, ?int $year = null): array
4951
{
5052
$approvedVacations = $user
5153
->vacations()
5254
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
53-
->whereBelongsTo($yearPeriod)
55+
->whereYear("date", $year ?? Carbon::now()->year)
5456
->cache("vacations:{$user->id}")
5557
->approved()
5658
->get()
@@ -63,7 +65,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
6365
$pendingVacations = $user
6466
->vacations()
6567
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
66-
->whereBelongsTo($yearPeriod)
68+
->whereYear("date", $year ?? Carbon::now()->year)
6769
->cache("vacations:{$user->id}")
6870
->pending()
6971
->get()
@@ -73,8 +75,9 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
7375
],
7476
);
7577

76-
$holidays = $yearPeriod
77-
->holidays
78+
$holidays = Holiday::query()
79+
->whereYear("date", $year)
80+
->get()
7881
->mapWithKeys(fn(Holiday $holiday): array => [$holiday->date->toDateString() => $holiday->name]);
7982

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

87-
public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): JsonResource
90+
public function aggregateVacationRequests(User $user, ?int $year = null): JsonResource
8891
{
89-
if ($user->can("listAllRequests")) {
90-
$vacationRequests = $yearPeriod->vacationRequests()
91-
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
92+
$year ??= Carbon::now()->year;
93+
94+
$query = $user->can("listAllRequests")
95+
? VacationRequest::query()
9296
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
93-
->latest("updated_at")
94-
->limit(3)
95-
->get();
96-
} else {
97-
$vacationRequests = $user->vacationRequests()
98-
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
99-
->whereBelongsTo($yearPeriod)
100-
->latest("updated_at")
101-
->limit(3)
102-
->get();
103-
}
97+
: $user->vacationRequests();
98+
99+
$vacationRequests = $query
100+
->with(["user", "vacations", "vacations.user", "vacations.user.profile", "user.permissions", "user.profile"])
101+
->whereYear("from", $year)
102+
->latest("updated_at")
103+
->limit(3)
104+
->get();
104105

105106
return VacationRequestResource::collection($vacationRequests);
106107
}

app/Domain/PolishHolidaysRetriever.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Illuminate\Support\Carbon;
88
use Illuminate\Support\Collection;
9-
use Toby\Models\YearPeriod;
109
use Yasumi\Holiday;
1110
use Yasumi\Yasumi;
1211

@@ -15,9 +14,9 @@ class PolishHolidaysRetriever
1514
protected const string PROVIDER_KEY = "Poland";
1615
protected const string LANG_KEY = "pl";
1716

18-
public function getForYearPeriod(YearPeriod $yearPeriod): Collection
17+
public function getForYear(int $year): Collection
1918
{
20-
$polishProvider = Yasumi::create(static::PROVIDER_KEY, $yearPeriod->year);
19+
$polishProvider = Yasumi::create(static::PROVIDER_KEY, $year);
2120

2221
$holidays = $polishProvider->getHolidays();
2322

0 commit comments

Comments
 (0)