forked from admin/services_core
60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Tenant;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
|
|
class TenantService
|
|
{
|
|
public function list(array $filters = [], ?User $actor = null)
|
|
{
|
|
return Tenant::query()
|
|
->when(
|
|
$actor && ! $actor->isMasterAdmin(),
|
|
fn ($q) => $q->whereIn(
|
|
'id',
|
|
$actor->userTenants()->select('tenant_id')
|
|
)
|
|
)
|
|
->when(
|
|
! empty($filters['search']),
|
|
fn ($q) => $q->where(function ($x) use ($filters) {
|
|
$x->where('tenant_code', 'ILIKE', "%{$filters['search']}%")
|
|
->orWhere('tenant_name', 'ILIKE', "%{$filters['search']}%")
|
|
->orWhere('email', 'ILIKE', "%{$filters['search']}%");
|
|
})
|
|
)
|
|
->when(
|
|
! empty($filters['status']),
|
|
fn ($q) => $q->where('status', $filters['status'])
|
|
)
|
|
->orderBy(
|
|
$filters['sort_by'] ?? 'id',
|
|
$filters['sort_direction'] ?? 'desc'
|
|
)
|
|
->paginate($filters['per_page'] ?? 10);
|
|
}
|
|
|
|
public function create(array $data): Tenant
|
|
{
|
|
if (! array_key_exists('status', $data) || empty($data['status'])) {
|
|
$data['status'] = 'active';
|
|
}
|
|
|
|
return Tenant::create($data);
|
|
}
|
|
|
|
public function update(Tenant $tenant, array $data): Tenant
|
|
{
|
|
$tenant->update($data);
|
|
|
|
return $tenant->fresh();
|
|
}
|
|
|
|
public function delete(Tenant $tenant): bool
|
|
{
|
|
return $tenant->delete();
|
|
}
|
|
}
|