major update
This commit is contained in:
@@ -3,40 +3,41 @@
|
||||
namespace MintyPHP\Service\Org;
|
||||
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Service\Audit\SystemAuditService;
|
||||
use MintyPHP\Service\Directory\DirectoryScopeGateway;
|
||||
use MintyPHP\Service\Directory\DirectorySettingsGateway;
|
||||
use MintyPHP\Service\Settings\SettingServicesFactory;
|
||||
use MintyPHP\Service\User\UserServicesFactory;
|
||||
|
||||
class DepartmentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?DepartmentRepository $departmentRepository = null,
|
||||
private readonly ?UserServicesFactory $userServicesFactory = null,
|
||||
private readonly ?DirectorySettingsGateway $settingsGateway = null,
|
||||
private readonly ?DirectoryScopeGateway $scopeGateway = null
|
||||
private readonly UserServicesFactory $userServicesFactory,
|
||||
private readonly DepartmentRepository $departmentRepository,
|
||||
private readonly DirectorySettingsGateway $settingsGateway,
|
||||
private readonly DirectoryScopeGateway $scopeGateway,
|
||||
private readonly SystemAuditService $systemAuditService
|
||||
) {
|
||||
}
|
||||
|
||||
public function list(): array
|
||||
{
|
||||
return $this->departmentRepository()->list();
|
||||
return $this->departmentRepository->list();
|
||||
}
|
||||
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
if (!empty($options['tenantUserId'])) {
|
||||
$tenantUserId = (int) $options['tenantUserId'];
|
||||
if ($tenantUserId > 0 && $this->scopeGateway()->hasGlobalAccess($tenantUserId)) {
|
||||
if ($tenantUserId > 0 && $this->scopeGateway->hasGlobalAccess($tenantUserId)) {
|
||||
unset($options['tenantUserId'], $options['tenantIds']);
|
||||
}
|
||||
}
|
||||
return $this->departmentRepository()->listPaged($options);
|
||||
return $this->departmentRepository->listPaged($options);
|
||||
}
|
||||
|
||||
public function listByTenantIds(array $tenantIds): array
|
||||
{
|
||||
return $this->departmentRepository()->listByTenantIds($tenantIds);
|
||||
return $this->departmentRepository->listByTenantIds($tenantIds);
|
||||
}
|
||||
|
||||
public function groupActiveByTenantIds(array $tenantIds): array
|
||||
@@ -74,7 +75,7 @@ class DepartmentService
|
||||
|
||||
public function listByIds(array $departmentIds): array
|
||||
{
|
||||
return $this->departmentRepository()->listByIds($departmentIds);
|
||||
return $this->departmentRepository->listByIds($departmentIds);
|
||||
}
|
||||
|
||||
public function listForUserAssignments(array $tenantIds, array $selectedDepartmentIds): array
|
||||
@@ -100,12 +101,12 @@ class DepartmentService
|
||||
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
return $this->departmentRepository()->findByUuid($uuid);
|
||||
return $this->departmentRepository->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
return $this->departmentRepository()->find($id);
|
||||
return $this->departmentRepository->find($id);
|
||||
}
|
||||
|
||||
public function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
@@ -118,7 +119,7 @@ class DepartmentService
|
||||
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
|
||||
}
|
||||
|
||||
$createdId = $this->departmentRepository()->create([
|
||||
$createdId = $this->departmentRepository->create([
|
||||
'description' => $form['description'],
|
||||
'tenant_id' => $form['tenant_id'],
|
||||
'code' => $form['code'] ?: null,
|
||||
@@ -131,11 +132,20 @@ class DepartmentService
|
||||
return ['ok' => false, 'errors' => [t('Department can not be created')], 'form' => $form];
|
||||
}
|
||||
|
||||
$createdDepartment = $this->departmentRepository()->find((int) $createdId);
|
||||
$createdDepartment = $this->departmentRepository->find((int) $createdId);
|
||||
$uuid = $createdDepartment['uuid'] ?? null;
|
||||
if (!empty($input['is_default'])) {
|
||||
$this->settingsGateway()->setDefaultDepartmentId((int) $createdId);
|
||||
$this->settingsGateway->setDefaultDepartmentId((int) $createdId);
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.departments.create', 'success', [
|
||||
'actor_user_id' => $currentUserId > 0 ? $currentUserId : null,
|
||||
'target_type' => 'department',
|
||||
'target_id' => (int) $createdId,
|
||||
'target_uuid' => is_string($uuid) ? $uuid : '',
|
||||
'metadata' => ['tenant_id' => $form['tenant_id']],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form, 'warnings' => $warnings, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
@@ -149,7 +159,9 @@ class DepartmentService
|
||||
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
|
||||
}
|
||||
|
||||
$updated = $this->departmentRepository()->update($departmentId, [
|
||||
$existingBefore = $this->departmentRepository->find($departmentId) ?? [];
|
||||
|
||||
$updated = $this->departmentRepository->update($departmentId, [
|
||||
'description' => $form['description'],
|
||||
'tenant_id' => $form['tenant_id'],
|
||||
'code' => $form['code'] ?: null,
|
||||
@@ -162,12 +174,27 @@ class DepartmentService
|
||||
return ['ok' => false, 'errors' => [t('Department can not be updated')], 'form' => $form];
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.departments.update', 'success', [
|
||||
'actor_user_id' => $currentUserId > 0 ? $currentUserId : null,
|
||||
'target_type' => 'department',
|
||||
'target_id' => $departmentId,
|
||||
'target_uuid' => (string) ($existingBefore['uuid'] ?? ''),
|
||||
'before' => [
|
||||
'active' => $existingBefore['active'] ?? null,
|
||||
'tenant_id' => $existingBefore['tenant_id'] ?? null,
|
||||
],
|
||||
'after' => [
|
||||
'active' => $form['active'],
|
||||
'tenant_id' => $form['tenant_id'],
|
||||
],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form, 'warnings' => $warnings];
|
||||
}
|
||||
|
||||
public function syncTenant(int $departmentId, int $tenantId): int
|
||||
{
|
||||
$updated = $this->departmentRepository()->setTenant($departmentId, $tenantId);
|
||||
$updated = $this->departmentRepository->setTenant($departmentId, $tenantId);
|
||||
if (!$updated) {
|
||||
return -1;
|
||||
}
|
||||
@@ -185,7 +212,7 @@ class DepartmentService
|
||||
|
||||
public function cleanupUserAssignments(int $departmentId): int
|
||||
{
|
||||
return $this->userServicesFactory()
|
||||
return $this->userServicesFactory
|
||||
->createUserDepartmentRepository()
|
||||
->removeInvalidForDepartment($departmentId);
|
||||
}
|
||||
@@ -197,16 +224,22 @@ class DepartmentService
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$department = $this->departmentRepository()->findByUuid($uuid);
|
||||
$department = $this->departmentRepository->findByUuid($uuid);
|
||||
if (!$department || !isset($department['id'])) {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$deleted = $this->departmentRepository()->delete((int) $department['id']);
|
||||
$deleted = $this->departmentRepository->delete((int) $department['id']);
|
||||
if (!$deleted) {
|
||||
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.departments.delete', 'success', [
|
||||
'target_type' => 'department',
|
||||
'target_id' => (int) $department['id'],
|
||||
'target_uuid' => (string) ($department['uuid'] ?? ''),
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'department' => $department];
|
||||
}
|
||||
|
||||
@@ -237,7 +270,7 @@ class DepartmentService
|
||||
{
|
||||
$warnings = [];
|
||||
$code = $form['code'] ?? '';
|
||||
if ($code !== '' && $this->departmentRepository()->existsByCode($code, $excludeId)) {
|
||||
if ($code !== '' && $this->departmentRepository->existsByCode($code, $excludeId)) {
|
||||
$warnings[] = t('Department code already exists');
|
||||
}
|
||||
return $warnings;
|
||||
@@ -252,29 +285,4 @@ class DepartmentService
|
||||
return 1;
|
||||
}
|
||||
|
||||
private function departmentRepository(): DepartmentRepository
|
||||
{
|
||||
return $this->departmentRepository ?? new DepartmentRepository();
|
||||
}
|
||||
|
||||
private function userServicesFactory(): UserServicesFactory
|
||||
{
|
||||
return $this->userServicesFactory ?? new UserServicesFactory();
|
||||
}
|
||||
|
||||
private function settingsGateway(): DirectorySettingsGateway
|
||||
{
|
||||
if ($this->settingsGateway instanceof DirectorySettingsGateway) {
|
||||
return $this->settingsGateway;
|
||||
}
|
||||
|
||||
return new DirectorySettingsGateway(
|
||||
(new SettingServicesFactory())->createSettingGateway()
|
||||
);
|
||||
}
|
||||
|
||||
private function scopeGateway(): DirectoryScopeGateway
|
||||
{
|
||||
return $this->scopeGateway ?? new DirectoryScopeGateway();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user