Commit 37c4fc03 authored by Jacob Priddy's avatar Jacob Priddy 👌

Arrow functoins are cool... Also door access requests use cases

parent 27db1ee0
......@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Request extends Model
{
protected $fillable = ['title', 'request', 'reply', 'accepted', 'user_id'];
protected $fillable = ['title', 'request', 'reply', 'resolved', 'user_id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
......
......@@ -286,6 +286,12 @@ return [
// Statistics
Source\UseCases\Admin\Statistics\StatisticsUseCaseServiceProvider::class,
// Requests
Source\UseCases\Requests\GetRequest\GetRequestUseCaseServiceProvider::class,
Source\UseCases\Requests\GetRequests\GetRequestsUseCaseServiceProvider::class,
Source\UseCases\Requests\CreateRequest\CreateRequestUseCaseServiceProvider::class,
Source\UseCases\Requests\InteractWithRequest\InteractWithRequestUseCaseServiceProvider::class,
],
/*
......
......@@ -35,7 +35,7 @@ class Request
string $title,
int $userId,
string $request,
bool $accepted = false,
bool $resolved = false,
?string $reply = null,
?Carbon $createdAt = null,
?Carbon $updatedAt = null
......@@ -46,7 +46,7 @@ class Request
$this->userId = $userId;
$this->request = $request;
$this->reply = $reply;
$this->resolved = $accepted;
$this->resolved = $resolved;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}
......@@ -156,4 +156,20 @@ class Request
{
return (bool)$this->reply;
}
/**
* @param string $reply
*/
public function setReply(string $reply): void
{
$this->reply = $reply;
}
/**
* @param bool $resolved
*/
public function setResolved(bool $resolved): void
{
$this->resolved = $resolved;
}
}
......@@ -5,6 +5,9 @@ namespace Source\Gateways\Requests;
use Source\Entities\Request;
use Source\Sanitize\CastsTo;
use Illuminate\Support\Facades\Log;
use Source\Gateways\PostgresSQLCodes;
use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\Builder;
use Source\Exceptions\EntityNotFoundException;
......@@ -15,16 +18,27 @@ class DatabaseRequestsRepository implements RequestsRepository
/**
* @param \App\Request $dbRequest
* @param \Source\Entities\Request $request
* @throws \Source\Exceptions\EntityNotFoundException
*/
protected static function saveRequest(\App\Request $dbRequest, Request $request): void
{
$dbRequest->setAttribute('title', $request->getTitle());
$dbRequest->setAttribute('request', $request->getRequest());
$dbRequest->setAttribute('reply', $request->getReply());
$dbRequest->setAttribute('accepted', $request->isResolved());
$dbRequest->setAttribute('resolved', $request->isResolved());
$dbRequest->setAttribute('user_id', $request->getUserId());
$dbRequest->save();
try {
$dbRequest->save();
} catch (QueryException $e) {
if ($e->getCode() === PostgresSQLCodes::FOREIGN_KEY_VIOLATION) {
throw new EntityNotFoundException('The user does not exist.');
}
Log::error('Failed saving door request: ' . $e);
throw $e;
}
}
/**
......@@ -39,7 +53,7 @@ class DatabaseRequestsRepository implements RequestsRepository
$request->getAttribute('user_id'),
$request->getAttribute('request'),
$request->getAttribute('reply'),
$request->getAttribute('accepted'),
$request->getAttribute('resolved'),
$request->getAttribute('created_at'),
$request->getAttribute('updated_at')
);
......@@ -75,13 +89,13 @@ class DatabaseRequestsRepository implements RequestsRepository
/**
* @inheritDoc
*/
public function get(string $requestId): ?Request
public function get(string $requestId): Request
{
/** @var \App\Request|null $request */
$request = \App\Request::query()->find(self::castToInt($requestId));
if (!$request) {
return null;
throw new EntityNotFoundException('Request does not exist.');
}
return self::makeRequestFromDb($request);
......
......@@ -33,7 +33,7 @@ class InMemoryRequestsRepository implements RequestsRepository
/**
* @inheritDoc
*/
public function get(string $requestId): ?Request
public function get(string $requestId): Request
{
foreach ($this->requests as $request) {
if ($request->hasIdOf($requestId)) {
......@@ -41,7 +41,7 @@ class InMemoryRequestsRepository implements RequestsRepository
}
}
return null;
throw new EntityNotFoundException('Request does not exist.');
}
/**
......
......@@ -12,7 +12,7 @@ interface RequestsRepository
*
* @param \Source\Entities\Request $request
* @return \Source\Entities\Request
* @throws \Source\Exceptions\EntityExistsException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function create(Request $request): Request;
......@@ -27,9 +27,10 @@ interface RequestsRepository
* Get a specific request
*
* @param string $requestId
* @return \Source\Entities\Request|null
* @return \Source\Entities\Request
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function get(string $requestId): ?Request;
public function get(string $requestId): Request;
/**
* @param string|null $query
......@@ -47,6 +48,7 @@ interface RequestsRepository
* @param \Source\Entities\Request $request
* @return \Source\Entities\Request
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function update(string $requestId, Request $request): Request;
}
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Admin\Statistics;
use JsonException;
use Source\Sanitize\CastsTo;
use Source\UseCases\BasePresenter;
use Source\Entities\DoorFailureRate;
......@@ -51,8 +50,8 @@ class WebPresenter extends BasePresenter implements Presenter
}
try {
$this->viewModel['entries']['labels'] = json_encode(array_map(fn (array $entry) => $this->formatDateTime($entry[0], self::HUMAN_DAY_FORMAT), $responseModel->getEntryCounts()), JSON_THROW_ON_ERROR);
$this->viewModel['entries']['counts'] = json_encode(array_map(fn (array $entry) => $entry[1], $responseModel->getEntryCounts()), JSON_THROW_ON_ERROR);
$this->viewModel['entries']['labels'] = json_encode(array_map(fn(array $entry) => self::formatDateTime($entry[0], self::HUMAN_DAY_FORMAT), $responseModel->getEntryCounts()), JSON_THROW_ON_ERROR);
$this->viewModel['entries']['counts'] = json_encode(array_map(fn(array $entry) => $entry[1], $responseModel->getEntryCounts()), JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$this->viewModel['entries'] = [
'labels' => '[]',
......
......@@ -15,9 +15,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->attempts = array_map(function (Attempt $attempt) {
return $this->formatAttempt($attempt);
}, $responseModel->getAttempts());
$this->attempts = array_map(fn(Attempt $attempt): array => self::formatAttempt($attempt), $responseModel->getAttempts());
}
/** @inheritDoc */
......
......@@ -23,7 +23,7 @@ abstract class BasePresenter
* @param string $format
* @return string|null
*/
public function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
public static function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
{
if ($datetime === null) {
return null;
......@@ -36,7 +36,7 @@ abstract class BasePresenter
* @param \Source\Entities\User $user
* @return array
*/
public function formatFullUser(User $user): array
public static function formatFullUser(User $user): array
{
return [
'id' => $user->getId(),
......@@ -45,9 +45,9 @@ abstract class BasePresenter
'display_name' => $user->getDisplayName(),
'emplid' => $user->getEmplid(),
'email' => $user->getEmail(),
'expires_at' => $this->formatDateTime($user->getExpiresAt()),
'created_at' => $this->formatDateTime($user->getCreatedAt()),
'updated_at' => $this->formatDateTime($user->getUpdatedAt()),
'expires_at' => self::formatDateTime($user->getExpiresAt()),
'created_at' => self::formatDateTime($user->getCreatedAt()),
'updated_at' => self::formatDateTime($user->getUpdatedAt()),
];
}
......@@ -55,7 +55,7 @@ abstract class BasePresenter
* @param \Source\Entities\User $user
* @return array
*/
public function formatPartialUser(User $user): array
public static function formatPartialUser(User $user): array
{
$expired = false;
if ($user->getExpiresAt()) {
......@@ -76,14 +76,14 @@ abstract class BasePresenter
* @param \Source\Entities\Group $group
* @return array
*/
public function formatGroup(Group $group): array
public static function formatGroup(Group $group): array
{
return [
'id' => $group->getId(),
'title' => $group->getTitle(),
'description' => $group->getDescription(),
'created_at' => $this->formatDateTime($group->getCreatedAt()),
'updated_at' => $this->formatDateTime($group->getUpdatedAt()),
'created_at' => self::formatDateTime($group->getCreatedAt()),
'updated_at' => self::formatDateTime($group->getUpdatedAt()),
];
}
......@@ -91,7 +91,7 @@ abstract class BasePresenter
* @param \Source\Entities\Door|null $door
* @return array
*/
public function formatDoor(?Door $door): array
public static function formatDoor(?Door $door): array
{
if (!$door) {
return [];
......@@ -102,9 +102,9 @@ abstract class BasePresenter
'name' => $door->getName(),
'location' => $door->getLocation(),
'version' => $door->getVersion() ?? 'Unknown',
'created_at' => $this->formatDateTime($door->getCreatedAt()),
'updated_at' => $this->formatDateTime($door->getUpdatedAt()),
'last_seen_at' => $this->formatDateTime($door->getLastSeenAt()),
'created_at' => self::formatDateTime($door->getCreatedAt()),
'updated_at' => self::formatDateTime($door->getUpdatedAt()),
'last_seen_at' => self::formatDateTime($door->getLastSeenAt()),
];
}
......@@ -112,7 +112,7 @@ abstract class BasePresenter
* @param \Source\Entities\Token|null $token
* @return array
*/
public function formatToken(?Token $token): array
public static function formatToken(?Token $token): array
{
if (!$token) {
return [];
......@@ -122,9 +122,9 @@ abstract class BasePresenter
'id' => $token->getId(),
'name' => $token->getName(),
'user_id' => $token->getUserId(),
'expires_at' => $this->formatDateTime($token->getExpiresAt()),
'created_at' => $this->formatDateTime($token->getCreatedAt()),
'updated_at' => $this->formatDateTime($token->getUpdatedAt()),
'expires_at' => self::formatDateTime($token->getExpiresAt()),
'created_at' => self::formatDateTime($token->getCreatedAt()),
'updated_at' => self::formatDateTime($token->getUpdatedAt()),
];
}
......@@ -132,7 +132,7 @@ abstract class BasePresenter
* @param \Source\Entities\Schedule|null $schedule
* @return array
*/
public function formatSchedule(?Schedule $schedule): array
public static function formatSchedule(?Schedule $schedule): array
{
if (!$schedule) {
return [];
......@@ -145,10 +145,10 @@ abstract class BasePresenter
'rset' => $schedule->getRset(),
'duration' => $schedule->getDuration(),
'description' => $schedule->getDescription(),
'start_date' => $this->formatDateTime($schedule->getStart()),
'end_date' => $this->formatDateTime($schedule->getEnd()),
'created_at' => $this->formatDateTime($schedule->getCreatedAt()),
'updated_at' => $this->formatDateTime($schedule->getUpdatedAt()),
'start_date' => self::formatDateTime($schedule->getStart()),
'end_date' => self::formatDateTime($schedule->getEnd()),
'created_at' => self::formatDateTime($schedule->getCreatedAt()),
'updated_at' => self::formatDateTime($schedule->getUpdatedAt()),
];
}
......@@ -156,7 +156,7 @@ abstract class BasePresenter
* @param \Source\Entities\Attempt|null $attempt
* @return array
*/
public function formatAttempt(?Attempt $attempt): array
public static function formatAttempt(?Attempt $attempt): array
{
if (!$attempt) {
return [];
......@@ -165,7 +165,7 @@ abstract class BasePresenter
return [
'id' => $attempt->getId(),
'door_id' => $attempt->getDoorId(),
'created_at' => $this->formatDateTime($attempt->getCreatedAt()),
'created_at' => self::formatDateTime($attempt->getCreatedAt()),
];
}
......@@ -173,7 +173,7 @@ abstract class BasePresenter
* @param \Source\Entities\Entry|null $entry
* @return array
*/
public function formatEntry(?Entry $entry): array
public static function formatEntry(?Entry $entry): array
{
if (!$entry) {
return [];
......@@ -184,7 +184,7 @@ abstract class BasePresenter
'user_id' => $entry->getUserId(),
'door_id' => $entry->getDoorId(),
'success' => $entry->wasSuccessful(),
'created_at' => $this->formatDateTime($entry->getCreatedAt()),
'created_at' => self::formatDateTime($entry->getCreatedAt()),
];
}
......@@ -192,7 +192,7 @@ abstract class BasePresenter
* @param \Source\Entities\Override|null $override
* @return array
*/
public function formatOverride(?Override $override): array
public static function formatOverride(?Override $override): array
{
if (!$override) {
return [];
......@@ -204,10 +204,10 @@ abstract class BasePresenter
'user_id' => $override->getUserId(),
'door_id' => $override->getDoorId(),
'type' => $override->getType(),
'start' => $this->formatDateTime($override->getStart()),
'end' => $this->formatDateTime($override->getEnd()),
'created_at' => $this->formatDateTime($override->getCreatedAt()),
'updated_at' => $this->formatDateTime($override->getUpdatedAt()),
'start' => self::formatDateTime($override->getStart()),
'end' => self::formatDateTime($override->getEnd()),
'created_at' => self::formatDateTime($override->getCreatedAt()),
'updated_at' => self::formatDateTime($override->getUpdatedAt()),
];
}
}
......@@ -14,19 +14,19 @@ class EventsPresenter extends BasePresenter implements Presenter
{
$events = $responseModel->getOpenEvents();
$this->viewModel = array_map(function (ScheduleEvent $event) {
$this->viewModel = array_map(static function (ScheduleEvent $event) {
return [
'title' => 'Door unlocked.',
'start' => $this->formatDateTime($event->getBegin()),
'end' => $this->formatDateTime($event->getEnd()),
'start' => self::formatDateTime($event->getBegin()),
'end' => self::formatDateTime($event->getEnd()),
];
}, $events);
foreach ($responseModel->getUserEvents() as $event) {
$this->viewModel[] = [
'title' => 'Allowed via doorcode.',
'start' => $this->formatDateTime($event->getBegin()),
'end' => $this->formatDateTime($event->getEnd()),
'start' => self::formatDateTime($event->getBegin()),
'end' => self::formatDateTime($event->getEnd()),
];
}
}
......
......@@ -19,10 +19,10 @@ class JsonPresenter extends BasePresenter implements Presenter
return $a->cmp($b);
});
$this->viewModel['events'] = array_map(function (ScheduleEvent $event) {
$this->viewModel['events'] = array_map(static function (ScheduleEvent $event) {
return [
'begins_at' => $this->formatDateTime($event->getBegin()),
'ends_at' => $this->formatDateTime($event->getEnd()),
'begins_at' => self::formatDateTime($event->getBegin()),
'ends_at' => self::formatDateTime($event->getEnd()),
];
}, $events);
}
......
......@@ -15,9 +15,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->doors = array_map(function (Door $door) {
return $this->formatDoor($door);
}, $responseModel->getDoors());
$this->doors = array_map(fn(Door $door): array => self::formatDoor($door), $responseModel->getDoors());
}
/** @inheritDoc */
......
......@@ -32,14 +32,14 @@ class MePresenter extends BasePresenter implements Presenter
'location' => $door->getLocation(),
];
}, $responseModel->getDoorsForGroup($group));
$this->viewModel['schedules'][$group->getId()] = array_map(function (Schedule $schedule) {
$this->viewModel['schedules'][$group->getId()] = array_map(static function (Schedule $schedule) {
return [
'id' => $schedule->getId(),
'description' => $schedule->getDescription(),
'rset' => $schedule->getRset(),
'duration' => $schedule->getDuration(),
'start_date' => $this->formatDateTime($schedule->getStart()),
'end_date' => $this->formatDateTime($schedule->getEnd()),
'start_date' => self::formatDateTime($schedule->getStart()),
'end_date' => self::formatDateTime($schedule->getEnd()),
];
}, $responseModel->getSchedulesForGroup($group));
}
......
......@@ -18,18 +18,12 @@ class PrivilegedApiPresenter extends BasePresenter implements Presenter
public function present(ResponseModel $responseModel): void
{
$groups = $responseModel->getGroups();
$this->viewModel['groups'] = array_map(function (Group $group) {
return $this->formatGroup($group);
}, $groups);
$this->viewModel['groups'] = array_map(fn(Group $group): array => self::formatGroup($group), $groups);
$this->viewModel['doors'] = [];
$this->viewModel['schedules'] = [];
foreach ($groups as $group) {
$this->viewModel['doors'][$group->getId()] = array_map(function (Door $door) {
return $this->formatDoor($door);
}, $responseModel->getDoorsForGroup($group));
$this->viewModel['schedules'][$group->getId()] = array_map(function (Schedule $schedule) {
return $this->formatSchedule($schedule);
}, $responseModel->getSchedulesForGroup($group));
$this->viewModel['doors'][$group->getId()] = array_map(fn(Door $door): array => self::formatDoor($door), $responseModel->getDoorsForGroup($group));
$this->viewModel['schedules'][$group->getId()] = array_map(fn(Schedule $schedule): array => self::formatSchedule($schedule), $responseModel->getSchedulesForGroup($group));
}
}
......
......@@ -12,7 +12,7 @@ class APIPresenter extends BasePresenter implements Presenter
public function present(ResponseModel $responseModel): void
{
$this->viewModel['door_token'] = $responseModel->getToken();
$this->viewModel['door'] = $this->formatDoor($responseModel->getDoor());
$this->viewModel['door'] = self::formatDoor($responseModel->getDoor());
}
/** @inheritDoc */
......
......@@ -11,7 +11,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['door'] = $this->formatDoor($responseModel->getDoor());
$this->viewModel['door'] = self::formatDoor($responseModel->getDoor());
}
/** @inheritDoc */
......
......@@ -12,7 +12,7 @@ class APIPresenter extends BasePresenter implements Presenter
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
$this->viewModel['door'] = $this->formatDoor($responseModel->getDoor());
$this->viewModel['door'] = self::formatDoor($responseModel->getDoor());
}
/** @inheritDoc */
......
......@@ -15,9 +15,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->entries = array_map(function (Entry $entry) {
return $this->formatEntry($entry);
}, $responseModel->getEntries());
$this->entries = array_map(fn(Entry $entry): array => self::formatEntry($entry), $responseModel->getEntries());
}
/** @inheritDoc */
......
......@@ -15,9 +15,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->users = array_map(function (User $user) {
return $this->formatPartialUser($user);
}, $responseModel->getUsers());
$this->users = array_map(fn(User $user): array => self::formatPartialUser($user), $responseModel->getUsers());
}
/** @inheritDoc */
......
......@@ -11,7 +11,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['group'] = $this->formatGroup($responseModel->getGroup());
$this->viewModel['group'] = self::formatGroup($responseModel->getGroup());
}
/** @inheritDoc */
......
......@@ -11,7 +11,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['group'] = $this->formatGroup($responseModel->getGroup());
$this->viewModel['group'] = self::formatGroup($responseModel->getGroup());
}
/** @inheritDoc */
......
......@@ -15,9 +15,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->groups = array_map(function (Group $group) {
return $this->formatGroup($group);
}, $responseModel->getGroups());
$this->groups = array_map(fn(Group $group): array => self::formatGroup($group), $responseModel->getGroups());
}
/** @inheritDoc */
......
......@@ -17,7 +17,7 @@ class APIPresenter extends BasePresenter implements Presenter
if ($responseModel->hasError()) {
$this->viewModel['message'] = $responseModel->getError();