diff --git a/app/Domain/Listeners/SendCreatedVacationRequestNotification.php b/app/Domain/Listeners/SendCreatedVacationRequestNotification.php index 5aece51..bd31e9b 100644 --- a/app/Domain/Listeners/SendCreatedVacationRequestNotification.php +++ b/app/Domain/Listeners/SendCreatedVacationRequestNotification.php @@ -6,6 +6,7 @@ namespace Toby\Domain\Listeners; use Toby\Domain\Events\VacationRequestCreated; use Toby\Domain\Notifications\VacationRequestCreatedNotification; +use Toby\Domain\Notifications\VacationRequestCreatedOnEmployeeBehalf; class SendCreatedVacationRequestNotification { @@ -15,6 +16,12 @@ class SendCreatedVacationRequestNotification public function handle(VacationRequestCreated $event): void { - $event->vacationRequest->user->notify(new VacationRequestCreatedNotification($event->vacationRequest)); + $vacationRequest = $event->vacationRequest; + + if ($vacationRequest->creator->is($vacationRequest->user)) { + $event->vacationRequest->user->notify(new VacationRequestCreatedNotification($event->vacationRequest)); + } else { + $event->vacationRequest->user->notify(new VacationRequestCreatedOnEmployeeBehalf($event->vacationRequest)); + } } } diff --git a/app/Domain/Notifications/VacationRequestCreatedOnEmployeeBehalf.php b/app/Domain/Notifications/VacationRequestCreatedOnEmployeeBehalf.php new file mode 100644 index 0000000..7d255ff --- /dev/null +++ b/app/Domain/Notifications/VacationRequestCreatedOnEmployeeBehalf.php @@ -0,0 +1,74 @@ + $this->vacationRequest, + ], + ); + return $this->buildMailMessage($url); + } + + protected function buildMailMessage(string $url): MailMessage + { + $creator = $this->vacationRequest->creator->fullName; + $user = $this->vacationRequest->user->first_name; + $title = $this->vacationRequest->name; + $type = $this->vacationRequest->type->label(); + $from = $this->vacationRequest->from->toDisplayDate(); + $to = $this->vacationRequest->to->toDisplayDate(); + $days = $this->vacationRequest->vacations()->count(); + $appName = config("app.name"); + + return (new MailMessage()) + ->greeting(__("Hi :user!", [ + "user" => $user, + ])) + ->subject(__("Vacation request :title has been created on your behalf.", [ + "title" => $title, + ])) + ->line(__("The vacation request :title has been created correctly by :creator on your behalf in the :appName.", [ + "title" => $title, + "appName" => $appName, + "creator" => $creator, + ])) + ->line(__("Vacation type: :type", [ + "type" => $type, + ])) + ->line(__("From :from to :to (number of days: :days)", [ + "from" => $from, + "to" => $to, + "days" => $days, + ])) + ->action(__("Click here for details"), $url); + } +} diff --git a/app/Infrastructure/Http/Requests/VacationRequestRequest.php b/app/Infrastructure/Http/Requests/VacationRequestRequest.php index 28d03de..b3d0c20 100644 --- a/app/Infrastructure/Http/Requests/VacationRequestRequest.php +++ b/app/Infrastructure/Http/Requests/VacationRequestRequest.php @@ -20,7 +20,7 @@ class VacationRequestRequest extends FormRequest "type" => ["required", new Enum(VacationType::class)], "from" => ["required", "date_format:Y-m-d", new YearPeriodExists()], "to" => ["required", "date_format:Y-m-d", new YearPeriodExists()], - "skipFlow" => ["required", "boolean"], + "skipFlow" => ["nullable", "boolean"], "comment" => ["nullable"], ]; } diff --git a/database/factories/VacationRequestFactory.php b/database/factories/VacationRequestFactory.php index 4816781..2c5e65f 100644 --- a/database/factories/VacationRequestFactory.php +++ b/database/factories/VacationRequestFactory.php @@ -24,6 +24,7 @@ class VacationRequestFactory extends Factory return [ "user_id" => User::factory(), + "creator_id" => fn(array $attributes) => $attributes["user_id"], "year_period_id" => YearPeriod::factory(), "name" => fn(array $attributes) => $this->generateName($attributes), "type" => $this->faker->randomElement(VacationType::cases()), diff --git a/resources/lang/pl.json b/resources/lang/pl.json index 4dbab48..9ef462f 100644 --- a/resources/lang/pl.json +++ b/resources/lang/pl.json @@ -70,5 +70,7 @@ "Vacation request :title has been cancelled": "Wniosek urlopowy :title został anulowany", "The vacation request :title for user :requester has been cancelled.": "Wniosek urlopowy :title od użytkownika :requester został anulowany.", "Vacation request :title has been rejected": "Wniosek urlopowy :title został odrzucony", - "The vacation request :title for user :requester has been rejected.": "Wniosek urlopowy :title od użytkownika :requester został odrzucony." + "The vacation request :title for user :requester has been rejected.": "Wniosek urlopowy :title od użytkownika :requester został odrzucony.", + "Vacation request :title has been created on your behalf.": "Wniosek urlopowy :title został utworzony w Twoim imieniu", + "The vacation request :title has been created correctly by :creator on your behalf in the :appName.": "W systemie :appName został w Twoim imieniu poprawnie utworzony wnioskek urlopowy :title przez :creator" } diff --git a/tests/Feature/VacationRequestTest.php b/tests/Feature/VacationRequestTest.php index 95c9c79..1bc21d1 100644 --- a/tests/Feature/VacationRequestTest.php +++ b/tests/Feature/VacationRequestTest.php @@ -69,6 +69,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 11)->toDateString(), @@ -178,6 +179,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 11)->toDateString(), @@ -202,6 +204,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 5)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 6)->toDateString(), @@ -233,6 +236,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 4, 18)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 4, 18)->toDateString(), @@ -268,6 +272,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), @@ -304,6 +309,7 @@ class VacationRequestTest extends FeatureTestCase $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), @@ -320,6 +326,7 @@ class VacationRequestTest extends FeatureTestCase $currentYearPeriod = YearPeriod::current(); $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), "to" => Carbon::create($currentYearPeriod->year, 2, 6)->toDateString(), @@ -337,6 +344,7 @@ class VacationRequestTest extends FeatureTestCase $nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1); $this->actingAs($user) ->post("/vacation-requests", [ + "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 12, 27)->toDateString(), "to" => Carbon::create($nextYearPeriod->year, 1, 2)->toDateString(), diff --git a/tests/Unit/VacationRequestStatesTest.php b/tests/Unit/VacationRequestStatesTest.php index ed943d6..193d3ce 100644 --- a/tests/Unit/VacationRequestStatesTest.php +++ b/tests/Unit/VacationRequestStatesTest.php @@ -6,6 +6,7 @@ namespace Tests\Unit; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Notification; use Tests\TestCase; use Tests\Traits\InteractsWithYearPeriods; use Toby\Domain\Enums\VacationRequestState; @@ -26,6 +27,8 @@ class VacationRequestStatesTest extends TestCase { parent::setUp(); + Notification::fake(); + $this->stateManager = $this->app->make(VacationRequestStateManager::class); $this->createCurrentYearPeriod();