1use crate::object::{EventId, EventType, IdempotencyToken, Payload};
2use serde::Serialize;
3use std::fmt::Debug;
4use time::OffsetDateTime;
5
6#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
7pub struct Event<PT> {
8 pub id: EventId,
9 pub idempotency_token: Option<IdempotencyToken>,
10 pub event_type: EventType,
11 #[cfg_attr(feature = "sqlx", sqlx(json))]
12 pub payload: Payload<PT>,
13 pub created_at: OffsetDateTime,
14 pub locked_until: OffsetDateTime,
15 pub status: EventStatus,
16}
17impl<PT> Event<PT>
18where
19 PT: Debug + Clone + Serialize,
20{
21 pub fn new(
22 event_type: EventType,
23 payload: Payload<PT>,
24 idempotency_token: Option<IdempotencyToken>,
25 ) -> Self {
26 Self {
27 id: EventId::default(),
28 idempotency_token,
29 event_type,
30 payload,
31 created_at: OffsetDateTime::now_utc(),
32 locked_until: OffsetDateTime::UNIX_EPOCH,
33 status: EventStatus::Pending,
34 }
35 }
36}
37
38#[cfg_attr(feature = "sqlx", derive(Debug, sqlx::Type))]
39#[cfg_attr(
40 feature = "sqlx",
41 sqlx(type_name = "status", rename_all = "PascalCase")
42)]
43#[derive(Clone, PartialEq)]
44pub enum EventStatus {
45 Pending,
47 Processing,
48 Sent,
49}