This commit is contained in:
Adrian Hopek
2022-04-22 12:41:00 +02:00
parent 7d12a1a153
commit fad4290cc3
33 changed files with 599 additions and 78 deletions

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Toby\Domain\Slack\Handlers;
use Illuminate\Support\Carbon;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Spatie\SlashCommand\Handlers\SignatureHandler;
use Toby\Domain\Actions\VacationRequest\CreateAction;
use Toby\Domain\Enums\VacationType;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\YearPeriod;
class HomeOffice extends SignatureHandler
{
protected $signature = "toby zdalnie {kiedy?}";
protected $description = "Pracuj zdalnie wybranego dnia (domyślnie dzisiaj)";
public function handle(Request $request): Response
{
$date = $this->getDateFromArgument($this->getArgument('kiedy') ?? "dzisiaj");
$user = $this->findUserBySlackId($request->userId);
$yearPeriod = YearPeriod::findByYear($date->year);
app(CreateAction::class)->execute([
"user_id" => $user->id,
"type" => VacationType::HomeOffice,
"from" => $date,
"to" => $date,
"year_period_id" => $yearPeriod->id,
"flow_skipped" => false,
], $user);
return $this->respondToSlack("Praca zdalna dnia {$date->toDisplayString()} została utworzona pomyślnie.")
->displayResponseToEveryoneOnChannel();
}
protected function getDateFromArgument(string $argument): Carbon
{
return match ($argument) {
"dzisiaj" => Carbon::today(),
"jutro" => Carbon::tomorrow(),
default => Carbon::create($argument),
};
}
protected function findUserBySlackId(string $slackId): User
{
/** @var User $user */
$user = User::query()
->whereRelation("profile", "slack_id", $slackId)
->first();
return $user;
}
}