This commit is contained in:
Adrian Hopek 2022-02-22 08:27:29 +01:00
parent c8616716cf
commit ef54b37691
7 changed files with 98 additions and 3 deletions

View File

@ -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
{
$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));
}
}
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Toby\Domain\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use InvalidArgumentException;
use Toby\Eloquent\Models\VacationRequest;
class VacationRequestCreatedOnEmployeeBehalf extends Notification
{
use Queueable;
public function __construct(
protected VacationRequest $vacationRequest,
) {
}
public function via(): array
{
return ["mail"];
}
/**
* @throws InvalidArgumentException
*/
public function toMail(): MailMessage
{
$url = route(
"vacation.requests.show",
[
"vacationRequest" => $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);
}
}

View File

@ -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"],
];
}

View File

@ -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()),

View File

@ -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"
}

View File

@ -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(),

View File

@ -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();