Skip to content

Commit

Permalink
fix membership recomputation
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree committed Mar 5, 2024
1 parent 6d361bc commit 01ff432
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
60 changes: 30 additions & 30 deletions src/Models/Squads/Squad.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,7 @@ protected static function boot()
! array_key_exists('type', $model->getChanges()))
return;

// kick members which are non longer eligible according to new filters
$model->members->each(function ($user) use ($model) {
if (! $model->isUserEligible($user))
$model->members()->detach($user->id);
});

// invite members which are eligible according to new filters (only for auto squads)
if ($model->type == 'auto') {
$users = User::standard()
->whereDoesntHave('squads', function (Builder $query) use ($model) {
$query->where('id', $model->id);
})->get();

$users->each(function ($user) use ($model) {
if ($model->isUserEligible($user))
$model->members()->save($user);
});
}
$model->recomputeSquadMemberships();
});
}

Expand All @@ -120,24 +103,41 @@ public function isUserEligible(User $user): bool {
}

/**
* Checks all users for eligibility. This function is used in migrations after major changes and bugs in the squad
* eligibility code to ensure that we are in a valid state.
* Checks if the members of this squad are still eligible and if new members have to be added.
* This function is typically called after changes in the squad configuration or in a deferred migration if the behaviour of the squad eligibility logic changes
* @return void
* @throws InvalidFilterException
*/
public static function recheckAllUsers(): void {
Squad::where('type', 'auto')->get()->each(function (Squad $squad) {
User::chunk(100, function ($users) use ($squad) {
$users->each(function (User $user) use ($squad) {
$is_member = $squad->members()->where('id', $user->id)->exists();
private function recomputeSquadMemberships(): void {
// kick members which are non longer eligible according to new filters
$this->members->each(function ($user) {
if (! $this->isUserEligible($user))
$this->members()->detach($user->id);
});

if ($is_member && ! $squad->isUserEligible($user))
$squad->members()->detach($user->id);
// invite members which are eligible according to new filters (only for auto squads)
if ($this->type == 'auto') {
$users = User::standard()
->whereDoesntHave('squads', function (Builder $query) {
$query->where('id', $this->id);
})->get();

if (! $is_member && $squad->isUserEligible($user))
$squad->members()->attach($user->id);
});
$users->each(function ($user) {
if ($this->isUserEligible($user))
$this->members()->save($user);
});
}
}

/**
* Checks all users for eligibility. This function is used in migrations after major changes and bugs in the squad
* eligibility code to ensure that we are in a valid state.
* @return void
* @throws InvalidFilterException
*/
public static function recomputeAllSquadMemberships(): void {
Squad::where('type', 'auto')->get()->each(function (Squad $squad) {
$squad->recomputeSquadMemberships();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UpgradeSquadsMaj4Min4Hf2 extends Migration
public function up()
{
DeferredMigration::schedule(function (){
Squad::recheckAllUsers();
Squad::recomputeAllSquadMemberships();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function up()

// since the squad filter change with this migration, we have to recompute the eligibility of everyone
DeferredMigration::schedule(function (){
Squad::recheckAllUsers();
Squad::recomputeAllSquadMemberships();
});
}

Expand Down

0 comments on commit 01ff432

Please sign in to comment.