dstpabuaran.com/app/Services/Admin/Settings/RoleService.php
Yoga Pangestu 0023309a8f Refactor services to improve role checks and streamline data retrieval
- Updated CustomerService to simplify getAll method.
- Refactored ProductService to utilize HasRoleChecks trait and improved role verification logic.
- Enhanced ProductVariantService with new methods for fetching data for restocking and transactions.
- Cleaned up RawMaterialService by removing unused methods and improving data retrieval.
- Adjusted SupplierService to streamline getAll method.
- Refactored RoleService to use Spatie's Role model and improved role filtering logic.
- Updated NotificationService to handle role labels more effectively.
- Improved StockMutationService by removing redundant paginated method.
- Cleaned up various frontend components to directly accept necessary props instead of nested data objects.
- Updated tests to reflect changes in service method names and ensure proper notification handling.
2026-08-09 11:33:25 +07:00

78 lines
2.3 KiB
PHP

<?php
namespace App\Services\Admin\Settings;
use App\Concerns\HasRoleChecks;
use App\Enums\Role;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role as SpatieRole;
class RoleService
{
use HasRoleChecks;
public function paginated(int $perPage = 25, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
{
return SpatieRole::query()
->select(['id', 'name'])
->withCount('permissions')
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
->orderBy($sort, $direction)
->paginate($perPage);
}
public function store(array $data): Role
{
return DB::transaction(function () use ($data) {
$role = SpatieRole::create(['name' => $data['name']]);
$role->syncPermissions($data['permissions']);
return $role;
});
}
public function update(Role $role, array $data): Role
{
DB::transaction(function () use ($role, $data) {
$role->update(['name' => $data['name']]);
$role->syncPermissions($data['permissions']);
});
return $role->fresh('permissions');
}
public function destroy(Role $role): bool
{
return $role->delete();
}
public function getPermissionsByModule(): array
{
return Permission::all()
->groupBy(fn ($p) => explode('.', $p->name)[0])
->map(fn ($group) => $group->pluck('name')->map(fn ($name) => explode('.', $name, 2)[1])->values()->toArray())
->toArray();
}
public function getForEmployee(): Collection
{
$query = SpatieRole::where('name', '!=', Role::DEVELOPER->value);
$user = auth()->user();
if (self::hasAnyRole([Role::ADMIN_TOKO, Role::DIREKTUR])) {
$query->where('name', '!=', Role::ADMIN_BAHAN_BAKU->value);
}
if (! self::hasAnyRole([Role::DEVELOPER, Role::OWNER, Role::DIREKTUR, Role::ADMIN_TOKO])) {
$userRoles = $user->roles->pluck('name');
$query->whereIn('name', $userRoles);
}
return $query->get(['id', 'name']);
}
}