baseline
This commit is contained in:
161
lib/Service/DepartmentService.php
Normal file
161
lib/Service/DepartmentService.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Service;
|
||||
|
||||
use MintyPHP\Repository\DepartmentRepository;
|
||||
use MintyPHP\Repository\TenantDepartmentRepository;
|
||||
use MintyPHP\Repository\UserDepartmentRepository;
|
||||
use MintyPHP\Service\SettingService;
|
||||
use MintyPHP\Service\TenantScopeService;
|
||||
|
||||
class DepartmentService
|
||||
{
|
||||
public static function list(): array
|
||||
{
|
||||
return DepartmentRepository::list();
|
||||
}
|
||||
|
||||
public static function listPaged(array $options): array
|
||||
{
|
||||
if (!empty($options['tenantUserId'])) {
|
||||
$tenantUserId = (int) $options['tenantUserId'];
|
||||
if ($tenantUserId > 0 && TenantScopeService::hasGlobalAccess($tenantUserId)) {
|
||||
unset($options['tenantUserId'], $options['tenantIds']);
|
||||
}
|
||||
}
|
||||
return DepartmentRepository::listPaged($options);
|
||||
}
|
||||
|
||||
public static function listByTenantIds(array $tenantIds): array
|
||||
{
|
||||
return DepartmentRepository::listByTenantIds($tenantIds);
|
||||
}
|
||||
|
||||
public static function listByIds(array $departmentIds): array
|
||||
{
|
||||
return DepartmentRepository::listByIds($departmentIds);
|
||||
}
|
||||
|
||||
public static function listForUserAssignments(array $tenantIds, array $selectedDepartmentIds): array
|
||||
{
|
||||
$departments = $tenantIds ? self::listByTenantIds($tenantIds) : self::list();
|
||||
$extraDepartments = $selectedDepartmentIds ? self::listByIds($selectedDepartmentIds) : [];
|
||||
if (!$extraDepartments) {
|
||||
return $departments;
|
||||
}
|
||||
$departmentMap = [];
|
||||
foreach ($departments as $department) {
|
||||
if (isset($department['id'])) {
|
||||
$departmentMap[(int) $department['id']] = $department;
|
||||
}
|
||||
}
|
||||
foreach ($extraDepartments as $department) {
|
||||
if (isset($department['id'])) {
|
||||
$departmentMap[(int) $department['id']] = $department;
|
||||
}
|
||||
}
|
||||
return array_values($departmentMap);
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
{
|
||||
return DepartmentRepository::findByUuid($uuid);
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
{
|
||||
return DepartmentRepository::find($id);
|
||||
}
|
||||
|
||||
public static function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$errors = self::validateBase($form);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$createdId = DepartmentRepository::create([
|
||||
'description' => $form['description'],
|
||||
'created_by' => $currentUserId > 0 ? $currentUserId : null,
|
||||
]);
|
||||
|
||||
if (!$createdId) {
|
||||
return ['ok' => false, 'errors' => [t('Department can not be created')], 'form' => $form];
|
||||
}
|
||||
|
||||
$createdDepartment = DepartmentRepository::find((int) $createdId);
|
||||
$uuid = $createdDepartment['uuid'] ?? null;
|
||||
if (!empty($input['is_default']) && $createdId) {
|
||||
SettingService::setDefaultDepartmentId((int) $createdId);
|
||||
}
|
||||
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
public static function updateFromAdmin(int $departmentId, array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$errors = self::validateBase($form);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$updated = DepartmentRepository::update($departmentId, [
|
||||
'description' => $form['description'],
|
||||
'modified_by' => $currentUserId > 0 ? $currentUserId : null,
|
||||
]);
|
||||
|
||||
if (!$updated) {
|
||||
return ['ok' => false, 'errors' => [t('Department can not be updated')], 'form' => $form];
|
||||
}
|
||||
|
||||
return ['ok' => true, 'form' => $form];
|
||||
}
|
||||
|
||||
public static function syncTenants(int $departmentId, array $tenantIds): int
|
||||
{
|
||||
$updated = TenantDepartmentRepository::replaceForDepartment($departmentId, $tenantIds);
|
||||
if (!$updated) {
|
||||
return -1;
|
||||
}
|
||||
return UserDepartmentRepository::removeInvalidForDepartment($departmentId);
|
||||
}
|
||||
|
||||
public static function deleteByUuid(string $uuid): array
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if ($uuid === '') {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$department = DepartmentRepository::findByUuid($uuid);
|
||||
if (!$department || !isset($department['id'])) {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$deleted = DepartmentRepository::delete((int) $department['id']);
|
||||
if (!$deleted) {
|
||||
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||||
}
|
||||
|
||||
return ['ok' => true, 'department' => $department];
|
||||
}
|
||||
|
||||
private static function sanitizeBase(array $input): array
|
||||
{
|
||||
return [
|
||||
'description' => trim((string) ($input['description'] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
private static function validateBase(array $form): array
|
||||
{
|
||||
$errors = [];
|
||||
if ($form['description'] === '') {
|
||||
$errors[] = t('Description cannot be empty');
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user