use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Notification {
pub id: String,
pub title: String,
pub content: String,
#[serde(rename = "type")]
pub r#type: String,
#[serde(rename = "isRead")]
pub is_read: bool,
#[serde(rename = "createdAt")]
pub created_at: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NotificationList {
pub list: Vec<Notification>,
#[serde(rename = "unreadCount")]
pub unread_count: i64,
pub total: i64,
pub page: i64,
#[serde(rename = "pageSize")]
pub page_size: i64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NotificationUnreadCount {
#[serde(rename = "unreadCount")]
pub unread_count: i64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NotificationPreference {
#[serde(rename = "typeCode")]
pub type_code: String,
#[serde(rename = "channelInApp")]
pub channel_in_app: bool,
#[serde(rename = "channelEmail")]
pub channel_email: bool,
#[serde(rename = "channelSms")]
pub channel_sms: bool,
#[serde(rename = "channelPush")]
pub channel_push: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeviceRegistration {
pub platform: String,
pub token: String,
#[serde(rename = "appVersion")]
pub app_version: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WSEvent {
#[serde(rename = "type")]
pub r#type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub topic: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
#[serde(rename = "connId", default, skip_serializing_if = "Option::is_none")]
pub conn_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub timestamp: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
pub fn parse_notification_event(ev: &WSEvent) -> Option<Notification> {
if ev.r#type != "event" || ev.topic.as_deref() != Some("system") {
return None;
}
let data = ev.data.as_ref()?;
let n: Notification = if let Some(s) = data.as_str() {
serde_json::from_str(s).ok()?
} else {
serde_json::from_value(data.clone()).ok()?
};
if n.id.is_empty() {
return None;
}
Some(n)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_notification_event_object() {
let ev = WSEvent {
r#type: "event".to_string(),
topic: Some("system".to_string()),
data: Some(serde_json::json!({
"id": "n1", "title": "T", "content": "C", "type": "system",
"isRead": false, "createdAt": "2026-06-19T00:00:00Z"
})),
..Default::default()
};
let n = parse_notification_event(&ev).unwrap();
assert_eq!(n.id, "n1");
assert_eq!(n.r#type, "system");
}
#[test]
fn parse_notification_event_string_data() {
let ev = WSEvent {
r#type: "event".to_string(),
topic: Some("system".to_string()),
data: Some(serde_json::Value::String(
r#"{"id":"n2","title":"T","content":"C","type":"billing","isRead":true,"createdAt":"x"}"#
.to_string(),
)),
..Default::default()
};
let n = parse_notification_event(&ev).unwrap();
assert_eq!(n.id, "n2");
assert!(n.is_read);
}
#[test]
fn parse_notification_event_non_system_returns_none() {
let ev = WSEvent {
r#type: "event".to_string(),
topic: Some("billing".to_string()),
data: Some(serde_json::json!({"id":"x"})),
..Default::default()
};
assert!(parse_notification_event(&ev).is_none());
let ev2 = WSEvent {
r#type: "welcome".to_string(),
topic: Some("system".to_string()),
..Default::default()
};
assert!(parse_notification_event(&ev2).is_none());
}
}