reconstruction of the project
This commit is contained in:
+54
-34
@@ -3,11 +3,17 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Notification extends BaseModel
|
||||
{
|
||||
protected $table = 'notifications';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
// ✅ CI: soft deletes + timestamps
|
||||
use SoftDeletes;
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'message',
|
||||
@@ -26,64 +32,78 @@ class Notification extends BaseModel
|
||||
'school_year',
|
||||
'semester',
|
||||
];
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
protected $useSoftDeletes = true;
|
||||
|
||||
protected $casts = [
|
||||
'scheduled_at' => 'datetime',
|
||||
'expires_at' => 'datetime',
|
||||
// If delivery_channels is JSON in DB:
|
||||
'delivery_channels' => 'array',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get active (non-deleted, non-expired) notifications
|
||||
* Equivalent of CI getActiveNotifications()
|
||||
* Active = scheduled_at <= now AND (expires_at is null OR expires_at > now)
|
||||
*/
|
||||
public function getActiveNotifications($targetGroup = null)
|
||||
public static function getActiveNotifications(?string $targetGroup = null): array
|
||||
{
|
||||
$builder = $this->where('scheduled_at <= NOW()')
|
||||
->where('(expires_at IS NULL OR expires_at > NOW())');
|
||||
$q = static::query()
|
||||
->where('scheduled_at', '<=', now())
|
||||
->where(function ($w) {
|
||||
$w->whereNull('expires_at')
|
||||
->orWhere('expires_at', '>', now());
|
||||
});
|
||||
|
||||
if (!empty($targetGroup)) {
|
||||
$builder->where('target_group', $targetGroup);
|
||||
$q->where('target_group', $targetGroup);
|
||||
}
|
||||
|
||||
return $builder->orderBy('priority', 'DESC')
|
||||
->orderBy('scheduled_at', 'DESC')
|
||||
->findAll();
|
||||
return $q->orderByDesc('priority')
|
||||
->orderByDesc('scheduled_at')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get soft-deleted (archived) notifications
|
||||
* Equivalent of CI getDeletedNotifications()
|
||||
*/
|
||||
public function getDeletedNotifications()
|
||||
public static function getDeletedNotifications(): array
|
||||
{
|
||||
return $this->onlyDeleted()
|
||||
->orderBy('deleted_at', 'DESC')
|
||||
->findAll();
|
||||
return static::onlyTrashed()
|
||||
->orderByDesc('deleted_at')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all notifications including soft-deleted
|
||||
* Equivalent of CI getAllNotificationsWithDeleted()
|
||||
*/
|
||||
public function getAllNotificationsWithDeleted()
|
||||
public static function getAllNotificationsWithDeleted(): array
|
||||
{
|
||||
return $this->withDeleted()
|
||||
->orderBy('created_at', 'DESC')
|
||||
->findAll();
|
||||
return static::withTrashed()
|
||||
->orderByDesc('created_at')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a soft-deleted notification by ID
|
||||
* Equivalent of CI restoreNotification()
|
||||
*/
|
||||
public function restoreNotification($id)
|
||||
public static function restoreNotification(int $id): bool
|
||||
{
|
||||
return $this->update($id, ['deleted_at' => null]);
|
||||
$row = static::withTrashed()->find($id);
|
||||
if (!$row) return false;
|
||||
return (bool) $row->restore();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cleanup expired notifications (optional for cron job use)
|
||||
* Equivalent of CI deleteExpiredNotifications()
|
||||
* Soft-deletes rows with expires_at < now (and expires_at not null)
|
||||
*/
|
||||
public function deleteExpiredNotifications()
|
||||
public static function deleteExpiredNotifications(): int
|
||||
{
|
||||
return $this->where('expires_at IS NOT NULL')
|
||||
->where('expires_at < NOW()')
|
||||
->delete();
|
||||
return static::query()
|
||||
->whereNotNull('expires_at')
|
||||
->where('expires_at', '<', now())
|
||||
->delete(); // soft delete
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user