Skip to content

Commit

Permalink
feat: Update Validations API to Support Roster (#3761)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalumTowers authored Aug 13, 2024
1 parent 402ce31 commit 320bc23
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
50 changes: 0 additions & 50 deletions app/Http/Controllers/Api/CTS/ValidationsController.php

This file was deleted.

43 changes: 43 additions & 0 deletions app/Http/Controllers/Api/ValidationsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Atc\Position;
use App\Models\Roster;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;

class ValidationsController
{
public function view(Request $request)
{
if (! $request->get('position')) {
return response()->json([
'status' => '400',
'message' => 'No position was supplied.',
], 400);
}

try {
$position = Position::where('callsign', $request->get('position'))->firstOrFail();
} catch (ModelNotFoundException $e) {
return response()->json([
'status' => '404',
'message' => 'Position not found.',
], 404);
}

$validatedMembers = cache()->remember("validation_members_{$position->id}", now()->addMinutes(30), function () use ($position) {
return Roster::all()->filter(function (Roster $roster) use ($position) {
return $roster->accountCanControl($position);
})->map(function (Roster $roster) {
return ['id' => $roster->account_id];
});
});

return response()->json([
'status' => ['position' => $position->name],
'validated_members' => $validatedMembers,
]);
}
}
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

Route::get('validations')->uses('Api\CTS\ValidationsController@view')->name('api.validations');
Route::get('validations')->uses('Api\ValidationsController@view')->name('api.validations');
Route::get('metar/{airportIcao}')->uses('Site\MetarController@get')->name('api.metar');

Route::group([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace Tests\Feature\CTS;
namespace Tests\Feature\API;

use App\Models\Cts\Member;
use App\Models\Cts\Validation;
use App\Models\Cts\ValidationPosition;
use App\Models\Atc\Position;
use App\Models\Mship\Account;
use App\Models\Mship\Account\Endorsement;
use App\Models\Mship\Qualification;
use App\Models\Mship\State;
use App\Models\Roster;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

Expand Down Expand Up @@ -35,23 +38,32 @@ public function itReturns404WhenPositionDoesNotExist()
/** @test */
public function itReturnsAJsonResponse()
{
$member = factory(Member::class)->create();
$position = factory(ValidationPosition::class)->create();
$validation = factory(Validation::class)->create([
'member_id' => $member,
'position_id' => $position,
$qualification = Qualification::code('S2')->first();
$account = Account::factory()->create();
$account->addQualification($qualification);
$account->addState(State::findByCode('DIVISION'));
$position = Position::factory()->create([
'type' => Position::TYPE_TOWER,
]);
Roster::create([
'account_id' => $account->id,
]);

$endorsement = Endorsement::factory()->create([
'account_id' => $account,
'endorsable_type' => Position::class,
'endorsable_id' => $position,
]);

$this->call('GET', route('api.validations'), ['position' => $position->position])
$this->call('GET', route('api.validations'), ['position' => $position->callsign])
->assertStatus(200)
->assertExactJson([
'status' => [
'position' => $position->position,
'position' => $position->name,
],
'validated_members' => [
[
'id' => $member->cid,
'name' => $member->name,
'id' => $endorsement->account_id,
],
],
]);
Expand Down

0 comments on commit 320bc23

Please sign in to comment.