this repo has no description
1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, sqlx::Type, Serialize, Deserialize)]
7#[sqlx(type_name = "notification_channel", rename_all = "lowercase")]
8pub enum NotificationChannel {
9 Email,
10 Discord,
11 Telegram,
12 Signal,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type, Serialize, Deserialize)]
16#[sqlx(type_name = "notification_status", rename_all = "lowercase")]
17pub enum NotificationStatus {
18 Pending,
19 Processing,
20 Sent,
21 Failed,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type, Serialize, Deserialize)]
25#[sqlx(type_name = "notification_type", rename_all = "snake_case")]
26pub enum NotificationType {
27 Welcome,
28 EmailVerification,
29 PasswordReset,
30 EmailUpdate,
31 AccountDeletion,
32 AdminEmail,
33 PlcOperation,
34}
35
36#[derive(Debug, Clone, FromRow)]
37pub struct QueuedNotification {
38 pub id: Uuid,
39 pub user_id: Uuid,
40 pub channel: NotificationChannel,
41 pub notification_type: NotificationType,
42 pub status: NotificationStatus,
43 pub recipient: String,
44 pub subject: Option<String>,
45 pub body: String,
46 pub metadata: Option<serde_json::Value>,
47 pub attempts: i32,
48 pub max_attempts: i32,
49 pub last_error: Option<String>,
50 pub created_at: DateTime<Utc>,
51 pub updated_at: DateTime<Utc>,
52 pub scheduled_for: DateTime<Utc>,
53 pub processed_at: Option<DateTime<Utc>>,
54}
55
56pub struct NewNotification {
57 pub user_id: Uuid,
58 pub channel: NotificationChannel,
59 pub notification_type: NotificationType,
60 pub recipient: String,
61 pub subject: Option<String>,
62 pub body: String,
63 pub metadata: Option<serde_json::Value>,
64}
65
66impl NewNotification {
67 pub fn new(
68 user_id: Uuid,
69 channel: NotificationChannel,
70 notification_type: NotificationType,
71 recipient: String,
72 subject: Option<String>,
73 body: String,
74 ) -> Self {
75 Self {
76 user_id,
77 channel,
78 notification_type,
79 recipient,
80 subject,
81 body,
82 metadata: None,
83 }
84 }
85
86 pub fn email(
87 user_id: Uuid,
88 notification_type: NotificationType,
89 recipient: String,
90 subject: String,
91 body: String,
92 ) -> Self {
93 Self::new(
94 user_id,
95 NotificationChannel::Email,
96 notification_type,
97 recipient,
98 Some(subject),
99 body,
100 )
101 }
102}