From 16d2277011a22d54b31f584dd26ee0bff58a9d51 Mon Sep 17 00:00:00 2001 From: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:23:34 +0200 Subject: [PATCH] #250 - add command for giving keys to office and taking them (#342) * #250 - wip * #250 - wip * #250 - wip * #250 - fix * #250 - fix * #250 - fix * Apply suggestions from code review Co-authored-by: Krzysztof Rewak --------- Co-authored-by: Krzysztof Rewak --- .../Slack/Handlers/DailySummary.php | 8 ++-- .../Slack/Handlers/LeaveKeysInOffice.php | 47 +++++++++++++++++++ .../Slack/Handlers/TakeKeysFromOffice.php | 47 +++++++++++++++++++ config/laravel-slack-slash-command.php | 4 ++ lang/pl.json | 8 +++- 5 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 app/Infrastructure/Slack/Handlers/LeaveKeysInOffice.php create mode 100644 app/Infrastructure/Slack/Handlers/TakeKeysFromOffice.php diff --git a/app/Infrastructure/Slack/Handlers/DailySummary.php b/app/Infrastructure/Slack/Handlers/DailySummary.php index 9d50d23d..71d5775a 100644 --- a/app/Infrastructure/Slack/Handlers/DailySummary.php +++ b/app/Infrastructure/Slack/Handlers/DailySummary.php @@ -7,7 +7,7 @@ use Illuminate\Support\Carbon; use Spatie\SlashCommand\Request; use Spatie\SlashCommand\Response; -use Toby\Domain\Actions\Slack\RetrieveDailySummaryAction; +use Toby\Domain\Actions\Slack\GenerateDailySummaryAction; class DailySummary extends SignatureHandler { @@ -16,11 +16,11 @@ class DailySummary extends SignatureHandler public function handle(Request $request): Response { - $retrieveDailySummary = app()->make(RetrieveDailySummaryAction::class); + $generateDailySummary = app()->make(GenerateDailySummaryAction::class); - $dailySummary = $retrieveDailySummary->execute(Carbon::now()); + $dailySummary = $generateDailySummary->execute(Carbon::now()); return $this->respondToSlack($dailySummary->getTitle()) - ->withAttachments($dailySummary->getAttachments()); + ->withAttachments($dailySummary->getAttachments()->flatten()->all()); } } diff --git a/app/Infrastructure/Slack/Handlers/LeaveKeysInOffice.php b/app/Infrastructure/Slack/Handlers/LeaveKeysInOffice.php new file mode 100644 index 00000000..bb64dc37 --- /dev/null +++ b/app/Infrastructure/Slack/Handlers/LeaveKeysInOffice.php @@ -0,0 +1,47 @@ +findUserBySlackIdOrFail($request->userId); + + /** @var Key|null $key */ + $key = $authUser->keys()->first(); + + if (!$key) { + throw ValidationException::withMessages(["key" => __("You don't have any key to leave in the office.")]); + } + + $key->user()->associate(null); + + $key->save(); + + $key->notify(new KeyHasBeenLeftInTheOffice($authUser)); + + return $this->respondToSlack( + __(":white_check_mark: Key no. :number has been left in the office", ["number" => $key->id]), + ); + } +} diff --git a/app/Infrastructure/Slack/Handlers/TakeKeysFromOffice.php b/app/Infrastructure/Slack/Handlers/TakeKeysFromOffice.php new file mode 100644 index 00000000..ef50450c --- /dev/null +++ b/app/Infrastructure/Slack/Handlers/TakeKeysFromOffice.php @@ -0,0 +1,47 @@ +findUserBySlackIdOrFail($request->userId); + + /** @var Key|null $key */ + $key = Key::query()->whereNull("user_id")->first(); + + if (!$key) { + throw ValidationException::withMessages(["key" => __("There are no keys in the office.")]); + } + + $key->user()->associate($authUser); + + $key->save(); + + $key->notify(new KeyHasBeenTakenFromTheOfficeNotification($authUser)); + + return $this->respondToSlack( + __(":white_check_mark: Key no. :number has been taken from the office", ["number" => $key->id]), + ); + } +} diff --git a/config/laravel-slack-slash-command.php b/config/laravel-slack-slash-command.php index 996baaeb..0819fb54 100644 --- a/config/laravel-slack-slash-command.php +++ b/config/laravel-slack-slash-command.php @@ -7,8 +7,10 @@ use Toby\Infrastructure\Slack\Handlers\GiveKeysTo; use Toby\Infrastructure\Slack\Handlers\Help; use Toby\Infrastructure\Slack\Handlers\KeyList; +use Toby\Infrastructure\Slack\Handlers\LeaveKeysInOffice; use Toby\Infrastructure\Slack\Handlers\RemoteWork; use Toby\Infrastructure\Slack\Handlers\TakeKeysFrom; +use Toby\Infrastructure\Slack\Handlers\TakeKeysFromOffice; return [ "signing_secret" => env("SLACK_SIGNING_SECRET"), @@ -18,6 +20,8 @@ KeyList::class, RemoteWork::class, DailySummary::class, + TakeKeysFromOffice::class, + LeaveKeysInOffice::class, Help::class, CatchAll::class, ], diff --git a/lang/pl.json b/lang/pl.json index 9011d187..3e2bbd2f 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -144,5 +144,11 @@ "The deadline for OHS training for some employees is about to expire.": "Niedługo mija termin szkolenia BHP dla części pracowników.", "Below is a list of employees with upcoming OHS training:": "Poniżej znajduje się lista pracowników ze zbliżającym się terminem szkolenia BHP:", "The deadline for OHS training for some employees has passed.": "Termin szkolenia BHP dla części pracowników minął.", - "Below is a list of employees with overdue OHS training:": "Poniżej znajduje się lista pracowników z przeterminowanym szkoleniem BHP:" + "Below is a list of employees with overdue OHS training:": "Poniżej znajduje się lista pracowników z przeterminowanym szkoleniem BHP:", + "Take the keys from the office.": "Weź klucze z biura.", + "There are no keys in the office.": "W biurze nie ma kluczy.", + ":white_check_mark: Key no. :number has been taken from the office": ":white_check_mark: Klucz nr :number został zabrany z biura", + "Leave the keys in the office.": "Zostaw klucze w biurze.", + "You don't have any key to leave in the office.": "Nie masz żadnych kluczy do pozostawienia w biurze.", + ":white_check_mark: Key no. :number has been left in the office": ":white_check_mark: Klucz nr :number został pozostawiony w biurze" }