feat: implement event model for registration process with payload handling

This commit is contained in:
itsscb 2025-03-15 22:15:59 +01:00
parent 04de306091
commit 33cd5d0b5c
5 changed files with 121 additions and 2 deletions

View File

@ -14,3 +14,5 @@ tower-http = { version = "0.6.2", features = ["fs"] }
tracing = "0.1.41"
serde = { version = "1.0.203", features = ["derive"] }
lettre = { version = "0.11.15", default-features = false, features = ["builder", "smtp-transport", "ring", "rustls", "rustls-tls"] }
uuid = { version = "1.16.0", features = ["serde", "v4"] }
chrono = "0.4.38"

View File

@ -1,10 +1,12 @@
mod api;
mod model;
pub use model::config;
use crate::model::config::SMTPConfig;
use axum::Router;
pub use model::config;
pub use model::event;
pub use model::Event;
pub use model::EventPayload;
use tower_http::services::{ServeDir, ServeFile};
const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@ -1 +1,3 @@
pub mod config;
pub mod event;
pub use event::{Event, EventPayload};

59
src/model/event.rs Normal file
View File

@ -0,0 +1,59 @@
mod registration;
use chrono::{DateTime, Utc};
pub use registration::RegistrationEvent;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd)]
#[serde(bound = "T: DeserializeOwned")]
pub struct Event<T>
where
T: EventPayload,
{
id: uuid::Uuid,
timestamp: DateTime<Utc>,
version: u32,
domain: String,
payload: T,
}
impl<T> From<T> for Event<T>
where
T: EventPayload,
{
fn from(payload: T) -> Self {
Self {
id: uuid::Uuid::new_v4(),
timestamp: Utc::now(),
version: 1,
domain: payload.domain().to_string(),
payload,
}
}
}
impl<T> Event<T>
where
T: EventPayload,
{
pub const fn id(&self) -> &uuid::Uuid {
&self.id
}
pub const fn timestamp(&self) -> &DateTime<Utc> {
&self.timestamp
}
pub const fn version(&self) -> u32 {
self.version
}
pub const fn payload(&self) -> &T {
&self.payload
}
}
pub trait EventPayload: Debug + Clone + Serialize + DeserializeOwned + PartialEq + Eq {
fn domain(&self) -> &str;
}

View File

@ -0,0 +1,54 @@
use crate::model::event::EventPayload;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd)]
pub enum RegistrationEvent {
RegistrationRequestReceived {
email_address: String,
},
VerificationEmailSent {
email_address: String,
secret: String,
},
VerificationEmailFailed {
email_address: String,
error: String,
},
VerificationSucceeded {
email_address: String,
},
}
impl RegistrationEvent {
#[must_use]
pub const fn registration_request_received(email_address: String) -> Self {
Self::RegistrationRequestReceived { email_address }
}
#[must_use]
pub const fn verification_email_sent(email_address: String, secret: String) -> Self {
Self::VerificationEmailSent {
email_address,
secret,
}
}
#[must_use]
pub const fn verification_email_failed(email_address: String, error: String) -> Self {
Self::VerificationEmailFailed {
email_address,
error,
}
}
#[must_use]
pub const fn verification_succeeded(email_address: String) -> Self {
Self::VerificationSucceeded { email_address }
}
}
impl EventPayload for RegistrationEvent {
fn domain(&self) -> &'static str {
"registration"
}
}