Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/tailwindcss-3.4.15
Browse files Browse the repository at this point in the history
  • Loading branch information
detheridge02 authored Nov 19, 2024
2 parents 4448ff3 + fa038ad commit 2824bba
Show file tree
Hide file tree
Showing 29 changed files with 403 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function handle()
}

if ($this->argument('account')) {
$activeWaitingLists = $activeWaitingLists->filter(function ($waitingList) {
return $waitingList->accounts->contains($this->argument('account'));
$activeWaitingLists = $activeWaitingLists->filter(function (WaitingList $waitingList) {
return $waitingList->includesAccount($this->argument('account'));
});

if ($activeWaitingLists->isEmpty()) {
Expand All @@ -56,8 +56,8 @@ public function handle()
}

foreach ($activeWaitingLists as $waitingList) {
foreach ($waitingList->accounts as $account) {
UpdateAccountWaitingListEligibility::dispatch($account);
foreach ($waitingList->waitingListAccounts as $waitingListAccount) {
UpdateAccountWaitingListEligibility::dispatch($waitingListAccount->account);
}
}

Expand Down
8 changes: 7 additions & 1 deletion app/Events/Training/AccountAddedToWaitingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\AccountCentricEvent;
use App\Models\Mship\Account;
use App\Models\Training\WaitingList;
use App\Models\Training\WaitingList\WaitingListAccount;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
Expand All @@ -16,7 +17,12 @@ class AccountAddedToWaitingList implements AccountCentricEvent
/**
* Create a new event instance.
*/
public function __construct(public Account $account, public WaitingList $waitingList, public Account $staffAccount) {}
public function __construct(
public Account $account,
public WaitingList $waitingList,
public Account $staffAccount,
public WaitingListAccount $waitingListAccount,
) {}

public function getAccount(): Account
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Filament\Resources\WaitingListResource\Widgets\IndividualWaitingListOverview;
use App\Models\Atc\PositionGroup;
use App\Models\Mship\Account;
use App\Models\Training\WaitingList;
use App\Models\Training\WaitingList\WaitingListFlag;
use Carbon\Carbon;
use Filament\Actions;
Expand All @@ -16,6 +17,9 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;

/**
* @property WaitingList $record
*/
class ViewWaitingList extends ViewRecord
{
protected static string $resource = WaitingListResource::class;
Expand Down Expand Up @@ -45,7 +49,7 @@ protected function getHeaderActions(): array
TextInput::make('account_id')
->label('Account CID')
->rule(fn () => function ($attribute, $value, $fail) {
if ($this->record->accounts->contains('id', $value)) {
if ($this->record->includesAccount($value)) {
$fail('This account is already in this waiting list.');
}
})
Expand Down
28 changes: 19 additions & 9 deletions app/Http/Controllers/Mship/WaitingLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@

use App\Http\Controllers\BaseController;
use App\Models\Training\WaitingList;
use App\Models\Training\WaitingList\WaitingListAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class WaitingLists extends BaseController
{
public function index(Request $request)
{
$atcWaitingLists = $request->user()->currentWaitingLists()
->withPivot([
'created_at',
])->where('department', WaitingList::ATC_DEPARTMENT)->get();
$pilotWaitingLists = $request->user()->currentWaitingLists()->withPivot([
'created_at',
])->where('department', WaitingList::PILOT_DEPARTMENT)->get();
/** @var Collection<WaitingListAccount> $waitingListAccounts */
$waitingListAccounts = $request->user()->waitingListAccounts;

$atcWaitingListAccounts = collect();
$pilotWaitingListAccounts = collect();

foreach ($waitingListAccounts as $waitingListAccount) {
if ($waitingListAccount->waitingList->department == WaitingList::ATC_DEPARTMENT) {
$atcWaitingListAccounts->push($waitingListAccount);
}

if ($waitingListAccount->waitingList->department == WaitingList::PILOT_DEPARTMENT) {
$pilotWaitingListAccounts->push($waitingListAccount);
}
}

return view('mship.waiting-lists.index', [
'isOBS' => $request->user()->qualification_atc->is_o_b_s,
'atcLists' => $atcWaitingLists,
'pilotLists' => $pilotWaitingLists,
'atcWaitingListAccounts' => $atcWaitingListAccounts,
'pilotWaitingListAccounts' => $pilotWaitingListAccounts,
]);
}
}
2 changes: 1 addition & 1 deletion app/Jobs/Training/UpdateAccountWaitingListEligibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle()
{
$service = new CheckWaitingListFlags($this->account);

$accountWaitingLists = $this->account->currentWaitingLists;
$accountWaitingLists = $this->account->currentWaitingLists();

foreach ($accountWaitingLists as $waitingList) {
WriteWaitingListFlagSummary::handle($waitingList, $service);
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Training/WaitingList/AssignFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()
public function handle(AccountAddedToWaitingList $event)
{
/** @var WaitingListAccount $waitingList */
$waitingListAccount = $event->waitingList->accounts()->findOrFail($event->account->id)->pivot;
$waitingListAccount = $event->waitingListAccount;

$flags = $event->waitingList->flags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public function handle(AccountAltered $event)
return;
}

if ($account->currentWaitingLists->count() == 0) {
if ($account->currentWaitingLists()->count() == 0) {
Log::debug("Inactive account {$account->id} is not in a waiting list, skipping");

return;
}

foreach ($account->currentWaitingLists as $waitingList) {
foreach ($account->currentWaitingLists() as $waitingList) {
Log::info("Inactive account {$account->id} is in waiting list {$waitingList->id} - removing from waiting list");

$waitingList->removeFromWaitingList($account);
}

Log::info("Account {$account->id} is in waiting lists {$account->currentWaitingLists->pluck('id')->join(', ')}, with inactive account state - (fake) notifying account");
Log::info("Account {$account->id} is in waiting lists {$account->currentWaitingLists()->pluck('id')->join(', ')}, with inactive account state - (fake) notifying account");

$account->notify(new RemovedFromWaitingListInactiveAccount($account->currentWaitingLists));
$account->notify(new RemovedFromWaitingListInactiveAccount($account->currentWaitingLists()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Events\Mship\AccountAltered;
use App\Models\Mship\State;
use App\Models\Training\WaitingList;
use App\Notifications\Training\RemovedFromWaitingListNonHomeMember;
use Illuminate\Support\Facades\Log;

Expand All @@ -20,7 +21,7 @@ public function handle(AccountAltered $event)
// ensure we have the latest data
$account = $event->account->refresh();

$accountsWaitingList = $account->currentWaitingLists->filter(function ($waitingList) {
$accountsWaitingList = $account->currentWaitingLists()->filter(function (WaitingList $waitingList) {
return $waitingList->home_members_only;
});

Expand Down
7 changes: 5 additions & 2 deletions app/Models/Mship/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
* @property-read int|null $permissions_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Smartcars\Pirep> $pireps
* @property-read int|null $pireps_count
* @property-read \App\Models\Training\WaitingList\WaitingListAccount $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Mship\Qualification> $qualifications
* @property-read int|null $qualifications_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Sys\Notification> $readSystemNotifications
Expand Down Expand Up @@ -182,9 +183,9 @@
* @method static \Illuminate\Database\Eloquent\Builder|Account newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Account notBanned()
* @method static \Illuminate\Database\Eloquent\Builder|Account onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Account permission($permissions)
* @method static \Illuminate\Database\Eloquent\Builder|Account permission($permissions, $without = false)
* @method static \Illuminate\Database\Eloquent\Builder|Account query()
* @method static \Illuminate\Database\Eloquent\Builder|Account role($roles, $guard = null)
* @method static \Illuminate\Database\Eloquent\Builder|Account role($roles, $guard = null, $without = false)
* @method static \Illuminate\Database\Eloquent\Builder|Account whereAge($value)
* @method static \Illuminate\Database\Eloquent\Builder|Account whereCertCheckedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Account whereCreatedAt($value)
Expand Down Expand Up @@ -214,6 +215,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|Account whereVatsimRefreshToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|Account whereVatsimTokenExpires($value)
* @method static \Illuminate\Database\Eloquent\Builder|Account withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Account withoutPermission($permissions)
* @method static \Illuminate\Database\Eloquent\Builder|Account withoutRole($roles, $guard = null)
* @method static \Illuminate\Database\Eloquent\Builder|Account withoutTrashed()
*
* @mixin \Eloquent
Expand Down
45 changes: 37 additions & 8 deletions app/Models/Mship/Concerns/HasWaitingLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,50 @@

use App\Models\Training\WaitingList;
use App\Models\Training\WaitingList\WaitingListAccount;
use Illuminate\Support\Collection;

trait HasWaitingLists
{
public function waitingListAccounts()
{
return $this->hasMany(WaitingListAccount::class)->with('waitingList');
}

/**
* Get *all* waiting lists for this account, including ones the account has been removed from
*
* @return Collection<WaitingList>
*/
public function waitingLists()
{
return $this->belongsToMany(
WaitingList::class,
'training_waiting_list_account',
'account_id',
'list_id'
)->using(WaitingListAccount::class)->withPivot(['id', 'deleted_at']);
return $this->waitingListAccounts()
->withTrashed()
->get()
->mapWithKeys(function (WaitingListAccount $waitingListAccount) {
return [$waitingListAccount->waitingList->id => $waitingListAccount->waitingList];
})
->values();
}

public function currentWaitingLists()
/**
* Get all "live" waiting lists for this account
*
* @return Collection<WaitingList>
*/
public function currentWaitingLists(): Collection
{
return $this->waitingLists()->wherePivot('deleted_at', null);
// waiting list accounts soft delete so this will exclude them
$waitingListAccounts = $this->waitingListAccounts()
->with('waitingList')
->get();

// @fixme maybe replace with mapWithKeys
$waitingLists = collect();
foreach ($waitingListAccounts as $waitingListAccount) {
$waitingList = $waitingListAccount->waitingList;
$waitingLists->put($waitingList->id, $waitingList);
}

return $waitingLists->values();
}
}
61 changes: 36 additions & 25 deletions app/Models/Training/WaitingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public function staff()
*/
public function accounts(): BelongsToMany
{
// this aint gonna work because the waitinglistaccount is no longer a pivot!

return $this->belongsToMany(
Account::class,
'training_waiting_list_account',
Expand Down Expand Up @@ -149,21 +151,7 @@ public function flags()
}

/**
* Get the position of an account in the eligible waiting list.
*
* @return int|null
*/
public function accountPosition(Account $account)
{
$key = $this->accounts->search(function ($accountItem) use ($account) {
return $accountItem->id == $account->id;
});

return ($key !== false) ? $key + 1 : null;
}

/**
* Alternative to accountPosition for use with filament, beware N+1 issues
* Find the position of a WaitingListAccount on this waiting list.
*/
public function positionOf(WaitingListAccount $waitingListAccount): ?int
{
Expand All @@ -181,8 +169,8 @@ public function addFlag(WaitingListFlag $flag)
{
$savedFlag = $this->flags()->save($flag);

$this->accounts()->each(function ($account) use ($flag) {
$account->pivot->flags()->attach($flag);
$this->waitingListAccounts()->each(function (WaitingListAccount $listAccount) use ($flag) {
$listAccount->flags()->attach($flag);
});

event(new FlagAddedToWaitingList($this));
Expand All @@ -202,21 +190,44 @@ public function removeFlag(WaitingListFlag $flag)

/**
* Add an Account to a waiting list.
*
* @fixme don't use pivot here, add the WaitingListAccount explicitly
*/
public function addToWaitingList(Account $account, Account $staffAccount, ?Carbon $createdAt = null)
public function addToWaitingList(Account $account, Account $staffAccount, ?Carbon $createdAt = null): WaitingListAccount
{
$timestamp = $createdAt != null ? $createdAt : Carbon::now();
$this->accounts()->attach($account, ['added_by' => $staffAccount->id]);

$waitingListAccount = new WaitingListAccount;
$waitingListAccount->account_id = $account->id;
$waitingListAccount->added_by = $staffAccount->id;

$waitingListAccount = $this->waitingListAccounts()->save($waitingListAccount);

// the following code is required as the timestamp for created_at gets overridden during the creation
// process, despite being disabled on the pivot!!
$pivot = $this->accounts()->find($account->id)->pivot;
$pivot->created_at = $timestamp;
$pivot->save();
$waitingListAccount->created_at = $timestamp;
$waitingListAccount->save();

event(new AccountAddedToWaitingList($account, $this->fresh(), $staffAccount, $waitingListAccount));

return $waitingListAccount;
}

public function includesAccount(int|Account $accountId): bool
{
if ($accountId instanceof Account) {
$accountId = $accountId->id;
}

return $this->waitingListAccounts()->where('account_id', $accountId)->exists();
}

public function findWaitingListAccount(int|Account $accountId): ?WaitingListAccount
{
if ($accountId instanceof Account) {
$accountId = $accountId->id;
}

event(new AccountAddedToWaitingList($account, $this->fresh(), $staffAccount));
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->waitingListAccounts()->where('account_id', $accountId)->first();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Training/WaitingList/WaitingListAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Training\WaitingList;

use App\Models\Cts\TheoryResult;
use App\Models\Model;
use App\Models\Mship\Account;
use App\Models\Training\WaitingList;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -22,6 +23,7 @@
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property array|null $flags_status_summary
* @property-read Account|null $account
* @property-read \App\Models\Training\WaitingList\WaitingListAccountFlag $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Training\WaitingList\WaitingListFlag> $flags
* @property-read int|null $flags_count
* @property-read mixed $atc_hour_check
Expand All @@ -47,7 +49,7 @@
*
* @mixin \Eloquent
*/
class WaitingListAccount extends Pivot
class WaitingListAccount extends Model
{
use SoftDeletes;

Expand Down
Loading

0 comments on commit 2824bba

Please sign in to comment.