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}
209
210impl super::Entity for IntegrationEvent {
211 type Id = Uuid;
212
213 fn entity_id(&self) -> &Self::Id {
214 &self.id
215 }
216
217 fn entity_type() -> &'static str {
218 "IntegrationEvent"
219 }
220}
221
222impl backbone_core::PersistentEntity for IntegrationEvent {
223 fn entity_id(&self) -> String {
224 self.id.to_string()
225 }
226 fn set_entity_id(&mut self, id: String) {
227 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
228 self.id = uuid;
229 }
230 }
231 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
232 self.metadata.created_at
233 }
234 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
235 self.metadata.created_at = Some(ts);
236 }
237 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
238 self.metadata.updated_at
239 }
240 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
241 self.metadata.updated_at = Some(ts);
242 }
243 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
244 self.metadata.deleted_at
245 }
246 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
247 self.metadata.deleted_at = ts;
248 }
249}
250
251impl backbone_orm::EntityRepoMeta for IntegrationEvent {
252 fn column_types() -> std::collections::HashMap<String, String> {
253 let mut m = std::collections::HashMap::new();
254 m.insert("id".to_string(), "uuid".to_string());
255 m.insert("connector_id".to_string(), "uuid".to_string());
256 m.insert("mapped_ref_id".to_string(), "uuid".to_string());
257 m.insert("status".to_string(), "integration_status".to_string());
258 m
259 }
260 fn search_fields() -> &'static [&'static str] {
261 &["event_type", "external_id", "business_key", "payload"]
262 }
263 fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
264 &[("connector", "integration_connectors", "connectorId")]
265 }
266}
267
268#[derive(Debug, Clone, Default)]
273pub struct IntegrationEventBuilder {
274 connector_id: Option<Uuid>,
275 event_type: Option<String>,
276 external_id: Option<String>,
277 business_key: Option<String>,
278 status: Option<IntegrationStatus>,
279 payload: Option<String>,
280 mapped_ref_type: Option<String>,
281 mapped_ref_id: Option<Uuid>,
282 error_detail: Option<String>,
283}
284
285impl IntegrationEventBuilder {
286 pub fn connector_id(mut self, value: Uuid) -> Self {
288 self.connector_id = Some(value);
289 self
290 }
291
292 pub fn event_type(mut self, value: String) -> Self {
294 self.event_type = Some(value);
295 self
296 }
297
298 pub fn external_id(mut self, value: String) -> Self {
300 self.external_id = Some(value);
301 self
302 }
303
304 pub fn business_key(mut self, value: String) -> Self {
306 self.business_key = Some(value);
307 self
308 }
309
310 pub fn status(mut self, value: IntegrationStatus) -> Self {
312 self.status = Some(value);
313 self
314 }
315
316 pub fn payload(mut self, value: String) -> Self {
318 self.payload = Some(value);
319 self
320 }
321
322 pub fn mapped_ref_type(mut self, value: String) -> Self {
324 self.mapped_ref_type = Some(value);
325 self
326 }
327
328 pub fn mapped_ref_id(mut self, value: Uuid) -> Self {
330 self.mapped_ref_id = Some(value);
331 self
332 }
333
334 pub fn error_detail(mut self, value: String) -> Self {
336 self.error_detail = Some(value);
337 self
338 }
339
340 pub fn build(self) -> Result<IntegrationEvent, String> {
344 let connector_id = self.connector_id.ok_or_else(|| "connector_id is required".to_string())?;
345 let event_type = self.event_type.ok_or_else(|| "event_type is required".to_string())?;
346 let external_id = self.external_id.ok_or_else(|| "external_id is required".to_string())?;
347 let business_key = self.business_key.ok_or_else(|| "business_key is required".to_string())?;
348 let payload = self.payload.ok_or_else(|| "payload is required".to_string())?;
349
350 Ok(IntegrationEvent {
351 id: Uuid::new_v4(),
352 connector_id,
353 event_type,
354 external_id,
355 business_key,
356 status: self.status.unwrap_or_default(),
357 payload,
358 mapped_ref_type: self.mapped_ref_type,
359 mapped_ref_id: self.mapped_ref_id,
360 error_detail: self.error_detail,
361 metadata: AuditMetadata::default(),
362 })
363 }
364}