ADD: Notificaciones en tiempo real (#3)
* ADD: Notificaciones * ADD: Usuarios conectados en tiempo real
This commit is contained in:
parent
cc3684d23b
commit
dfb1fbf1e9
@ -3,9 +3,9 @@
|
||||
* @copyright 2024 Notsoweb (https://notsoweb.com) - All rights reserved.
|
||||
*/
|
||||
|
||||
use App\Models\Notification;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Notifications\DatabaseNotification;
|
||||
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
||||
use Notsoweb\LaravelCore\Controllers\VueController;
|
||||
|
||||
@ -31,7 +31,7 @@ public function __construct()
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$q = request()->get('query');
|
||||
$q = request()->get('q');
|
||||
|
||||
$model = auth()->user()
|
||||
->notifications();
|
||||
@ -41,45 +41,38 @@ public function index()
|
||||
->orWhere('data->message', "LIKE", "%{$q}%");
|
||||
}
|
||||
|
||||
return $this->view('index', [
|
||||
return ApiResponse::OK->response([
|
||||
'models' => $model
|
||||
->paginate(config('app.pagination'))
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Todas las notificaciones
|
||||
*/
|
||||
public function all(): JsonResponse
|
||||
{
|
||||
$query = request()->get('query');
|
||||
|
||||
$model = auth()->user()
|
||||
->notifications();
|
||||
|
||||
if($query) {
|
||||
$model = $model->where('data->title', 'LIKE', "%{$query}%")
|
||||
->orWhere('data->message', "LIKE", "%{$query}%");
|
||||
}
|
||||
|
||||
return ApiResponse::OK->axios([
|
||||
'notifications' => $model
|
||||
->paginate(config('app.pagination'))
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marcar notificación como leída
|
||||
*/
|
||||
public function read(Request $request): JsonResponse
|
||||
{
|
||||
$notification = DatabaseNotification::find($request->get('id'));
|
||||
$notification = Notification::find($request->get('id'));
|
||||
|
||||
if ($notification) {
|
||||
$notification->markAsRead();
|
||||
}
|
||||
|
||||
return ApiResponse::OK->axios();
|
||||
return ApiResponse::OK->response();
|
||||
}
|
||||
|
||||
/**
|
||||
* Marcar notificación como cerrada
|
||||
*/
|
||||
public function close(Request $request): JsonResponse
|
||||
{
|
||||
$notification = Notification::find($request->get('id'));
|
||||
|
||||
if ($notification) {
|
||||
$notification->markAsClosed();
|
||||
}
|
||||
|
||||
return ApiResponse::OK->response();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,9 +80,10 @@ public function read(Request $request): JsonResponse
|
||||
*/
|
||||
public function allUnread(): JsonResponse
|
||||
{
|
||||
return ApiResponse::OK->axios([
|
||||
return ApiResponse::OK->response([
|
||||
'total' => auth()->user()->unreadNotifications()->count(),
|
||||
'notifications' => auth()->user()->unreadNotifications()->limit(10)->get(),
|
||||
'unread_closed' => auth()->user()->unreadNotifications()->where('is_closed', true)->count(),
|
||||
'notifications' => auth()->user()->unreadNotifications()->where('is_closed', false)->limit(10)->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ public function index()
|
||||
QuerySupport::queryByKeys($users, ['name', 'email']);
|
||||
|
||||
return ApiResponse::OK->response([
|
||||
'users' => $users->paginate(config('app.pagination'))
|
||||
'models' => $users->paginate(config('app.pagination'))
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
39
app/Http/Traits/HasDatabaseNotifications.php
Normal file
39
app/Http/Traits/HasDatabaseNotifications.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php namespace App\Http\Traits;
|
||||
|
||||
use App\Models\Notification;
|
||||
|
||||
/**
|
||||
* Notificaciones de base de datos
|
||||
*
|
||||
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
||||
*/
|
||||
trait HasDatabaseNotifications
|
||||
{
|
||||
/**
|
||||
* Get the entity's notifications.
|
||||
*/
|
||||
public function notifications()
|
||||
{
|
||||
return $this->morphMany(Notification::class, 'notifiable')->with('user:id,name,paternal,maternal,profile_photo_path')->latest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entity's read notifications.
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
public function readNotifications()
|
||||
{
|
||||
return $this->notifications()->read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entity's unread notifications.
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
public function unreadNotifications()
|
||||
{
|
||||
return $this->notifications()->unread();
|
||||
}
|
||||
}
|
||||
13
app/Http/Traits/IsNotifiable.php
Normal file
13
app/Http/Traits/IsNotifiable.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php namespace App\Http\Traits;
|
||||
|
||||
use Illuminate\Notifications\RoutesNotifications;
|
||||
|
||||
/**
|
||||
* Notificaciones personalizadas
|
||||
*
|
||||
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
||||
*/
|
||||
trait IsNotifiable
|
||||
{
|
||||
use HasDatabaseNotifications, RoutesNotifications;
|
||||
}
|
||||
161
app/Models/Notification.php
Normal file
161
app/Models/Notification.php
Normal file
@ -0,0 +1,161 @@
|
||||
<?php namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\HasCollection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Notifications\DatabaseNotificationCollection;
|
||||
|
||||
/**
|
||||
* Sistema personalizado de notificaciones
|
||||
*
|
||||
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
||||
*/
|
||||
class Notification extends Model
|
||||
{
|
||||
use HasCollection;
|
||||
|
||||
/**
|
||||
* The "type" of the primary key ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $keyType = 'string';
|
||||
|
||||
/**
|
||||
* Indicates if the IDs are auto-incrementing.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $incrementing = false;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'notifications';
|
||||
|
||||
/**
|
||||
* The guarded attributes on the model.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'data' => 'array',
|
||||
'read_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(fn($model) => $model->user_id = auth()?->user()?->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of collection that should be used for the model.
|
||||
*/
|
||||
protected static string $collectionClass = DatabaseNotificationCollection::class;
|
||||
|
||||
/**
|
||||
* Get the notifiable entity that the notification belongs to.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
|
||||
*/
|
||||
public function notifiable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Una notificación puede ser creada por un usuario
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the notification as read.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function markAsRead()
|
||||
{
|
||||
if (is_null($this->read_at)) {
|
||||
$this->forceFill(['read_at' => $this->freshTimestamp()])->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marcar notificación como cerrada
|
||||
*/
|
||||
public function markAsClosed()
|
||||
{
|
||||
$this->forceFill(['is_closed' => true])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the notification as unread.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function markAsUnread()
|
||||
{
|
||||
if (! is_null($this->read_at)) {
|
||||
$this->forceFill(['read_at' => null])->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a notification has been read.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
return $this->read_at !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a notification has not been read.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function unread()
|
||||
{
|
||||
return $this->read_at === null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include read notifications.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder<static> $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder<static>
|
||||
*/
|
||||
public function scopeRead(Builder $query)
|
||||
{
|
||||
return $query->whereNotNull('read_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include unread notifications.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder<static> $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder<static>
|
||||
*/
|
||||
public function scopeUnread(Builder $query)
|
||||
{
|
||||
return $query->whereNull('read_at');
|
||||
}
|
||||
}
|
||||
@ -5,10 +5,10 @@
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\Http\Traits\HasProfilePhoto;
|
||||
use App\Http\Traits\IsNotifiable;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
@ -25,7 +25,7 @@ class User extends Authenticatable
|
||||
HasFactory,
|
||||
HasRoles,
|
||||
HasProfilePhoto,
|
||||
Notifiable;
|
||||
IsNotifiable;
|
||||
|
||||
/**
|
||||
* Atributos permitidos
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
/**
|
||||
* Descripción
|
||||
* Notificación de recuperación de contraseña
|
||||
*
|
||||
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
||||
*
|
||||
|
||||
@ -1,9 +1,19 @@
|
||||
<?php namespace App\Notifications;
|
||||
/**
|
||||
* @copyright (c) 2024 Notsoweb Software (https://notsoweb.com) - All Rights Reserved
|
||||
*/
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Messages\BroadcastMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
/**
|
||||
* Notificación de usuario
|
||||
*
|
||||
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
class UserNotification extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
@ -17,6 +27,7 @@ public function __construct(
|
||||
public ?string $message = null,
|
||||
public string $type = 'info',
|
||||
public int $timeout = 20,
|
||||
public bool $save = true,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -26,10 +37,13 @@ public function __construct(
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
'broadcast',
|
||||
'database'
|
||||
];
|
||||
$vias = ['broadcast'];
|
||||
|
||||
if ($this->save) {
|
||||
$vias[] = 'database';
|
||||
}
|
||||
|
||||
return $vias;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -22,7 +22,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
if ($this->app->environment('local')) {
|
||||
if ($this->app->environment('local') && config('telescope.enabled') == 'true') {
|
||||
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
|
||||
$this->app->register(TelescopeServiceProvider::class);
|
||||
}
|
||||
|
||||
@ -17,6 +17,11 @@ public function up(): void
|
||||
$table->morphs('notifiable');
|
||||
$table->text('data');
|
||||
$table->timestamp('read_at')->nullable();
|
||||
$table->boolean('is_closed')->default(false);
|
||||
$table->foreignId('user_id') // Usuario que crea la notificación
|
||||
->nullable()
|
||||
->constrained('users')
|
||||
->nullOnDelete();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
@ -27,6 +27,7 @@ public function run(): void
|
||||
] = $this->onCRUD('users', $users);
|
||||
|
||||
$userSettings = $this->onPermission('users.settings', 'Configuración de usuarios', $users);
|
||||
$userOnline = $this->onPermission('users.online', 'Usuarios en linea', $users);
|
||||
|
||||
// Desarrollador
|
||||
Role::create([
|
||||
@ -43,7 +44,8 @@ public function run(): void
|
||||
$userCreate,
|
||||
$userEdit,
|
||||
$userDestroy,
|
||||
$userSettings
|
||||
$userSettings,
|
||||
$userOnline
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,5 +28,14 @@ public function run(): void
|
||||
'email' => $admin->email,
|
||||
'password' => $admin->hash,
|
||||
])->assignRole(__('admin'));
|
||||
|
||||
$demo = UserSecureSupport::create('demo@notsoweb.com');
|
||||
|
||||
User::create([
|
||||
'name' => 'Demo',
|
||||
'paternal' => 'Notsoweb',
|
||||
'email' => $demo->email,
|
||||
'password' => $demo->hash,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,8 +43,10 @@
|
||||
Route::get('permissions', [SystemController::class, 'permissions'])->name('permissions');
|
||||
Route::get('roles', [SystemController::class, 'roles'])->name('roles');
|
||||
Route::prefix('notifications')->name('notifications.')->group(function() {
|
||||
Route::get('all', [NotificationController::class, 'index'])->name('all');
|
||||
Route::get('all-unread', [NotificationController::class, 'allUnread'])->name('all-unread');
|
||||
Route::post('read', [NotificationController::class, 'read'])->name('read');
|
||||
Route::post('close', [NotificationController::class, 'close'])->name('close');
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,3 +63,4 @@
|
||||
Route::post('forgot-password', [LoginController::class, 'forgotPassword'])->name('forgot-password');
|
||||
Route::post('reset-password', [LoginController::class, 'resetPassword'])->name('reset-password');
|
||||
});
|
||||
|
||||
|
||||
@ -1,9 +1,18 @@
|
||||
<?php use Illuminate\Support\Facades\Broadcast;
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
|
||||
// Notificación usuario especifico
|
||||
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
|
||||
return (int) $user->id === (int) $id;
|
||||
});
|
||||
|
||||
// Usuarios en linea
|
||||
Broadcast::channel('online', function ($user) {
|
||||
return $user;
|
||||
});
|
||||
|
||||
// Notificación global
|
||||
Broadcast::channel('Global', function ($user) {
|
||||
return $user->id !== null;
|
||||
});
|
||||
@ -6,6 +6,6 @@ export default defineConfig({
|
||||
laravel({
|
||||
input: ['resources/css/app.css', 'resources/js/app.js'],
|
||||
refresh: true,
|
||||
}),
|
||||
})
|
||||
],
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user