Files
alrahma_sunday_school_api/app/Models/Notification.php
T
2026-03-05 12:29:37 -05:00

90 lines
2.2 KiB
PHP

<?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();
}
}