- Updated CustomerService to simplify getAll method. - Refactored ProductService to utilize HasRoleChecks trait and improved role verification logic. - Enhanced ProductVariantService with new methods for fetching data for restocking and transactions. - Cleaned up RawMaterialService by removing unused methods and improving data retrieval. - Adjusted SupplierService to streamline getAll method. - Refactored RoleService to use Spatie's Role model and improved role filtering logic. - Updated NotificationService to handle role labels more effectively. - Improved StockMutationService by removing redundant paginated method. - Cleaned up various frontend components to directly accept necessary props instead of nested data objects. - Updated tests to reflect changes in service method names and ensure proper notification handling.
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\User;
|
|
use App\Notifications\WebPushNotification;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class NotificationService
|
|
{
|
|
/**
|
|
* @param array<Role> $roles
|
|
*/
|
|
public static function notify(array $roles, string $title, string $body, string $url, ?User $additionalUser = null): void
|
|
{
|
|
$roleLabels = array_map(fn (Role $role) => $role->label(), $roles);
|
|
|
|
$users = User::query()
|
|
->select(['id'])
|
|
->where('is_active', true)
|
|
->whereHas('roles', fn ($q) => $q->whereIn('name', $roleLabels))
|
|
->get();
|
|
|
|
if ($additionalUser && ! $users->contains('id', $additionalUser->id)) {
|
|
$users->push($additionalUser);
|
|
}
|
|
|
|
$users->each(function (User $user) use ($title, $body, $url) {
|
|
try {
|
|
$user->notifications()->create([
|
|
'title' => $title,
|
|
'body' => $body,
|
|
'url' => $url,
|
|
]);
|
|
|
|
$user->notify(new WebPushNotification($title, $body));
|
|
} catch (\Exception $e) {
|
|
Log::error("Gagal mengirim notifikasi ke user {$user->id}: {$e->getMessage()}");
|
|
}
|
|
});
|
|
}
|
|
}
|