Skip to main content

pakem/core/notification/
mod.rs

1use async_trait::async_trait;
2use serde_json::Value;
3
4/// Trait Notification — Kontrak untuk class notifikasi.
5#[async_trait]
6pub trait Notification: Send + Sync {
7    /// Judul notifikasi.
8    fn subject(&self) -> String {
9        "Notification from Lumina".to_string()
10    }
11
12    /// Representasi Mail.
13    fn to_mail(&self) -> Option<(String, Value)> {
14        None
15    }
16
17    /// Representasi Database (untuk disimpan di tabel notifications).
18    fn to_database(&self) -> Option<Value> {
19        None
20    }
21
22    /// Representasi WhatsApp.
23    fn to_whatsapp(&self) -> Option<String> {
24        None
25    }
26}
27
28/// Notify — Helper untuk mengirim notifikasi ke user.
29pub struct Notify;
30
31impl Notify {
32    /// Kirim notifikasi ke email user.
33    pub async fn send<N>(
34        req: &crate::core::request::Request,
35        to_email: &str,
36        notification: N,
37    ) -> Result<(), String>
38    where
39        N: Notification,
40    {
41        if let Some((template, data)) = notification.to_mail() {
42            req.mail()
43                .to(to_email)
44                .subject(&notification.subject())
45                .send(&template, data)
46                .await?;
47        }
48        Ok(())
49    }
50}