use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum ProviderKind {
Smtp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum SmtpEncryption {
StartTls,
Tls,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum MailLogStatus {
Sent,
Failed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
pub struct MailProviderView {
pub id: Uuid,
pub name: String,
pub kind: ProviderKind,
pub active: bool,
pub host: String,
pub port: i32,
pub username: Option<String>,
pub password_set: bool,
pub encryption: SmtpEncryption,
pub from_email: String,
pub from_name: Option<String>,
pub reply_to: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct CreateProviderReq {
pub name: String,
pub host: String,
pub port: i32,
pub encryption: SmtpEncryption,
pub from_email: String,
#[serde(default)]
pub from_name: Option<String>,
#[serde(default)]
pub reply_to: Option<String>,
#[serde(default)]
pub username: Option<String>,
#[serde(default)]
pub password: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema)]
pub struct UpdateProviderReq {
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub host: Option<String>,
#[serde(default)]
pub port: Option<i32>,
#[serde(default)]
pub encryption: Option<SmtpEncryption>,
#[serde(default)]
pub from_email: Option<String>,
#[serde(default)]
pub from_name: Option<Option<String>>,
#[serde(default)]
pub reply_to: Option<Option<String>>,
#[serde(default)]
pub username: Option<Option<String>>,
#[serde(default)]
pub password: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
pub struct MailTemplateView {
pub id: Uuid,
pub event_name: String,
pub locale: String,
pub subject: String,
pub html_body: String,
pub text_body: String,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, ToSchema)]
pub struct UpdateTemplateReq {
#[serde(default)]
pub subject: Option<String>,
#[serde(default)]
pub html_body: Option<String>,
#[serde(default)]
pub text_body: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PreviewReq {
pub sample: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PreviewResp {
pub subject: String,
pub html_body: String,
pub text_body: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct MailLogView {
pub id: Uuid,
pub to: String,
pub template_id: Option<Uuid>,
pub provider_id: Option<Uuid>,
pub status: MailLogStatus,
pub error: Option<String>,
pub sent_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct MailStatusResp {
pub transport: String,
pub fallback_mode: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct TouchedResp {
pub touched: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct TestSentResp {
pub to: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn enum_wire_strings_match_entity_string_values() {
assert_eq!(serde_json::to_value(ProviderKind::Smtp).unwrap(), "smtp");
assert_eq!(
serde_json::to_value(SmtpEncryption::StartTls).unwrap(),
"starttls"
);
assert_eq!(serde_json::to_value(SmtpEncryption::Tls).unwrap(), "tls");
assert_eq!(serde_json::to_value(SmtpEncryption::None).unwrap(), "none");
assert_eq!(serde_json::to_value(MailLogStatus::Sent).unwrap(), "sent");
assert_eq!(
serde_json::to_value(MailLogStatus::Failed).unwrap(),
"failed"
);
let e: SmtpEncryption = serde_json::from_value(serde_json::json!("starttls")).unwrap();
assert_eq!(e, SmtpEncryption::StartTls);
}
}