add projet
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class Notification extends BaseModel
|
||||
{
|
||||
protected $table = 'notifications';
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'message',
|
||||
'target_group',
|
||||
'delivery_channels',
|
||||
'priority',
|
||||
'status',
|
||||
'action_url',
|
||||
'attachment_path',
|
||||
'scheduled_at',
|
||||
'sent_at',
|
||||
'expires_at',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
'school_year',
|
||||
'semester',
|
||||
];
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
protected $useSoftDeletes = true;
|
||||
|
||||
/**
|
||||
* Get active (non-deleted, non-expired) notifications
|
||||
*/
|
||||
public function getActiveNotifications($targetGroup = null)
|
||||
{
|
||||
$builder = $this->where('scheduled_at <= NOW()')
|
||||
->where('(expires_at IS NULL OR expires_at > NOW())');
|
||||
|
||||
if (!empty($targetGroup)) {
|
||||
$builder->where('target_group', $targetGroup);
|
||||
}
|
||||
|
||||
return $builder->orderBy('priority', 'DESC')
|
||||
->orderBy('scheduled_at', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get soft-deleted (archived) notifications
|
||||
*/
|
||||
public function getDeletedNotifications()
|
||||
{
|
||||
return $this->onlyDeleted()
|
||||
->orderBy('deleted_at', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all notifications including soft-deleted
|
||||
*/
|
||||
public function getAllNotificationsWithDeleted()
|
||||
{
|
||||
return $this->withDeleted()
|
||||
->orderBy('created_at', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a soft-deleted notification by ID
|
||||
*/
|
||||
public function restoreNotification($id)
|
||||
{
|
||||
return $this->update($id, ['deleted_at' => null]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup expired notifications (optional for cron job use)
|
||||
*/
|
||||
public function deleteExpiredNotifications()
|
||||
{
|
||||
return $this->where('expires_at IS NOT NULL')
|
||||
->where('expires_at < NOW()')
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user