API: Show from schedule via shift type, added comments

This commit is contained in:
Igor Scheller 2023-12-30 17:22:32 +01:00 committed by Michael Weimann
parent 3432829c91
commit 8c64447273
4 changed files with 68 additions and 11 deletions

View File

@ -24,7 +24,7 @@ ENV TRUSTED_PROXIES 10.0.0.0/8,::ffff:10.0.0.0/8,\
# Engelsystem development workspace # Engelsystem development workspace
# Contains all tools required to build / manage the system # Contains all tools required to build / manage the system
FROM es_base AS es_workspace FROM es_base AS es_workspace
RUN echo 'memory_limit = 512M' > /usr/local/etc/php/conf.d/docker-php.ini RUN echo 'memory_limit = 1024M' > /usr/local/etc/php/conf.d/docker-php.ini
RUN apk add --no-cache gettext git nodejs npm yarn RUN apk add --no-cache gettext git nodejs npm yarn
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
ENTRYPOINT php -r 'sleep(PHP_INT_MAX);' ENTRYPOINT php -r 'sleep(PHP_INT_MAX);'

View File

@ -35,7 +35,7 @@ class ShiftsController extends ApiController
'shift.shiftEntries.user.contact', 'shift.shiftEntries.user.contact',
'shift.shiftEntries.user.personalData', 'shift.shiftEntries.user.personalData',
'shift.shiftType', 'shift.shiftType',
'shift.schedule', 'shift.schedule.shiftType.neededAngelTypes.angelType',
]) ])
->get(); ->get();
@ -63,7 +63,7 @@ class ShiftsController extends ApiController
'shiftEntries.user.contact', 'shiftEntries.user.contact',
'shiftEntries.user.personalData', 'shiftEntries.user.personalData',
'shiftType', 'shiftType',
'schedule', 'schedule.shiftType.neededAngelTypes.angelType',
]) ])
->orderBy('start') ->orderBy('start')
->get(); ->get();
@ -85,7 +85,7 @@ class ShiftsController extends ApiController
'shift.shiftEntries.user.contact', 'shift.shiftEntries.user.contact',
'shift.shiftEntries.user.personalData', 'shift.shiftEntries.user.personalData',
'shift.shiftType', 'shift.shiftType',
'shift.schedule', 'shift.schedule.shiftType.neededAngelTypes.angelType',
]) ])
->get(); ->get();
@ -139,10 +139,14 @@ class ShiftsController extends ApiController
*/ */
protected function getNeededAngelTypes(Shift $shift): Collection protected function getNeededAngelTypes(Shift $shift): Collection
{ {
$neededAngelTypes = new Collection();
if (!$shift->schedule) { if (!$shift->schedule) {
// Get from shift // Get from shift
$neededAngelTypes = $shift->neededAngelTypes; $neededAngelTypes = $shift->neededAngelTypes;
} else { } elseif ($shift->schedule->needed_from_shift_type) {
// Load instead from shift type
$neededAngelTypes = $shift->schedule->shiftType->neededAngelTypes;
} elseif (!$shift->schedule->needed_from_shift_type) {
// Load instead from location // Load instead from location
$neededAngelTypes = $shift->location->neededAngelTypes; $neededAngelTypes = $shift->location->neededAngelTypes;
} }

View File

@ -66,7 +66,8 @@ class ShiftsController extends BaseController
$query $query
->where('angel_types.restricted', false) ->where('angel_types.restricted', false)
->orWhereNot('confirm_user_id', false); ->orWhereNot('confirm_user_id', false);
}); })
->pluck('id');
/** @var ShiftEntry[]|DbCollection $shiftEntries */ /** @var ShiftEntry[]|DbCollection $shiftEntries */
$shiftEntries = $user->shiftEntries()->with('shift')->get(); $shiftEntries = $user->shiftEntries()->with('shift')->get();
@ -75,20 +76,25 @@ class ShiftsController extends BaseController
// Load needed from shift if no schedule configured, else from room // Load needed from shift if no schedule configured, else from room
->leftJoin('schedule_shift', 'schedule_shift.shift_id', 'shifts.id') ->leftJoin('schedule_shift', 'schedule_shift.shift_id', 'shifts.id')
->leftJoin('schedules', 'schedules.id', 'schedule_shift.schedule_id') ->leftJoin('schedules', 'schedules.id', 'schedule_shift.schedule_id')
// From shift
->leftJoin('needed_angel_types', function (JoinClause $query): void { ->leftJoin('needed_angel_types', function (JoinClause $query): void {
$query->on('needed_angel_types.shift_id', 'shifts.id') $query->on('needed_angel_types.shift_id', 'shifts.id')
->whereNull('schedule_shift.shift_id'); ->whereNull('schedule_shift.shift_id');
}) })
// Via schedule shift type
->leftJoin('needed_angel_types AS nast', function (JoinClause $query): void { ->leftJoin('needed_angel_types AS nast', function (JoinClause $query): void {
$query->on('nast.shift_type_id', 'shifts.shift_type_id') $query->on('nast.shift_type_id', 'shifts.shift_type_id')
->whereNotNull('schedule_shift.shift_id') ->whereNotNull('schedule_shift.shift_id')
->where('schedules.needed_from_shift_type', true); ->where('schedules.needed_from_shift_type', true);
}) })
// Via schedule location
->leftJoin('needed_angel_types AS nas', function (JoinClause $query): void { ->leftJoin('needed_angel_types AS nas', function (JoinClause $query): void {
$query->on('nas.location_id', 'shifts.location_id') $query->on('nas.location_id', 'shifts.location_id')
->whereNotNull('schedule_shift.shift_id') ->whereNotNull('schedule_shift.shift_id')
->where('schedules.needed_from_shift_type', false); ->where('schedules.needed_from_shift_type', false);
}) })
// Not already signed in // Not already signed in
->whereNotIn('shifts.id', $shiftEntries->pluck('shift_id')) ->whereNotIn('shifts.id', $shiftEntries->pluck('shift_id'))
// Same angel types // Same angel types

View File

@ -23,6 +23,7 @@ use Illuminate\Database\Eloquent\ModelNotFoundException;
class ShiftsControllerTest extends ApiBaseControllerTest class ShiftsControllerTest extends ApiBaseControllerTest
{ {
protected Location $location; protected Location $location;
protected Schedule $schedule;
protected Shift $shiftA; protected Shift $shiftA;
protected Shift $shiftB; protected Shift $shiftB;
@ -75,12 +76,43 @@ class ShiftsControllerTest extends ApiBaseControllerTest
$this->assertEquals($this->shiftB->title, $shiftBData['title'], 'Title is equal'); $this->assertEquals($this->shiftB->title, $shiftBData['title'], 'Title is equal');
$this->assertEquals($this->location->id, $shiftBData['location']['id'], 'Same location'); $this->assertEquals($this->location->id, $shiftBData['location']['id'], 'Same location');
$this->assertEquals($this->shiftB->shiftType->id, $shiftBData['shift_type']['id'], 'Shift type equals'); $this->assertEquals($this->shiftB->shiftType->id, $shiftBData['shift_type']['id'], 'Shift type equals');
$this->assertCount(2, $shiftBData['entries']); $this->assertCount(3, $shiftBData['entries']);
// No users // No users
$entriesB = collect($shiftBData['entries'])->sortBy('type.id'); $entriesB = collect($shiftBData['entries'])->sortBy('type.id');
$this->assertCount(0, $entriesB[0]['users']); $this->assertCount(0, $entriesB[0]['users']);
} }
/**
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByAngeltype
* @covers \Engelsystem\Controllers\Api\ShiftsController::getNeededAngelTypes
*/
public function testEntriesViaShiftType(): void
{
$this->schedule->needed_from_shift_type = true;
$this->schedule->save();
/** @var ShiftEntry $firstEntry */
$firstEntry = $this->shiftB->shiftEntries->first();
$request = new Request();
$request = $request->withAttribute('angeltype_id', $firstEntry->angelType->id);
$controller = new ShiftsController(new Response());
$response = $controller->entriesByAngeltype($request);
$this->validateApiResponse('/angeltypes/{id}/shifts', 'get', $response);
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
$this->assertJson($response->getContent());
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('data', $data);
$this->assertCount(5, $data['data']);
$shift = $data['data'][0];
$this->assertTrue(count($shift['entries']) >= 1);
}
/** /**
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByAngeltype * @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByAngeltype
*/ */
@ -174,7 +206,7 @@ class ShiftsControllerTest extends ApiBaseControllerTest
parent::setUp(); parent::setUp();
$this->location = Location::factory()->create(); $this->location = Location::factory()->create();
$schedule = Schedule::factory()->create(); $this->schedule = Schedule::factory()->create();
// Shifts // Shifts
$this->shiftA = Shift::factory(1) $this->shiftA = Shift::factory(1)
@ -183,7 +215,8 @@ class ShiftsControllerTest extends ApiBaseControllerTest
$this->shiftB = Shift::factory(1) $this->shiftB = Shift::factory(1)
->create(['location_id' => $this->location->id, 'start' => Carbon::now()->addHour()]) ->create(['location_id' => $this->location->id, 'start' => Carbon::now()->addHour()])
->first(); ->first();
(new ScheduleShift(['shift_id' => $this->shiftB->id, 'schedule_id' => $schedule->id, 'guid' => 'a']))->save(); (new ScheduleShift(['shift_id' => $this->shiftB->id, 'schedule_id' => $this->schedule->id, 'guid' => 'a']))
->save();
// "Empty" entry to be skipped // "Empty" entry to be skipped
NeededAngelType::factory(1)->create(['location_id' => null, 'shift_id' => $this->shiftA->id, 'count' => 0]); NeededAngelType::factory(1)->create(['location_id' => null, 'shift_id' => $this->shiftA->id, 'count' => 0]);
@ -191,19 +224,31 @@ class ShiftsControllerTest extends ApiBaseControllerTest
// Needed entry by shift // Needed entry by shift
/** @var NeededAngelType $byShift */ /** @var NeededAngelType $byShift */
$byShift = NeededAngelType::factory(2) $byShift = NeededAngelType::factory(2)
->create(['location_id' => null, 'shift_id' => $this->shiftA->id, 'count' => 2]) ->create(['location_id' => null, 'shift_type_id' => null, 'shift_id' => $this->shiftA->id, 'count' => 2])
->first(); ->first();
// Needed entry by location // Needed entry by location
/** @var NeededAngelType $byLocation */ /** @var NeededAngelType $byLocation */
$byLocation = NeededAngelType::factory(1) $byLocation = NeededAngelType::factory(1)
->create(['location_id' => $this->location->id, 'shift_id' => null, 'count' => 3]) ->create(['location_id' => $this->location->id, 'shift_type_id' => null, 'shift_id' => null, 'count' => 3])
->first(); ->first();
// Needed entry by shift type
$shiftType = $this->shiftB->shiftType;
/** @var NeededAngelType $byShiftType */
$byShiftType = NeededAngelType::factory(2)
->create(['location_id' => null, 'shift_type_id' => $shiftType->id, 'count' => 3])
->first();
ShiftEntry::factory(5)->create([
'shift_id' => $this->shiftB->id,
'angel_type_id' => $byShiftType->angel_type_id,
]);
// Added by both // Added by both
NeededAngelType::factory(1) NeededAngelType::factory(1)
->create([ ->create([
'location_id' => $this->location->id, 'location_id' => $this->location->id,
'shift_type_id' => null,
'shift_id' => null, 'shift_id' => null,
'angel_type_id' => $byShift->angel_type_id, 'angel_type_id' => $byShift->angel_type_id,
'count' => 3, 'count' => 3,
@ -225,6 +270,8 @@ class ShiftsControllerTest extends ApiBaseControllerTest
// Additional (not required by shift nor location) // Additional (not required by shift nor location)
ShiftEntry::factory(1)->create(['shift_id' => $this->shiftA->id]); ShiftEntry::factory(1)->create(['shift_id' => $this->shiftA->id]);
$this->schedule->shiftType()->associate($shiftType);
foreach (User::all() as $user) { foreach (User::all() as $user) {
// Generate user data // Generate user data
/** @var User $user */ /** @var User $user */