Skip to content

Commit

Permalink
Revert "fix: correctly determine if account can control when position…
Browse files Browse the repository at this point in the history
… in multiple position groups"

This reverts commit 44720c7.
  • Loading branch information
AxonC committed Mar 3, 2024
1 parent 44720c7 commit 61ec8a0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 398 deletions.
2 changes: 1 addition & 1 deletion app/Models/Atc/PositionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function conditions()

public function positions()
{
return $this->belongsToMany(Position::class, 'position_group_positions', 'position_group_id', 'position_id')->using(PositionGroupPosition::class);
return $this->belongsToMany(Position::class, 'position_group_positions', 'position_group_id', 'position_id');
}

public function endorsement()
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Atc/PositionGroupPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Models\Atc;

use Illuminate\Database\Eloquent\Relations\Pivot;
use App\Models\Model;

class PositionGroupPosition extends Pivot
class PositionGroupPosition extends Model
{
public function positionGroup()
{
Expand Down
42 changes: 5 additions & 37 deletions app/Models/Roster.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Atc\PositionGroup;
use App\Models\Atc\PositionGroupPosition;
use App\Models\Mship\Account;
use App\Models\Mship\Account\Endorsement;
use App\Models\Mship\Qualification;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -68,20 +67,11 @@ public function accountCanControl(Position $position)
return false;
}

$assignedPositionGroupsWithPosition = Endorsement::where('account_id', $this->account->id)
->whereHasMorph('endorsable', PositionGroup::class, fn ($query) => $query->whereHas('positions', fn ($query) => $query->where('positions.id', $position->id)))
->get()
->map(fn ($endorsement) => $endorsement->endorsable);

$unassignedPositionGroupsWithPosition = PositionGroup::whereHas('positions', fn ($query) => $query->where('positions.id', $position->id))
->whereDoesntHave('membershipEndorsement', fn ($query) => $query->where('account_id', $this->account->id))
->get();

$checkPositionForPositionGroup = function (PositionGroupPosition $positionGroupPosition) {
// If the position is part of a group,
// a) are they a home member with a rating above the position's maximum?
// b) are they a visiting or transferring member with an endorsement up to a rating above the position group's maximum?
// c) are they endorsed on this specific position group?
// If the position is part of a group,
// a) are they a home member with a rating above the position's maximum?
// b) are they a visiting or transferring member with an endorsement up to a rating above the position group's maximum?
// c) are they endorsed on this specific position group?
if ($positionGroupPosition = PositionGroupPosition::where('position_id', $position->id)->first()) {
$isEntitledByHomeMemberRating = isset($positionGroupPosition->positionGroup?->maximumAtcQualification)
&& $this->account->hasState('DIVISION') &&
$this->account->qualification_atc->vatsim > $positionGroupPosition->positionGroup?->maximumAtcQualification?->vatsim;
Expand All @@ -108,28 +98,6 @@ public function accountCanControl(Position $position)
->exists();

return $isEntitledByHomeMemberRating || $isEndorsedToRating || $hasEndorsementForPositionGroup;
};

/** If there are a PositionGroup(s) which contain the specified position
* perform a series of checks to determine if the account is entitled to
* control the position */
if ($assignedPositionGroupsWithPosition->count() > 0) {
return $assignedPositionGroupsWithPosition->some(fn ($positionGroup) => $checkPositionForPositionGroup($positionGroup->positions->where('id', $position->id)->first()->pivot));
}

/** Check any unassigned position groups have a maximum atc qualification
* if so, check if the account has a rating above the maximum specified
* qualification and if so, they are entitled to control even if the
* position group hasn't been endorsed to that member. */
$unassignedPositionGroupsWithPositionWithMaxRating = $unassignedPositionGroupsWithPosition->filter(fn ($positionGroup) => isset($positionGroup->maximumAtcQualification));
if ($unassignedPositionGroupsWithPositionWithMaxRating->count() > 0) {
return $unassignedPositionGroupsWithPosition->some(
function (PositionGroup $positionGroup) use ($position) {
$positionGroupPosition = $positionGroup->positions->where('id', $position->id)->first()->pivot;

return $this->account->qualification_atc->vatsim > $positionGroupPosition->positionGroup->maximumAtcQualification->vatsim;
}
);
}

// If the position is above their rating, do they
Expand Down
Loading

0 comments on commit 61ec8a0

Please sign in to comment.