1
0
This commit is contained in:
2026-02-04 23:31:53 +01:00
commit cd59ccd99b
2401 changed files with 56808 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace MintyPHP\Repository;
use MintyPHP\DB;
class UserDepartmentRepository
{
public static function listDepartmentIdsByUserId(int $userId): array
{
$rows = DB::select('select department_id from user_departments where user_id = ?', (string) $userId);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['user_departments'] ?? $row;
if (is_array($data) && isset($data['department_id'])) {
$ids[] = (int) $data['department_id'];
}
}
return array_values(array_unique($ids));
}
public static function replaceForUser(int $userId, array $departmentIds): bool
{
DB::delete('delete from user_departments where user_id = ?', (string) $userId);
if (!$departmentIds) {
return true;
}
foreach ($departmentIds as $departmentId) {
DB::insert(
'insert into user_departments (user_id, department_id, created) values (?,?,NOW())',
(string) $userId,
(string) $departmentId
);
}
return true;
}
public static function removeInvalidForDepartment(int $departmentId): int
{
$query = 'delete ud from user_departments ud ' .
'where ud.department_id = ? and not exists (' .
'select 1 from user_tenants ut ' .
'join tenant_departments td on td.tenant_id = ut.tenant_id ' .
'where ut.user_id = ud.user_id and td.department_id = ud.department_id' .
')';
$result = DB::delete($query, (string) $departmentId);
return $result !== false ? (int) $result : 0;
}
}