backbone_integrations/domain/entity/
integration_event.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::IntegrationStatus;
7use super::AuditMetadata;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct IntegrationEventId(pub Uuid);
13
14impl IntegrationEventId {
15 pub fn new(id: Uuid) -> Self { Self(id) }
16 pub fn generate() -> Self { Self(Uuid::new_v4()) }
17 pub fn into_inner(self) -> Uuid { self.0 }
18}
19
20impl std::fmt::Display for IntegrationEventId {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 write!(f, "{}", self.0)
23 }
24}
25
26impl std::str::FromStr for IntegrationEventId {
27 type Err = uuid::Error;
28 fn from_str(s: &str) -> Result<Self, Self::Err> {
29 Ok(Self(Uuid::parse_str(s)?))
30 }
31}
32
33impl From<Uuid> for IntegrationEventId {
34 fn from(id: Uuid) -> Self { Self(id) }
35}
36
37impl From<IntegrationEventId> for Uuid {
38 fn from(id: IntegrationEventId) -> Self { id.0 }
39}
40
41impl AsRef<Uuid> for IntegrationEventId {
42 fn as_ref(&self) -> &Uuid { &self.0 }
43}
44
45impl std::ops::Deref for IntegrationEventId {
46 type Target = Uuid;
47 fn deref(&self) -> &Self::Target { &self.0 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
51pub struct IntegrationEvent {
52 pub id: Uuid,
53 pub connector_id: Uuid,
54 pub event_type: String,
55 pub external_id: String,
56 pub business_key: String,
57 pub status: IntegrationStatus,
58 pub payload: String,
59 pub mapped_ref_type: Option<String>,
60 pub mapped_ref_id: Option<Uuid>,
61 pub error_detail: Option<String>,
62 #[serde(default)]
63 #[sqlx(json)]
64 pub metadata: AuditMetadata,
65}
66
67impl IntegrationEvent {
68 pub fn builder() -> IntegrationEventBuilder {
70 <IntegrationEventBuilder as Default>::default()
71 }
72
73 pub fn new(connector_id: Uuid, event_type: String, external_id: String, business_key: String, status: IntegrationStatus, payload: String) -> Self {
75 Self {
76 id: Uuid::new_v4(),
77 connector_id,
78 event_type,
79 external_id,
80 business_key,
81 status,
82 payload,
83 mapped_ref_type: None,
84 mapped_ref_id: None,
85 error_detail: None,
86 metadata: AuditMetadata::default(),
87 }
88 }
89
90 pub fn id(&self) -> &Uuid {
92 &self.id
93 }
94
95 pub fn typed_id(&self) -> IntegrationEventId {
97 IntegrationEventId(self.id)
98 }
99
100 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
102 self.metadata.created_at.as_ref()
103 }
104
105 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
107 self.metadata.updated_at.as_ref()
108 }
109
110 pub fn is_deleted(&self) -> bool {
112 self.metadata.deleted_at.is_some()
113 }
114
115 pub fn is_active(&self) -> bool {
117 self.metadata.deleted_at.is_none()
118 }
119
120 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
122 self.metadata.deleted_at.as_ref()
123 }
124
125 pub fn created_by(&self) -> Option<&Uuid> {
127 self.metadata.created_by.as_ref()
128 }
129
130 pub fn updated_by(&self) -> Option<&Uuid> {
132 self.metadata.updated_by.as_ref()
133 }
134
135 pub fn deleted_by(&self) -> Option<&Uuid> {
137 self.metadata.deleted_by.as_ref()
138 }
139
140 pub fn status(&self) -> &IntegrationStatus {
142 &self.status
143 }
144
145
146 pub fn with_mapped_ref_type(mut self, value: String) -> Self {
152 self.mapped_ref_type = Some(value);
153 self
154 }
155
156 pub fn with_mapped_ref_id(mut self, value: Uuid) -> Self {
158 self.mapped_ref_id = Some(value);
159 self
160 }
161
162 pub fn with_error_detail(mut self, value: String) -> Self {
164 self.error_detail = Some(value);
165 self
166 }
167
168 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
174 for (key, value) in fields {
175 match key.as_str() {
176 "connector_id" => {
177 if let Ok(v) = serde_json::from_value(value) { self.connector_id = v; }
178 }
179 "event_type" => {
180 if let Ok(v) = serde_json::from_value(value) { self.event_type = v; }
181 }
182 "external_id" => {
183 if let Ok(v) = serde_json::from_value(value) { self.external_id = v; }
184 }
185 "business_key" => {
186 if let Ok(v) = serde_json::from_value(value) { self.business_key = v; }
187 }
188 "status" => {
189 if let Ok(v) = serde_json::from_value(value) { self.status = v; }
190 }
191 "payload" => {
192 if let Ok(v) = serde_json::from_value(value) { self.payload = v; }
193 }
194 "mapped_ref_type" => {
195 if let Ok(v) = serde_json::from_value(value) { self.mapped_ref_type = v; }
196 }
197 "mapped_ref_id" => {
198 if let Ok(v) = serde_json::from_value(value) { self.mapped_ref_id = v; }
199 }
200 "error_detail" => {
201 if let Ok(v) = serde_json::from_value(value) { self.error_detail = v; }
202 }
203 _ => {} }
205 }
206 }
207
208 }
211
212impl super::Entity for IntegrationEvent {
213 type Id = Uuid;
214
215 fn entity_id(&self) -> &Self::Id {
216 &self.id
217 }
218
219 fn entity_type() -> &'static str {
220 "IntegrationEvent"
221 }
222}
223
224impl backbone_core::PersistentEntity for IntegrationEvent {
225 fn entity_id(&self) -> String {
226 self.id.to_string()
227 }
228 fn set_entity_id(&mut self, id: String) {
229 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
230 self.id = uuid;
231 }
232 }
233 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
234 self.metadata.created_at
235 }
236 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
237 self.metadata.created_at = Some(ts);
238 }
239 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
240 self.metadata.updated_at
241 }
242 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
243 self.metadata.updated_at = Some(ts);
244 }
245 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
246 self.metadata.deleted_at
247 }
248 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
249 self.metadata.deleted_at = ts;
250 }
251}
252
253impl backbone_orm::EntityRepoMeta for IntegrationEvent {
254 fn column_types() -> std::collections::HashMap<String, String> {
255 let mut m = std::collections::HashMap::new();
256 m.insert("id".to_string(), "uuid".to_string());
257 m.insert("connector_id".to_string(), "uuid".to_string());
258 m.insert("mapped_ref_id".to_string(), "uuid".to_string());
259 m.insert("status".to_string(), "integration_status".to_string());
260 m
261 }
262 fn search_fields() -> &'static [&'static str] {
263 &["event_type", "external_id", "business_key", "payload"]
264 }
265}
266
267#[derive(Debug, Clone, Default)]
272pub struct IntegrationEventBuilder {
273 connector_id: Option<Uuid>,
274 event_type: Option<String>,
275 external_id: Option<String>,
276 business_key: Option<String>,
277 status: Option<IntegrationStatus>,
278 payload: Option<String>,
279 mapped_ref_type: Option<String>,
280 mapped_ref_id: Option<Uuid>,
281 error_detail: Option<String>,
282}
283
284impl IntegrationEventBuilder {
285 pub fn connector_id(mut self, value: Uuid) -> Self {
287 self.connector_id = Some(value);
288 self
289 }
290
291 pub fn event_type(mut self, value: String) -> Self {
293 self.event_type = Some(value);
294 self
295 }
296
297 pub fn external_id(mut self, value: String) -> Self {
299 self.external_id = Some(value);
300 self
301 }
302
303 pub fn business_key(mut self, value: String) -> Self {
305 self.business_key = Some(value);
306 self
307 }
308
309 pub fn status(mut self, value: IntegrationStatus) -> Self {
311 self.status = Some(value);
312 self
313 }
314
315 pub fn payload(mut self, value: String) -> Self {
317 self.payload = Some(value);
318 self
319 }
320
321 pub fn mapped_ref_type(mut self, value: String) -> Self {
323 self.mapped_ref_type = Some(value);
324 self
325 }
326
327 pub fn mapped_ref_id(mut self, value: Uuid) -> Self {
329 self.mapped_ref_id = Some(value);
330 self
331 }
332
333 pub fn error_detail(mut self, value: String) -> Self {
335 self.error_detail = Some(value);
336 self
337 }
338
339 pub fn build(self) -> Result<IntegrationEvent, String> {
343 let connector_id = self.connector_id.ok_or_else(|| "connector_id is required".to_string())?;
344 let event_type = self.event_type.ok_or_else(|| "event_type is required".to_string())?;
345 let external_id = self.external_id.ok_or_else(|| "external_id is required".to_string())?;
346 let business_key = self.business_key.ok_or_else(|| "business_key is required".to_string())?;
347 let payload = self.payload.ok_or_else(|| "payload is required".to_string())?;
348
349 Ok(IntegrationEvent {
350 id: Uuid::new_v4(),
351 connector_id,
352 event_type,
353 external_id,
354 business_key,
355 status: self.status.unwrap_or_default(),
356 payload,
357 mapped_ref_type: self.mapped_ref_type,
358 mapped_ref_id: self.mapped_ref_id,
359 error_detail: self.error_detail,
360 metadata: AuditMetadata::default(),
361 })
362 }
363}