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 LegacyLoginAlert,
37 MigrationVerification,
38}
39
40#[derive(Debug, Clone, FromRow)]
41pub struct QueuedComms {
42 pub id: Uuid,
43 pub user_id: Uuid,
44 pub channel: CommsChannel,
45 pub comms_type: CommsType,
46 pub status: CommsStatus,
47 pub recipient: String,
48 pub subject: Option<String>,
49 pub body: String,
50 pub metadata: Option<serde_json::Value>,
51 pub attempts: i32,
52 pub max_attempts: i32,
53 pub last_error: Option<String>,
54 pub created_at: DateTime<Utc>,
55 pub updated_at: DateTime<Utc>,
56 pub scheduled_for: DateTime<Utc>,
57 pub processed_at: Option<DateTime<Utc>>,
58}
59
60pub struct NewComms {
61 pub user_id: Uuid,
62 pub channel: CommsChannel,
63 pub comms_type: CommsType,
64 pub recipient: String,
65 pub subject: Option<String>,
66 pub body: String,
67 pub metadata: Option<serde_json::Value>,
68}
69
70impl NewComms {
71 pub fn new(
72 user_id: Uuid,
73 channel: CommsChannel,
74 comms_type: CommsType,
75 recipient: String,
76 subject: Option<String>,
77 body: String,
78 ) -> Self {
79 Self {
80 user_id,
81 channel,
82 comms_type,
83 recipient,
84 subject,
85 body,
86 metadata: None,
87 }
88 }
89
90 pub fn email(
91 user_id: Uuid,
92 comms_type: CommsType,
93 recipient: String,
94 subject: String,
95 body: String,
96 ) -> Self {
97 Self::new(
98 user_id,
99 CommsChannel::Email,
100 comms_type,
101 recipient,
102 Some(subject),
103 body,
104 )
105 }
106}