DONE: feedback module, use permission name for notification
This commit is contained in:
@@ -13,6 +13,7 @@ use Modules\Auth\Entities\User;
|
||||
use Modules\Auth\Services\PhoneVerificationOtpService;
|
||||
use Modules\Role\Entities\Role;
|
||||
use Modules\User\Notifications\UserActivationNotification;
|
||||
use Modules\User\Policies\UserPolicy;
|
||||
use Exception;
|
||||
|
||||
class CreateNewUser implements CreatesNewUsers
|
||||
@@ -23,11 +24,6 @@ class CreateNewUser implements CreatesNewUsers
|
||||
protected PhoneVerificationOtpService $phoneVerificationOtpService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validate and create a newly registered user.
|
||||
*
|
||||
* @param array<string, string> $input
|
||||
*/
|
||||
public function create(array $input): User
|
||||
{
|
||||
$phoneNumber = $this->phoneVerificationOtpService->normalizePhoneNumber($input['phone_number'] ?? '');
|
||||
@@ -77,6 +73,34 @@ class CreateNewUser implements CreatesNewUsers
|
||||
$user->assignRole($role);
|
||||
}
|
||||
|
||||
if ($user->status === 'pending') {
|
||||
$this->notifyAdminsForActivation($user);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify users who can kemaskini pengguna about new user requiring activation.
|
||||
*/
|
||||
private function notifyAdminsForActivation(User $newUser): void
|
||||
{
|
||||
try {
|
||||
$recipients = $this->getUsersWithPermission(UserPolicy::PERMISSION_UPDATE)
|
||||
->where('id', '!=', $newUser->id);
|
||||
|
||||
$sender = auth()->user() ?? $newUser;
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
try {
|
||||
$recipient->notify(new UserActivationNotification($newUser, $sender));
|
||||
} catch (Exception $e) {
|
||||
Log::error('Failed to send user activation notification: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Failed to notify admins for user activation: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user