use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use super::EventIntervalUnit;
use super::EventIntervalKind;
use super::EventNotificationChannel;
use super::EventMailErrorKind;
use super::AuditMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct MailId(pub Uuid);
impl MailId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for MailId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for MailId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for MailId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<MailId> for Uuid {
fn from(id: MailId) -> Self { id.0 }
}
impl AsRef<Uuid> for MailId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for MailId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Mail {
pub id: Uuid,
pub event_id: Uuid,
pub interval_nbr: i32,
pub interval_unit: EventIntervalUnit,
pub interval_kind: EventIntervalKind,
pub notification_channel: EventNotificationChannel,
pub scheduled_date: Option<DateTime<Utc>>,
pub mail_done: bool,
pub last_registration_id: Option<Uuid>,
pub template_ref: Option<Uuid>,
pub template_kind: Option<String>,
pub error_datetime: Option<DateTime<Utc>>,
pub error_kind: Option<EventMailErrorKind>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl Mail {
pub fn builder() -> MailBuilder {
<MailBuilder as Default>::default()
}
pub fn new(event_id: Uuid, interval_nbr: i32, interval_unit: EventIntervalUnit, interval_kind: EventIntervalKind, notification_channel: EventNotificationChannel, mail_done: bool) -> Self {
Self {
id: Uuid::new_v4(),
event_id,
interval_nbr,
interval_unit,
interval_kind,
notification_channel,
scheduled_date: None,
mail_done,
last_registration_id: None,
template_ref: None,
template_kind: None,
error_datetime: None,
error_kind: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> MailId {
MailId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn with_scheduled_date(mut self, value: DateTime<Utc>) -> Self {
self.scheduled_date = Some(value);
self
}
pub fn with_last_registration_id(mut self, value: Uuid) -> Self {
self.last_registration_id = Some(value);
self
}
pub fn with_template_ref(mut self, value: Uuid) -> Self {
self.template_ref = Some(value);
self
}
pub fn with_template_kind(mut self, value: String) -> Self {
self.template_kind = Some(value);
self
}
pub fn with_error_datetime(mut self, value: DateTime<Utc>) -> Self {
self.error_datetime = Some(value);
self
}
pub fn with_error_kind(mut self, value: EventMailErrorKind) -> Self {
self.error_kind = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"event_id" => {
if let Ok(v) = serde_json::from_value(value) { self.event_id = v; }
}
"interval_nbr" => {
if let Ok(v) = serde_json::from_value(value) { self.interval_nbr = v; }
}
"interval_unit" => {
if let Ok(v) = serde_json::from_value(value) { self.interval_unit = v; }
}
"interval_kind" => {
if let Ok(v) = serde_json::from_value(value) { self.interval_kind = v; }
}
"notification_channel" => {
if let Ok(v) = serde_json::from_value(value) { self.notification_channel = v; }
}
"scheduled_date" => {
if let Ok(v) = serde_json::from_value(value) { self.scheduled_date = v; }
}
"mail_done" => {
if let Ok(v) = serde_json::from_value(value) { self.mail_done = v; }
}
"last_registration_id" => {
if let Ok(v) = serde_json::from_value(value) { self.last_registration_id = v; }
}
"template_ref" => {
if let Ok(v) = serde_json::from_value(value) { self.template_ref = v; }
}
"template_kind" => {
if let Ok(v) = serde_json::from_value(value) { self.template_kind = v; }
}
"error_datetime" => {
if let Ok(v) = serde_json::from_value(value) { self.error_datetime = v; }
}
"error_kind" => {
if let Ok(v) = serde_json::from_value(value) { self.error_kind = v; }
}
_ => {} }
}
}
}
impl super::Entity for Mail {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"Mail"
}
}
impl backbone_core::PersistentEntity for Mail {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for Mail {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("event_id".to_string(), "uuid".to_string());
m.insert("last_registration_id".to_string(), "uuid".to_string());
m.insert("interval_unit".to_string(), "event_interval_unit".to_string());
m.insert("interval_kind".to_string(), "event_interval_kind".to_string());
m.insert("notification_channel".to_string(), "event_notification_channel".to_string());
m.insert("error_kind".to_string(), "event_mail_error_kind".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&[]
}
fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
&[("event", "events", "eventId")]
}
}
#[derive(Debug, Clone, Default)]
pub struct MailBuilder {
event_id: Option<Uuid>,
interval_nbr: Option<i32>,
interval_unit: Option<EventIntervalUnit>,
interval_kind: Option<EventIntervalKind>,
notification_channel: Option<EventNotificationChannel>,
scheduled_date: Option<DateTime<Utc>>,
mail_done: Option<bool>,
last_registration_id: Option<Uuid>,
template_ref: Option<Uuid>,
template_kind: Option<String>,
error_datetime: Option<DateTime<Utc>>,
error_kind: Option<EventMailErrorKind>,
}
impl MailBuilder {
pub fn event_id(mut self, value: Uuid) -> Self {
self.event_id = Some(value);
self
}
pub fn interval_nbr(mut self, value: i32) -> Self {
self.interval_nbr = Some(value);
self
}
pub fn interval_unit(mut self, value: EventIntervalUnit) -> Self {
self.interval_unit = Some(value);
self
}
pub fn interval_kind(mut self, value: EventIntervalKind) -> Self {
self.interval_kind = Some(value);
self
}
pub fn notification_channel(mut self, value: EventNotificationChannel) -> Self {
self.notification_channel = Some(value);
self
}
pub fn scheduled_date(mut self, value: DateTime<Utc>) -> Self {
self.scheduled_date = Some(value);
self
}
pub fn mail_done(mut self, value: bool) -> Self {
self.mail_done = Some(value);
self
}
pub fn last_registration_id(mut self, value: Uuid) -> Self {
self.last_registration_id = Some(value);
self
}
pub fn template_ref(mut self, value: Uuid) -> Self {
self.template_ref = Some(value);
self
}
pub fn template_kind(mut self, value: String) -> Self {
self.template_kind = Some(value);
self
}
pub fn error_datetime(mut self, value: DateTime<Utc>) -> Self {
self.error_datetime = Some(value);
self
}
pub fn error_kind(mut self, value: EventMailErrorKind) -> Self {
self.error_kind = Some(value);
self
}
pub fn build(self) -> Result<Mail, String> {
let event_id = self.event_id.ok_or_else(|| "event_id is required".to_string())?;
Ok(Mail {
id: Uuid::new_v4(),
event_id,
interval_nbr: self.interval_nbr.unwrap_or(1),
interval_unit: self.interval_unit.unwrap_or(EventIntervalUnit::Now),
interval_kind: self.interval_kind.unwrap_or_default(),
notification_channel: self.notification_channel.unwrap_or_default(),
scheduled_date: self.scheduled_date,
mail_done: self.mail_done.unwrap_or(false),
last_registration_id: self.last_registration_id,
template_ref: self.template_ref,
template_kind: self.template_kind,
error_datetime: self.error_datetime,
error_kind: self.error_kind,
metadata: AuditMetadata::default(),
})
}
}