## 📘 NotificationService — Dokumentasi Penggunaan

### 📄 **Deskripsi**
`NotificationService` adalah service Laravel custom yang bertugas mengirim notifikasi ke berbagai platform pihak ketiga seperti **WhatsApp** dan **Telegram**, berdasarkan konfigurasi yang tersimpan di database (`third_party_apis`).

---

## 🧩 Struktur Tabel `third_party_apis`

| Kolom       | Tipe Data | Contoh                                           |
|-------------|-----------|--------------------------------------------------|
| api_name    | string    | `WhatsApp`, `Telegram`                          |
| api_url     | string    | `http://116.203.191.58/api/`, `https://api.telegram.org/` |
| api_token   | string    | `db63f5...`, `bot123456:ABC-DEF1234...`         |

---

## 🔧 Cara Menggunakan NotificationService

### 1. **Konstruktor Service (Inject via Dependency Injection)**
```php
use App\Services\NotificationService;
```

### 2. **Memanggil Service**

#### Contoh dalam method `approve()`:
```php
public function approve(string $id, NotificationService $notificationService)
{
    try {
        $loan = Loan::findOrFail($id);
        $loan->update(['status' => 'approved']);

        $message = "Loan #$loan->code telah disetujui."; // isi pesan yang dikirimkan
        $phone = '6289123443215';
        /**
         * Kirim ke WhatsApp
         * Untuk $phone bisa di ambil dari DB dengan syarat nomer depan semuanya 62 atau manual
         */
        $notificationService->sendNotification('WhatsApp', $phone, $message);
        /**
         * Kirim ke Telegram
         * Untuk chat id telegram ini -974886024 bisa di ganti sesuai dengan tujuan chatnya
         * ini default ke group report bot cronjobs fbt di telegram 
         */
        $notificationService->sendNotification('Telegram', '-974886024', $message);

        return redirect()->route('loan.index')->with('success', "Loan #$loan->code approved successfully");
    } catch (\Exception $e) {
        return redirect()->route('loan.index')->with('error', 'Failed to approve loan: ' . $e->getMessage());
    }
}
```

---

## 🔌 Fungsi `sendNotification()`

```php
sendNotification(string $apiName, string $recipientId, string $message)
```

### Parameter:
- `apiName`: Nama API (harus sesuai dengan `api_name` di database), misalnya: `WhatsApp`, `Telegram`
- `recipientId`: 
  - Untuk **WhatsApp** → nomor HP tanpa `+`, misal: `6281234567890`
  - Untuk **Telegram** → `chat_id`, misal: `-974886024`
- `message`: Isi pesan yang akan dikirim

### Return:
- Jika sukses → response JSON: `{ "message": "Notification sent successfully." }`
- Jika gagal → response JSON: `{ "error": "Failed to send notification." }`

---

## 📲 Contoh Hasil Notifikasi

- **WhatsApp**:
  ```
  Loan #LN-2024-001 telah disetujui.
  ```

- **Telegram**:
  ```
  Loan #LN-2024-001 telah disetujui.
  ```

---

## ⚠️ Catatan Teknis

- Endpoint Telegram harus mengikuti format: `https://api.telegram.org/bot<token>/sendMessage`
- WhatsApp API memakai `POST` JSON ke endpoint: `http://116.203.191.58/api/send_message`
- Token API diambil dari kolom `api_token` pada tabel `third_party_apis`
- Gunakan `rtrim($url, '/')` untuk menghindari `//` di URL ()
- Tulisakna script dibawah untuk enable dan disable notifikasi di .env atau langsung rubah di config/services.php
```env
NOTIFICATION_ENABLED=true #default: true
NOTIF_TELEGRAM=false #default: false
NOTIF_WHATSAPP=false #default: false
```
