use crate::channel::Channel;
use crate::channels::{
DatabaseMessage, InAppMessage, MailMessage, PushMessage, SlackMessage, SmsMessage,
WhatsAppMessage,
};
pub trait Notification: Send + Sync {
fn via(&self) -> Vec<Channel>;
fn to_mail(&self) -> Option<MailMessage> {
None
}
fn to_database(&self) -> Option<DatabaseMessage> {
None
}
fn to_slack(&self) -> Option<SlackMessage> {
None
}
fn to_whatsapp(&self) -> Option<WhatsAppMessage> {
None
}
fn to_in_app(&self) -> Option<InAppMessage> {
None
}
fn to_sms(&self) -> Option<SmsMessage> {
None
}
fn to_push(&self) -> Option<PushMessage> {
None
}
fn notification_type(&self) -> &'static str {
std::any::type_name::<Self>()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestNotification;
impl Notification for TestNotification {
fn via(&self) -> Vec<Channel> {
vec![Channel::Mail, Channel::Database]
}
fn to_mail(&self) -> Option<MailMessage> {
Some(MailMessage::new().subject("Test").body("Test body"))
}
}
#[test]
fn test_notification_via() {
let notification = TestNotification;
let channels = notification.via();
assert_eq!(channels.len(), 2);
assert!(channels.contains(&Channel::Mail));
assert!(channels.contains(&Channel::Database));
}
#[test]
fn test_notification_to_mail() {
let notification = TestNotification;
let mail = notification.to_mail();
assert!(mail.is_some());
let mail = mail.unwrap();
assert_eq!(mail.subject, "Test");
}
#[test]
fn test_notification_to_database_default() {
let notification = TestNotification;
assert!(notification.to_database().is_none());
}
#[test]
fn test_notification_to_whatsapp_default() {
let notification = TestNotification;
assert!(notification.to_whatsapp().is_none());
}
#[test]
fn test_notification_to_in_app_default() {
let notification = TestNotification;
assert!(notification.to_in_app().is_none());
}
#[test]
fn test_notification_to_sms_default() {
let notification = TestNotification;
assert!(notification.to_sms().is_none());
}
#[test]
fn test_notification_to_push_default() {
let notification = TestNotification;
assert!(notification.to_push().is_none());
}
}