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 = "comms_channel", rename_all = "lowercase")]
8pub enum CommsChannel {
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 = "comms_status", rename_all = "lowercase")]
17pub enum CommsStatus {
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 = "comms_type", rename_all = "snake_case")]
26pub enum CommsType {
27 Welcome,
28 EmailVerification,
29 PasswordReset,
30 EmailUpdate,
31 AccountDeletion,
32 AdminEmail,
33 PlcOperation,
34 TwoFactorCode,
35 PasskeyRecovery,
36}
37
38#[derive(Debug, Clone, FromRow)]
39pub struct QueuedComms {
40 pub id: Uuid,
41 pub user_id: Uuid,
42 pub channel: CommsChannel,
43 pub comms_type: CommsType,
44 pub status: CommsStatus,
45 pub recipient: String,
46 pub subject: Option<String>,
47 pub body: String,
48 pub metadata: Option<serde_json::Value>,
49 pub attempts: i32,
50 pub max_attempts: i32,
51 pub last_error: Option<String>,
52 pub created_at: DateTime<Utc>,
53 pub updated_at: DateTime<Utc>,
54 pub scheduled_for: DateTime<Utc>,
55 pub processed_at: Option<DateTime<Utc>>,
56}
57
58pub struct NewComms {
59 pub user_id: Uuid,
60 pub channel: CommsChannel,
61 pub comms_type: CommsType,
62 pub recipient: String,
63 pub subject: Option<String>,
64 pub body: String,
65 pub metadata: Option<serde_json::Value>,
66}
67
68impl NewComms {
69 pub fn new(
70 user_id: Uuid,
71 channel: CommsChannel,
72 comms_type: CommsType,
73 recipient: String,
74 subject: Option<String>,
75 body: String,
76 ) -> Self {
77 Self {
78 user_id,
79 channel,
80 comms_type,
81 recipient,
82 subject,
83 body,
84 metadata: None,
85 }
86 }
87
88 pub fn email(
89 user_id: Uuid,
90 comms_type: CommsType,
91 recipient: String,
92 subject: String,
93 body: String,
94 ) -> Self {
95 Self::new(
96 user_id,
97 CommsChannel::Email,
98 comms_type,
99 recipient,
100 Some(subject),
101 body,
102 )
103 }
104}