Skip to content

Commit

Permalink
#250 - add command for giving keys to office and taking them (#342)
Browse files Browse the repository at this point in the history
* #250 - wip

* #250 - wip

* #250 - wip

* #250 - fix

* #250 - fix

* #250 - fix

* Apply suggestions from code review

Co-authored-by: Krzysztof Rewak <[email protected]>

---------

Co-authored-by: Krzysztof Rewak <[email protected]>
  • Loading branch information
EwelinaSkrzypacz and krzysztofrewak authored Sep 25, 2023
1 parent 3454ecf commit 16d2277
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/Infrastructure/Slack/Handlers/DailySummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}
47 changes: 47 additions & 0 deletions app/Infrastructure/Slack/Handlers/LeaveKeysInOffice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Toby\Infrastructure\Slack\Handlers;

use Illuminate\Validation\ValidationException;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Toby\Domain\Notifications\KeyHasBeenLeftInTheOffice;
use Toby\Eloquent\Models\Key;
use Toby\Infrastructure\Slack\Exceptions\UserNotFoundException;
use Toby\Infrastructure\Slack\Traits\FindsUserBySlackId;

class LeaveKeysInOffice extends SignatureHandler
{
use FindsUserBySlackId;

protected $signature = "toby klucze:biuro:zostaw";
protected $description = "Leave the keys in the office.";

/**
* @throws UserNotFoundException
* @throws ValidationException
*/
public function handle(Request $request): Response
{
$authUser = $this->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]),
);
}
}
47 changes: 47 additions & 0 deletions app/Infrastructure/Slack/Handlers/TakeKeysFromOffice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Toby\Infrastructure\Slack\Handlers;

use Illuminate\Validation\ValidationException;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Toby\Domain\Notifications\KeyHasBeenTakenFromTheOfficeNotification;
use Toby\Eloquent\Models\Key;
use Toby\Infrastructure\Slack\Exceptions\UserNotFoundException;
use Toby\Infrastructure\Slack\Traits\FindsUserBySlackId;

class TakeKeysFromOffice extends SignatureHandler
{
use FindsUserBySlackId;

protected $signature = "toby klucze:biuro:wez";
protected $description = "Take the keys from the office.";

/**
* @throws UserNotFoundException
* @throws ValidationException
*/
public function handle(Request $request): Response
{
$authUser = $this->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]),
);
}
}
4 changes: 4 additions & 0 deletions config/laravel-slack-slash-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -18,6 +20,8 @@
KeyList::class,
RemoteWork::class,
DailySummary::class,
TakeKeysFromOffice::class,
LeaveKeysInOffice::class,
Help::class,
CatchAll::class,
],
Expand Down
8 changes: 7 additions & 1 deletion lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 16d2277

Please sign in to comment.