diff --git a/app/Efz/EfzPendingBlock.php b/app/Efz/EfzPendingBlock.php index 96e28047..0ff77c9c 100644 --- a/app/Efz/EfzPendingBlock.php +++ b/app/Efz/EfzPendingBlock.php @@ -17,6 +17,7 @@ class EfzPendingBlock extends Block return $query->where('efz', '<=', now()->subYears(5)->endOfYear()) ->orWhereNull('efz'); }) + ->whereCurrentGroup() ->orderByRaw('lastname, firstname') ->whereHas('memberships', fn ($builder) => $builder->isLeader()); } diff --git a/app/Member/Member.php b/app/Member/Member.php index 0f44bf27..5e2a804a 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -405,6 +405,21 @@ class Member extends Model return $query; } + /** + * @param Builder $query + * @return Builder + */ + public function scopeWhereCurrentGroup(Builder $query): Builder + { + $group = app(NamiSettings::class)->localGroup(); + + if (!$group) { + return $query; + } + + return $query->where('group_id', $group->id); + } + public static function fromVcard(string $url, string $data): static { $settings = app(NamiSettings::class); diff --git a/app/Member/PsPendingBlock.php b/app/Member/PsPendingBlock.php index b73ca9b8..fc7daf2f 100644 --- a/app/Member/PsPendingBlock.php +++ b/app/Member/PsPendingBlock.php @@ -21,6 +21,7 @@ class PsPendingBlock extends Block ->orWhere(fn ($query) => $query->where('ps_at', '<=', $time)->whereNull('more_ps_at')) ->orWhere(fn ($query) => $query->where('ps_at', '>=', $time)->where('more_ps_at', '<=', $time)); }) + ->whereCurrentGroup() ->orderByRaw('lastname, firstname') ->whereHas('memberships', fn ($builder) => $builder->isLeader()); } diff --git a/app/Setting/NamiSettings.php b/app/Setting/NamiSettings.php index fb7c3cab..2b7913e6 100644 --- a/app/Setting/NamiSettings.php +++ b/app/Setting/NamiSettings.php @@ -2,6 +2,7 @@ namespace App\Setting; +use App\Group; use Spatie\LaravelSettings\Settings; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Nami; @@ -23,4 +24,9 @@ class NamiSettings extends Settings { return Nami::login($this->mglnr, $this->password); } + + public function localGroup(): ?Group + { + return Group::firstWhere('nami_id', $this->default_group_id); + } } diff --git a/tests/Feature/Member/PsPendingBlockTest.php b/tests/Feature/Member/PsPendingBlockTest.php index df9c126c..6fa54e97 100644 --- a/tests/Feature/Member/PsPendingBlockTest.php +++ b/tests/Feature/Member/PsPendingBlockTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Member; +use App\Group; use App\Member\Member; use App\Member\Membership; use App\Member\PsPendingBlock; @@ -14,37 +15,47 @@ class PsPendingBlockTest extends TestCase public function testItRendersContent(): void { + $this->withoutExceptionHandling()->withNamiSettings(12345, 'password', 101); + $group = Group::factory()->inNami(101)->create(); + $noPsAtAll = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Jane', 'lastname' => 'Doe']); $validPs = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Max', 'lastname' => 'Doe', 'ps_at' => now()->subYears(4)]); $validMorePs = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Joe', 'lastname' => 'Doe', 'more_ps_at' => now()->subYears(4)]); $invalidPs = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Mike', 'lastname' => 'Doe', 'ps_at' => now()->subYears(5)]); $invalidMorePs = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Nora', 'lastname' => 'Doe', 'more_ps_at' => now()->subYears(5)]); $invalidPsButValidMorePs = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) ->create(['firstname' => 'Hey', 'lastname' => 'Doe', 'ps_at' => now()->subYears(10), 'more_ps_at' => now()->subYears(3)]); $notALeader = Member::factory() ->defaults() + ->for($group) ->has(Membership::factory()->in('€ Mitglied', 5, 'Wölfling', 8)) ->create(['firstname' => 'Mae', 'lastname' => 'Doe']); @@ -58,4 +69,21 @@ class PsPendingBlockTest extends TestCase ], ], $data); } + + public function testItExcludesForeignGroups(): void + { + $this->withoutExceptionHandling()->withNamiSettings(12345, 'password', 101); + Group::factory()->inNami(101)->create(); + $otherGroup = Group::factory()->inNami(55)->create(); + + Member::factory() + ->defaults() + ->for($otherGroup) + ->has(Membership::factory()->in('€ LeiterIn', 5, 'Wölfling', 8)) + ->create(); + + $data = app(PsPendingBlock::class)->render()['data']; + + $this->assertCount(0, $data['members']); + } } diff --git a/tests/Feature/Membership/EfzPendingBlockTest.php b/tests/Feature/Membership/EfzPendingBlockTest.php index 42147772..5909d61e 100644 --- a/tests/Feature/Membership/EfzPendingBlockTest.php +++ b/tests/Feature/Membership/EfzPendingBlockTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Membership; use App\Efz\EfzPendingBlock; +use App\Group; use App\Member\Member; use App\Member\Membership; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -14,30 +15,38 @@ class EfzPendingBlockTest extends TestCase public function testItDisplaysEfzPending(): void { - $this->withoutExceptionHandling(); + $this->withoutExceptionHandling()->withNamiSettings(12345, 'password', 101); + $group = Group::factory()->inNami(101)->create(); + Member::factory() ->has(Membership::factory()->in('€ LeiterIn', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Max', 'lastname' => 'Muster', 'efz' => now()->subYear()]); Member::factory() ->has(Membership::factory()->in('€ Mitglied', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Jane', 'lastname' => 'Muster', 'efz' => now()->subYear()]); Member::factory() ->has(Membership::factory()->in('€ LeiterIn', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Mae', 'lastname' => 'Muster', 'efz' => now()->subYears(5)->startOfYear()]); Member::factory() ->has(Membership::factory()->in('€ LeiterIn', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Joe', 'lastname' => 'Muster', 'efz' => now()->subYears(5)->endOfYear()]); Member::factory() ->has(Membership::factory()->in('€ LeiterIn', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Moa', 'lastname' => 'Muster', 'efz' => null]); Member::factory() ->has(Membership::factory()->in('€ Mitglied', 1, 'Biber', 2)) ->defaults() + ->for($group) ->create(['firstname' => 'Doe', 'lastname' => 'Muster', 'efz' => now()->subYears(5)]); $data = app(EfzPendingBlock::class)->render()['data']; @@ -46,4 +55,21 @@ class EfzPendingBlockTest extends TestCase 'members' => ['Joe Muster', 'Mae Muster', 'Moa Muster'], ], $data); } + + public function testItExcludesForeignGroups(): void + { + $this->withoutExceptionHandling()->withNamiSettings(12345, 'password', 101); + Group::factory()->inNami(101)->create(); + $otherGroup = Group::factory()->inNami(55)->create(); + + Member::factory() + ->has(Membership::factory()->in('€ LeiterIn', 1, 'Biber', 2)) + ->defaults() + ->for($otherGroup) + ->create(['firstname' => 'Joe', 'lastname' => 'Muster', 'efz' => now()->subYears(5)->endOfYear()]); + + $data = app(EfzPendingBlock::class)->render()['data']; + + $this->assertCount(0, $data['members']); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index d401998d..784e1be6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,21 +29,21 @@ abstract class TestCase extends BaseTestCase Auth::fake(); } - public function loginNami(int $mglnr = 12345, string $password = 'password'): self + public function loginNami(int $mglnr = 12345, string $password = 'password', int $groupId = 55): self { Auth::success($mglnr, $password); - $this->withNamiSettings($mglnr, $password); - Group::factory()->create(['nami_id' => 55]); + $this->withNamiSettings($mglnr, $password, $groupId); + Group::factory()->create(['nami_id' => $groupId]); return $this; } - public function withNamiSettings(int $mglnr = 12345, string $password = 'password'): self + public function withNamiSettings(int $mglnr = 12345, string $password = 'password', int $groupId = 55): self { NamiSettings::fake([ 'mglnr' => $mglnr, 'password' => $password, - 'default_group_id' => 55, + 'default_group_id' => $groupId, ]); return $this;