fix issues related to school year
Tests / PHPUnit (push) Failing after 34s

This commit is contained in:
root
2026-07-31 18:52:25 -04:00
parent 8d7ee3b9fc
commit 09ea144bb2
23 changed files with 537 additions and 218 deletions
@@ -9,6 +9,7 @@ use App\Models\UserModel;
use App\Models\UserRoleModel;
use App\Models\ConfigurationModel;
use App\Models\StaffModel;
use App\Services\StaffDirectorySyncService;
use CodeIgniter\Controller;
class RolePermissionController extends Controller
@@ -20,6 +21,7 @@ class RolePermissionController extends Controller
protected $configModel;
protected $permissionModel;
protected $rolePermissionModel;
protected StaffDirectorySyncService $staffDirectorySync;
protected $request;
protected $db;
protected $schoolYear;
@@ -31,6 +33,7 @@ class RolePermissionController extends Controller
$this->userModel = new UserModel();
$this->userRoleModel = new UserRoleModel();
$this->staffModel = new StaffModel();
$this->staffDirectorySync = service('staffDirectorySync');
$this->configModel = new ConfigurationModel();
$this->permissionModel = new PermissionModel();
$this->rolePermissionModel = new RolePermissionModel();
@@ -224,49 +227,7 @@ class RolePermissionController extends Controller
private function updateStaffRecord(int $userId, array $newRoleNames): void
{
$excludedRoles = ['parent', 'student', 'guest'];
$loweredNewRoles = array_map('strtolower', $newRoleNames);
$user = $this->userModel->find($userId);
if (!$user) {
log_message('error', "updateStaffRecord: user_id {$userId} not found");
return;
}
// Fetch existing staff record if it exists
$existingStaff = $this->staffModel->where('user_id', $userId)->first();
// Extract existing roles from DB if any
$existingRoles = [];
if (!empty($existingStaff['role_name'])) {
$existingRoles = array_map('strtolower', array_map('trim', explode(',', $existingStaff['role_name'])));
}
// Merge and deduplicate roles
$allRoles = array_unique(array_merge($existingRoles, $loweredNewRoles));
// Determine staff roles (excluding parent/student/guest)
$staffRoles = array_filter($newRoleNames, fn($role) => !in_array($role, $excludedRoles));
// Determine active role
$activeRole = !empty($staffRoles) ? $staffRoles[0] : 'inactive';
$email = $this->generateUniqueStaffEmail($user['firstname'], $user['lastname'], $userId);
$now = utc_now();
$row = [
'user_id' => $userId,
'firstname' => $user['firstname'],
'lastname' => $user['lastname'],
'email' => $email,
'phone' => $user['cellphone'],
'role_name' => implode(', ', $allRoles), // historical roles
'active_role' => $activeRole,
'school_year' => $this->schoolYear,
'updated_at' => $now,
];
$this->staffModel->upsert($row);
$this->staffDirectorySync->syncUser($userId);
}
private function generateUniqueStaffEmail(string $firstname, string $lastname, int $userId): string