fix apply the plan docs/alrahma_api_fix_plan_school_year_only_v7
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
namespace App\Services\Staff;
|
||||
|
||||
use App\Models\Staff;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Services\System\GlobalConfigService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use RuntimeException;
|
||||
|
||||
class StaffCommandService
|
||||
@@ -15,9 +17,11 @@ class StaffCommandService
|
||||
public function create(array $payload): Staff
|
||||
{
|
||||
return DB::transaction(function () use ($payload) {
|
||||
$userId = $this->resolveUserId($payload);
|
||||
$role = $this->resolveRole($payload);
|
||||
$user = $this->resolveOrCreateUser($payload, $role);
|
||||
$userId = (int) $user->id;
|
||||
|
||||
$roleName = (string) ($payload['role_name'] ?? '');
|
||||
$roleName = (string) $role->name;
|
||||
$activeRole = strtolower($roleName);
|
||||
if (! empty($payload['status']) && strtolower((string) $payload['status']) === 'inactive') {
|
||||
$activeRole = 'inactive';
|
||||
@@ -25,8 +29,14 @@ class StaffCommandService
|
||||
|
||||
$schoolYear = (string) ($payload['school_year'] ?? $this->configService->getSchoolYear() ?? '');
|
||||
|
||||
$staff = Staff::query()->create([
|
||||
DB::table('user_roles')->updateOrInsert(
|
||||
['user_id' => $userId, 'role_id' => (int) $role->id],
|
||||
['created_at' => now(), 'updated_at' => now()]
|
||||
);
|
||||
|
||||
$staff = Staff::query()->updateOrCreate([
|
||||
'user_id' => $userId,
|
||||
], [
|
||||
'firstname' => (string) $payload['firstname'],
|
||||
'lastname' => (string) $payload['lastname'],
|
||||
'email' => (string) $payload['email'],
|
||||
@@ -34,7 +44,6 @@ class StaffCommandService
|
||||
'role_name' => $roleName,
|
||||
'active_role' => $activeRole,
|
||||
'school_year' => $schoolYear,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
@@ -49,15 +58,23 @@ class StaffCommandService
|
||||
public function update(Staff $staff, array $payload): Staff
|
||||
{
|
||||
return DB::transaction(function () use ($staff, $payload) {
|
||||
$role = array_key_exists('role_id', $payload) || array_key_exists('role_name', $payload)
|
||||
? $this->resolveRole($payload)
|
||||
: null;
|
||||
$updates = [];
|
||||
foreach (['firstname', 'lastname', 'email', 'phone', 'role_name', 'school_year'] as $field) {
|
||||
foreach (['firstname', 'lastname', 'email', 'phone', 'school_year'] as $field) {
|
||||
if (array_key_exists($field, $payload) && $payload[$field] !== '') {
|
||||
$updates[$field] = $payload[$field];
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('role_name', $updates)) {
|
||||
if ($role) {
|
||||
$updates['role_name'] = (string) $role->name;
|
||||
$updates['active_role'] = strtolower((string) $updates['role_name']);
|
||||
DB::table('user_roles')->updateOrInsert(
|
||||
['user_id' => (int) $staff->user_id, 'role_id' => (int) $role->id],
|
||||
['created_at' => now(), 'updated_at' => now()]
|
||||
);
|
||||
}
|
||||
if (! empty($payload['status']) && strtolower((string) $payload['status']) === 'inactive') {
|
||||
$updates['active_role'] = 'inactive';
|
||||
@@ -68,6 +85,15 @@ class StaffCommandService
|
||||
if (! empty($updates)) {
|
||||
$staff->fill($updates);
|
||||
$staff->save();
|
||||
|
||||
$userUpdates = array_intersect_key($updates, array_flip(['firstname', 'lastname', 'email', 'phone', 'school_year']));
|
||||
if (isset($userUpdates['phone'])) {
|
||||
$userUpdates['cellphone'] = $userUpdates['phone'];
|
||||
unset($userUpdates['phone']);
|
||||
}
|
||||
if ($userUpdates !== []) {
|
||||
User::query()->whereKey((int) $staff->user_id)->update($userUpdates + ['updated_at' => now()]);
|
||||
}
|
||||
}
|
||||
|
||||
return $staff->fresh();
|
||||
@@ -79,20 +105,69 @@ class StaffCommandService
|
||||
return (bool) $staff->delete();
|
||||
}
|
||||
|
||||
private function resolveUserId(array $payload): int
|
||||
private function resolveRole(array $payload): Role
|
||||
{
|
||||
if (! empty($payload['user_id'])) {
|
||||
return (int) $payload['user_id'];
|
||||
}
|
||||
|
||||
$email = $payload['email'] ?? null;
|
||||
if ($email) {
|
||||
$userId = User::query()->where('email', $email)->value('id');
|
||||
if ($userId) {
|
||||
return (int) $userId;
|
||||
if (! empty($payload['role_id'])) {
|
||||
$role = Role::query()->whereKey((int) $payload['role_id'])->where('is_active', 1)->first();
|
||||
if ($role) {
|
||||
return $role;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('A valid user_id or existing email is required.');
|
||||
$roleName = trim((string) ($payload['role_name'] ?? ''));
|
||||
if ($roleName !== '') {
|
||||
$role = Role::query()
|
||||
->where('is_active', 1)
|
||||
->where(function ($query) use ($roleName) {
|
||||
$query->whereRaw('LOWER(name) = ?', [strtolower($roleName)])
|
||||
->orWhereRaw('LOWER(slug) = ?', [strtolower($roleName)]);
|
||||
})
|
||||
->first();
|
||||
if ($role) {
|
||||
return $role;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('A valid active role is required.');
|
||||
}
|
||||
|
||||
private function resolveOrCreateUser(array $payload, Role $role): User
|
||||
{
|
||||
if (! empty($payload['user_id'])) {
|
||||
$user = User::query()->find((int) $payload['user_id']);
|
||||
if (! $user) {
|
||||
throw new RuntimeException('The selected user was not found.');
|
||||
}
|
||||
} else {
|
||||
$email = strtolower(trim((string) ($payload['email'] ?? '')));
|
||||
if ($email === '') {
|
||||
throw new RuntimeException('A valid email is required.');
|
||||
}
|
||||
$user = User::query()->whereRaw('LOWER(email) = ?', [$email])->first() ?? new User();
|
||||
}
|
||||
|
||||
$isNew = ! $user->exists;
|
||||
$user->firstname = (string) $payload['firstname'];
|
||||
$user->lastname = (string) $payload['lastname'];
|
||||
$user->email = strtolower(trim((string) $payload['email']));
|
||||
$user->cellphone = $payload['phone'] ?? $user->cellphone ?? null;
|
||||
$user->school_year = (string) ($payload['school_year'] ?? $this->configService->getSchoolYear() ?? '');
|
||||
$user->semester = $user->semester ?: (string) ($this->configService->getSemester() ?? '');
|
||||
$user->status = ! empty($payload['status']) && strtolower((string) $payload['status']) === 'inactive' ? 'Inactive' : 'Active';
|
||||
$user->is_verified = 1;
|
||||
$user->is_suspended = 0;
|
||||
$user->accept_school_policy = $user->accept_school_policy ?? 1;
|
||||
$user->school_id = $user->school_id ?: random_int(100000, 999999);
|
||||
if ($isNew) {
|
||||
$password = (string) ($payload['password'] ?? '');
|
||||
if ($password === '') {
|
||||
throw new RuntimeException('A temporary password is required for a new staff user.');
|
||||
}
|
||||
$user->password = Hash::make($password);
|
||||
}
|
||||
$user->user_type = (string) $role->name;
|
||||
$user->save();
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user