Skip to content

Commit

Permalink
[ADVAPP-977]: Enhance the user list experience by introducing filters…
Browse files Browse the repository at this point in the history
… for each license type (#1160)

* Add license filter in users table

* remove
  • Loading branch information
amit-canyon authored Dec 10, 2024
1 parent 8646487 commit 46a063a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,45 @@
$usersInRoleA->merge($usersInRoleB)->merge($usersInRoleC)
);
});

it('Filter users based on licenses', function () {
asSuperAdmin();

$usersWithRetentionCrmLicense = User::factory()
->count(3)
->create()
->each(function ($user) {
$user->grantLicense(LicenseType::RetentionCrm);
});

$usersWithRecruitmentCrmLicense = User::factory()
->count(3)
->create()
->each(function ($user) {
$user->grantLicense(LicenseType::RecruitmentCrm);
});

$usersWithConversationalAiLicense = User::factory()
->count(3)
->create()
->each(function ($user) {
$user->grantLicense(LicenseType::ConversationalAi);
});
$usersWithoutLicense = User::factory()
->count(3)
->create();

livewire(ListUsers::class)
->filterTable('licenses', [LicenseType::RetentionCrm->value])
->assertCanSeeTableRecords($usersWithRetentionCrmLicense)
->assertCanNotSeeTableRecords($usersWithRecruitmentCrmLicense->merge($usersWithConversationalAiLicense)->merge($usersWithoutLicense))
->filterTable('licenses', [LicenseType::RecruitmentCrm->value])
->assertCanSeeTableRecords($usersWithRecruitmentCrmLicense)
->assertCanNotSeeTableRecords($usersWithRetentionCrmLicense->merge($usersWithConversationalAiLicense)->merge($usersWithoutLicense))
->filterTable('licenses', [LicenseType::ConversationalAi->value])
->assertCanSeeTableRecords($usersWithConversationalAiLicense)
->assertCanNotSeeTableRecords($usersWithRetentionCrmLicense->merge($usersWithRecruitmentCrmLicense)->merge($usersWithoutLicense))
->filterTable('licenses', ['no_assigned_license'])
->assertCanSeeTableRecords($usersWithoutLicense)
->assertCanNotSeeTableRecords($usersWithRetentionCrmLicense->merge($usersWithRecruitmentCrmLicense)->merge($usersWithConversationalAiLicense));
});
30 changes: 30 additions & 0 deletions app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ function (Builder $query, array $data) {
->multiple()
->searchable()
->preload(),
SelectFilter::make('licenses')
->label('License')
->options(
fn (): array => [
'' => [
'no_assigned_license' => 'No Assigned License',
],
'Licenses' => collect(LicenseType::cases())
->mapWithKeys(fn ($case) => [$case->value => $case->name])
->toArray(),
]
)
->getSearchResultsUsing(fn (string $search): array => ['Licenses' => collect(LicenseType::cases())->filter(fn ($case) => str_contains(strtolower($case->name), strtolower($search)))->mapWithKeys(fn ($case) => [$case->value => $case->name])->toArray()])
->query(
function (Builder $query, array $data) {
if (empty($data['values'])) {
return;
}

$query->when(in_array('no_assigned_license', $data['values']), function ($query) {
$query->whereDoesntHave('licenses');
})
->{in_array('no_assigned_license', $data['values']) ? 'orWhereHas' : 'whereHas'}('licenses', function ($query) use ($data) {
$query->whereIn('type', array_filter($data['values'], fn ($value) => $value !== 'no_assigned_license'));
});
}
)
->multiple()
->searchable()
->preload(),
])
->defaultSort('name', 'asc');
}
Expand Down

0 comments on commit 46a063a

Please sign in to comment.