ifnex/04_Laravel/app/Observers/ShipmentObserver.php
Kazem Alghasi d6e04d53fa feat(core): integrate Kavenegar SMS notifications and audit logging
Implement a comprehensive notification system using Kavenegar SMS
gateway and enhance system traceability through audit logging and
detailed shipment status history.

- SMS Integration:
  - Add Kavenegar SMS service with configurable API keys and sender
    numbers via system settings.
  - Implement automated SMS notifications for shipment approval,
    rejection, successful payments, and tracking updates.
  - Add administrative UI in Filament to manage SMS gateway settings
    and toggle specific notification types.
- Audit & Tracking:
  - Apply `Auditable` trait to core models (User, Shipment, Wallet,
    etc.) to track changes.
  - Refactor `ShipmentStatusHistory` to include status transitions
    (`from_status` to `to_status`) and specific reasons for changes.
  - Implement `ShipmentObserver` to automate notification triggers
    on status changes.
- Database & Config:
  - Add migrations for enhanced shipment status history tracking.
  - Update `.env.example` and `config/ifnex.php` with Kavenegar
    configuration parameters.
2026-09-10 00:52:18 +03:30

46 lines
1.5 KiB
PHP

<?php
namespace App\Observers;
use App\Models\Shipment;
use App\Notifications\ShipmentUpdatedNotification;
use App\Notifications\ShipmentApprovedSms;
use App\Notifications\ShipmentRejectedSms;
use Illuminate\Support\Facades\Config;
class ShipmentObserver
{
public function updated(Shipment $shipment): void
{
$changes = $shipment->getChanges();
$ignored = ['updated_at'];
$changedFields = array_filter(
array_keys($changes),
fn ($key) => !in_array($key, $ignored)
);
if (empty($changedFields)) {
return;
}
ShipmentUpdatedNotification::notify($shipment);
$oldStatus = $shipment->getOriginal('status');
$newStatus = $shipment->status;
if ($oldStatus !== $newStatus) {
$trackingUrl = url("/track/{$shipment->awb_no}");
$smsEnabled = !empty(SystemSetting::get('kavenegar_api_key'));
if ($smsEnabled && $newStatus === \App\Enums\ShipmentStatus::Approved && (bool) SystemSetting::get('kavenegar_send_shipment_approved', true)) {
$shipment->user->notify(new ShipmentApprovedSms($shipment->awb_no, $trackingUrl));
}
if ($smsEnabled && $newStatus === \App\Enums\ShipmentStatus::Cancelled && (bool) SystemSetting::get('kavenegar_send_shipment_rejected', true)) {
$shipment->user->notify(new ShipmentRejectedSms($shipment->awb_no, 'سفارش شما رد شد.'));
}
}
}
}