Commit 02ce4ebd authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

current progress on this

parent 0e35ca28
......@@ -38,11 +38,11 @@ use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCaseServiceProvider;
use Source\UseCases\Tokens\CreateToken\CreateTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCaseServiceProvider;
use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\Schedules\ScheduleGet\ScheduleGetUseCaseServiceProvider;
use Source\UseCases\Door\StatusResponse\StatusResponseUseCaseServiceProvider;
use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCaseServiceProvider;
......@@ -69,6 +69,7 @@ use Source\UseCases\Entries\GetEntriesForDoorAndUser\GetEntriesForDoorAndUserUse
use Source\UseCases\GroupSchedule\ActiveSchedulesForGroup\ActiveSchedulesForGroupUseCaseServiceProvider;
use Source\UseCases\Door\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
use Source\UseCases\Users\Authenticate\AuthenticateUseCaseServiceProvider as UserAuthenticateUseCaseServiceProvider;
use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider as TokenAuthenticateUseCaseServiceProvider;
class AppServiceProvider extends ServiceProvider
{
......@@ -125,6 +126,8 @@ class AppServiceProvider extends ServiceProvider
// Door
AccessUseCaseServiceProvider::class,
StatusResponseUseCaseServiceProvider::class,
DoorAuthenticateUseCaseServiceProvider::class,
// Doors
GetDoorUseCaseServiceProvider::class,
......@@ -132,8 +135,6 @@ class AppServiceProvider extends ServiceProvider
DeleteDoorUseCaseServiceProvider::class,
UpdateDoorUseCaseServiceProvider::class,
GetAllDoorsUseCaseServiceProvider::class,
AuthenticateUseCaseServiceProvider::class,
DoorAuthenticateUseCaseServiceProvider::class,
UserAuthenticateUseCaseServiceProvider::class,
GenerateDoorTokenUseCaseServiceProvider::class,
......@@ -143,6 +144,7 @@ class AppServiceProvider extends ServiceProvider
ExpireTokenUseCaseServiceProvider::class,
UpdateTokenUseCaseServiceProvider::class,
GetAllTokensUseCaseServiceProvider::class,
TokenAuthenticateUseCaseServiceProvider::class,
// TokenUser
GetUserTokensUseCaseServiceProvider::class,
......
......@@ -3,6 +3,7 @@
namespace Source\Gateways\DoorSchedule;
use Carbon\Carbon;
use Source\Sanitize\CastsTo;
use Source\Entities\Schedule;
use Illuminate\Database\ConnectionInterface;
......@@ -11,6 +12,8 @@ class DatabaseDoorScheduleRepository implements DoorScheduleRepository
{
use CastsTo;
protected const DB_DATE_FORMAT = 'Y-m-d H:i:s';
/**
* @var \Illuminate\Database\ConnectionInterface
*/
......@@ -38,6 +41,44 @@ QUERY;
$this->castToInt($doorId),
]);
return array_map(function ($schedule) {
return new Schedule(
$schedule->id,
$schedule->group_id,
$schedule->type,
$schedule->rset,
$schedule->duration_ms,
$schedule->description,
$this->castToDate($schedule->start),
$this->castToDate($schedule->end),
$this->castToDate($schedule->created_at),
$this->castToDate($schedule->updated_at)
);
}, $schedules);
}
/**
* @inheritDoc
*/
public function getSchedulesForDorBetween(string $doorId, Carbon $begin, Carbon $end, int $type = Schedule::TYPE_OPEN_MODE): array
{
$format = self::DB_DATE_FORMAT;
$query = <<<QUERY
select S.id, S.group_id, S.type, S.rset, S.start, S.end, S.duration_ms, S.description, S.created_at, S.updated_at
FROM schedules as S
INNER JOIN door_group AS DG ON DG.group_id = S.group_id AND DG.door_id = :DOOR_ID
WHERE S.type = :TYPE AND (((to_timestamp(:BEGIN, '$format'), to_timestamp(:END, '$format')) OVERLAPS (s.start, S.end))
OR (S.start < to_timestamp(:END, '$format') AND S.end IS NULL))
QUERY;
$schedules = $this->db->select($query, [
':TYPE' => $this->castToInt($type),
':DOOR_ID' => $this->castToInt($doorId),
':BEGIN' => $begin->format(self::DB_DATE_FORMAT),
':END' => $end->format(self::DB_DATE_FORMAT),
]);
return array_map(function ($schedule) {
return new Schedule(
$schedule->id,
......
......@@ -3,6 +3,9 @@
namespace Source\Gateways\DoorSchedule;
use Carbon\Carbon;
use Source\Entities\Schedule;
interface DoorScheduleRepository
{
/**
......@@ -10,4 +13,13 @@ interface DoorScheduleRepository
* @return \Source\Entities\Schedule[]
*/
public function getActiveSchedulesForDoor(string $doorId): array;
/**
* @param string $doorId
* @param \Carbon\Carbon $begin
* @param \Carbon\Carbon $end
* @param int $type
* @return \Source\Entities\Schedule[]
*/
public function getSchedulesForDorBetween(string $doorId, Carbon $begin, Carbon $end, int $type = Schedule::TYPE_OPEN_MODE): array;
}
......@@ -3,6 +3,7 @@
namespace Source\Gateways\DoorSchedule;
use Carbon\Carbon;
use Source\Entities\Schedule;
class InMemoryDoorScheduleRepository implements DoorScheduleRepository
......@@ -32,4 +33,16 @@ class InMemoryDoorScheduleRepository implements DoorScheduleRepository
return $this->doorScheduleMap[$doorId];
}
/**
* @inheritDoc
*/
public function getSchedulesForDorBetween(string $doorId, Carbon $begin, Carbon $end, int $type = Schedule::TYPE_OPEN_MODE): array
{
if (!isset($this->doorScheduleMap[$doorId])) {
return [];
}
return $this->doorScheduleMap[$doorId];
}
}
<?php
namespace Source\UseCases\Door\StatusResponse;
use Source\UseCases\BasePresenter;
class JsonPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Door\StatusResponse;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Door\StatusResponse;
class ResponseModel
{
}
<?php
namespace Source\UseCases\Door\StatusResponse;
use Carbon\Carbon;
use Source\Gateways\Schedules\SchedulesRepository;
use Source\Gateways\Overrides\OverridesRepository;
use Source\Gateways\DoorSchedule\DoorScheduleRepository;
class StatusResponse implements StatusResponseUseCase
{
/**
* @var \Source\Gateways\Overrides\OverridesRepository
*/
protected OverridesRepository $overrides;
/**
* @var \Source\Gateways\DoorSchedule\DoorScheduleRepository
*/
protected DoorScheduleRepository $schedules;
/**
* The only things that can change the status of a door is a schedule or an override
* So we look at schedules and overrides to get the status events.
*
* @param \Source\Gateways\DoorSchedule\DoorScheduleRepository $schedules
* @param \Source\Gateways\Overrides\OverridesRepository $overrides
*/
public function __construct(DoorScheduleRepository $schedules, OverridesRepository $overrides)
{
$this->overrides = $overrides;
$this->schedules = $schedules;
}
/**
* @inheritDoc
*/
public function getStatusForDoor(string $doorId, Carbon $date, Presenter $presenter): void
{
// $this->schedules->getActiveSchedulesForDoor($doorId, $date)
}
}
<?php
namespace Source\UseCases\Door\StatusResponse;
use Carbon\Carbon;
interface StatusResponseUseCase
{
/**
* Get the status of a door at a specific date
*
* @param string $doorId
* @param \Carbon\Carbon $date
* @param \Source\UseCases\Door\StatusResponse\Presenter $presenter
*/
public function getStatusForDoor(string $doorId, Carbon $date, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Door\StatusResponse;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class StatusResponseUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(StatusResponseUseCase::class, static function (Application $app) {
return new StatusResponse();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [StatusResponseUseCase::class];
}
}
......@@ -168,4 +168,58 @@ class DoorScheduleDatabaseTest extends DatabaseTestCase
$this->schedules->create(new Schedule(0, $g->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->addDay(), Carbon::now()->addDays(10)));
$this->assertCount(0, $schedules);
}
/**
* @test
* @throws \Source\Exceptions\EntityExistsException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_gets_schedules_for_a_door_between_dates(): void
{
$g1 = $this->groups->create(new Group(
0,
'title',
'description'
));
$g2 = $this->groups->create(new Group(
0,
'title2',
'description2'
));
$d1 = $this->doors->create(new Door(
0,
'location',
'name',
new HashedSearchable('hash')
));
$d2 = $this->doors->create(new Door(
0,
'location2',
'name2',
new HashedSearchable('hash2')
));
$this->doorGroups->addDoorToGroup($d1->getId(), $g1->getId());
$this->doorGroups->addDoorToGroup($d2->getId(), $g2->getId());
// See the next three
$s1 = $this->schedules->create(new Schedule(0, $g1->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->subYear()));
$s2 = $this->schedules->create(new Schedule(0, $g1->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->subYear(), Carbon::now()->addYear()));
$s3 = $this->schedules->create(new Schedule(0, $g1->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->subSecond(), Carbon::now()->addSecond()));
// After search period don't see this one
$this->schedules->create(new Schedule(0, $g1->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->addDay()));
// Different group, don't see this one
$this->schedules->create(new Schedule(0, $g2->getId(), Schedule::TYPE_OPEN_MODE, '', 1235, '', Carbon::now()->subSecond(), Carbon::now()->addSecond()));
// Different type, don't see this one
$this->schedules->create(new Schedule(0, $g1->getId(), Schedule::TYPE_USER_ACCESS, '', 1235, '', Carbon::now()->subSecond(), Carbon::now()->addSecond()));
$schedules = $this->db->getSchedulesForDorBetween($d1->getId(), Carbon::now()->subMinute(), Carbon::now()->addMinute());
$this->assertCount(3, $schedules);
$this->assertEquals($s1->getId(), $schedules[0]->getId());
$this->assertEquals($s2->getId(), $schedules[1]->getId());
$this->assertEquals($s3->getId(), $schedules[2]->getId());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment