Skip to content

Commit

Permalink
New API Bulk Team Grant/Revoke. (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeburg authored Jan 15, 2024
1 parent 3fca804 commit 9f2753f
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 4 deletions.
32 changes: 29 additions & 3 deletions app/Http/Controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Lib\BulkTeamGrantRevoke;
use App\Lib\Reports\PeopleByTeamsReport;
use App\Models\Team;
use Illuminate\Auth\Access\AuthorizationException;
Expand Down Expand Up @@ -97,9 +98,6 @@ public function destroy(Team $team): JsonResponse
return $this->restDeleteSuccess();
}

/**
*
*/
/**
* People By Teams Report
*
Expand All @@ -113,4 +111,32 @@ public function peopleByTeamsReport(): JsonResponse

return response()->json(['teams' => PeopleByTeamsReport::execute()]);
}

/**
* Grant or revoke a team membership for a list of callsigns
*
* @return JsonResponse
* @throws AuthorizationException
*/

public function bulkGrantRevoke(): JsonResponse
{
$this->authorize('bulkGrantRevoke', Team::class);
$params = request()->validate([
'callsigns' => 'string|required',
'team_id' => 'integer|required|exists:team,id',
'grant' => 'boolean|required',
'commit' => 'boolean|sometimes',
]);

return response()->json([
'people' => BulkTeamGrantRevoke::execute(
$params['callsigns'],
$params['team_id'],
$params['grant'],
$params['commit'] ?? false
)
]);
}

}
85 changes: 85 additions & 0 deletions app/Lib/BulkTeamGrantRevoke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Lib;

use App\Models\Person;
use App\Models\PersonTeam;

class BulkTeamGrantRevoke
{
/**
* Bulk grant or revoke a single position for a list of callsigns
*
* @param string $callsigns callsign list separated by a newline
* @param int $teamId Team ID to grant or revoke
* @param bool $grant true=grant the position, false=revoke it
* @param bool $commit true=commit the changes, false=verify the callsigns & position changes
* @return array
*/

public static function execute(string $callsigns, int $teamId, bool $grant, bool $commit): array
{
$lines = explode("\n", $callsigns);
$errors = 0;
$results = [];

$reason = $grant ? 'bulk team grant' : 'bulk team revoke';

foreach ($lines as $callsign) {
$normalized = Person::normalizeCallsign($callsign);
if (empty($normalized)) {
continue;
}

$person = Person::where('callsign_normalized', $normalized)->first();

if (!$person) {
$errors++;
$results[] = [
'callsign' => $callsign,
'errors' => 'Callsign not found'
];
continue;
}

$result = [
'id' => $person->id,
'callsign' => $person->callsign,
];

if (in_array($person->status, [...Person::LOCKED_STATUSES, Person::PAST_PROSPECTIVE])) {
$errors++;
$result['errors'] = "Has status [{$person->status}], team cannot be granted thru this interface";
$results[] = $result;
continue;
}

$exists = PersonTeam::haveTeam($person->id, $teamId);

if ($grant) {
if ($exists) {
$result['errors'] = 'Team already granted';
} else {
if ($commit) {
PersonTeam::addPerson($teamId, $person->id, $reason);
}
$result['success'] = true;
}
} else {
if (!$exists) {
$result['errors'] = 'Team already revoked';
} else {
if ($commit) {
PersonTeam::removePerson($teamId, $person->id, $reason);
}
$result['success'] = true;
}
}

$results[] = $result;
}

return $results;
}

}
13 changes: 13 additions & 0 deletions app/Models/PersonTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ public static function haveMVREligibleForPerson(int $personId) : bool
->exists();
}

/**
* Is the person a team member?
*
* @param int $teamId
* @param int $personId
* @return bool
*/

public static function haveTeam(int $teamId, int $personId): bool
{
return self::where('team_id', $teamId)->where('person_id', $personId)->exists();
}

/**
* Find a membership record for the person and team.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function findForQuery(array $query, ?int $personId = null, bool $i

return $rows;
}

/**
* Load the roles associated with the team, and set the pseudo column role_ids
* @return void
Expand Down
11 changes: 11 additions & 0 deletions app/Policies/TeamPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ public function peopleByTeamsReport(Person $user): bool
return $user->hasRole(Role::MANAGE);
}

/**
* Can the user bulk revoke or grant a team membership?
*
* @param Person $user
* @return bool
*/

public function bulkGrantRevoke(Person $user) : bool
{
return $user->hasRole(Role::ADMIN);
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
Route::resource('swag', 'SwagController');

Route::get('team/people-by-teams', 'TeamController@peopleByTeamsReport');
Route::post('team/bulk-grant-revoke', 'TeamController@bulkGrantRevoke');
Route::resource('team', 'TeamController');

Route::get('training-session/sessions', 'TrainingSessionController@sessions');
Expand Down

0 comments on commit 9f2753f

Please sign in to comment.