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