backbone_integrations/domain/entity/
integration_connector.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::ConnectorKind;
7use super::ConnectorDirection;
8use super::ConnectorStatus;
9use super::AuditMetadata;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(transparent)]
14pub struct IntegrationConnectorId(pub Uuid);
15
16impl IntegrationConnectorId {
17 pub fn new(id: Uuid) -> Self { Self(id) }
18 pub fn generate() -> Self { Self(Uuid::new_v4()) }
19 pub fn into_inner(self) -> Uuid { self.0 }
20}
21
22impl std::fmt::Display for IntegrationConnectorId {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)
25 }
26}
27
28impl std::str::FromStr for IntegrationConnectorId {
29 type Err = uuid::Error;
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 Ok(Self(Uuid::parse_str(s)?))
32 }
33}
34
35impl From<Uuid> for IntegrationConnectorId {
36 fn from(id: Uuid) -> Self { Self(id) }
37}
38
39impl From<IntegrationConnectorId> for Uuid {
40 fn from(id: IntegrationConnectorId) -> Self { id.0 }
41}
42
43impl AsRef<Uuid> for IntegrationConnectorId {
44 fn as_ref(&self) -> &Uuid { &self.0 }
45}
46
47impl std::ops::Deref for IntegrationConnectorId {
48 type Target = Uuid;
49 fn deref(&self) -> &Self::Target { &self.0 }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
53pub struct IntegrationConnector {
54 pub id: Uuid,
55 pub provider: String,
56 pub kind: ConnectorKind,
57 pub direction: ConnectorDirection,
58 pub status: ConnectorStatus,
59 #[serde(default)]
60 #[sqlx(json)]
61 pub metadata: AuditMetadata,
62}
63
64impl IntegrationConnector {
65 pub fn builder() -> IntegrationConnectorBuilder {
67 <IntegrationConnectorBuilder as Default>::default()
68 }
69
70 pub fn new(provider: String, kind: ConnectorKind, direction: ConnectorDirection, status: ConnectorStatus) -> Self {
72 Self {
73 id: Uuid::new_v4(),
74 provider,
75 kind,
76 direction,
77 status,
78 metadata: AuditMetadata::default(),
79 }
80 }
81
82 pub fn id(&self) -> &Uuid {
84 &self.id
85 }
86
87 pub fn typed_id(&self) -> IntegrationConnectorId {
89 IntegrationConnectorId(self.id)
90 }
91
92 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
94 self.metadata.created_at.as_ref()
95 }
96
97 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
99 self.metadata.updated_at.as_ref()
100 }
101
102 pub fn is_deleted(&self) -> bool {
104 self.metadata.deleted_at.is_some()
105 }
106
107 pub fn is_active(&self) -> bool {
109 self.metadata.deleted_at.is_none()
110 }
111
112 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
114 self.metadata.deleted_at.as_ref()
115 }
116
117 pub fn created_by(&self) -> Option<&Uuid> {
119 self.metadata.created_by.as_ref()
120 }
121
122 pub fn updated_by(&self) -> Option<&Uuid> {
124 self.metadata.updated_by.as_ref()
125 }
126
127 pub fn deleted_by(&self) -> Option<&Uuid> {
129 self.metadata.deleted_by.as_ref()
130 }
131
132 pub fn status(&self) -> &ConnectorStatus {
134 &self.status
135 }
136
137
138 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
144 for (key, value) in fields {
145 match key.as_str() {
146 "provider" => {
147 if let Ok(v) = serde_json::from_value(value) { self.provider = v; }
148 }
149 "kind" => {
150 if let Ok(v) = serde_json::from_value(value) { self.kind = v; }
151 }
152 "direction" => {
153 if let Ok(v) = serde_json::from_value(value) { self.direction = v; }
154 }
155 "status" => {
156 if let Ok(v) = serde_json::from_value(value) { self.status = v; }
157 }
158 _ => {} }
160 }
161 }
162
163}
164
165impl super::Entity for IntegrationConnector {
166 type Id = Uuid;
167
168 fn entity_id(&self) -> &Self::Id {
169 &self.id
170 }
171
172 fn entity_type() -> &'static str {
173 "IntegrationConnector"
174 }
175}
176
177impl backbone_core::PersistentEntity for IntegrationConnector {
178 fn entity_id(&self) -> String {
179 self.id.to_string()
180 }
181 fn set_entity_id(&mut self, id: String) {
182 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
183 self.id = uuid;
184 }
185 }
186 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
187 self.metadata.created_at
188 }
189 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
190 self.metadata.created_at = Some(ts);
191 }
192 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
193 self.metadata.updated_at
194 }
195 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
196 self.metadata.updated_at = Some(ts);
197 }
198 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
199 self.metadata.deleted_at
200 }
201 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
202 self.metadata.deleted_at = ts;
203 }
204}
205
206impl backbone_orm::EntityRepoMeta for IntegrationConnector {
207 fn column_types() -> std::collections::HashMap<String, String> {
208 let mut m = std::collections::HashMap::new();
209 m.insert("id".to_string(), "uuid".to_string());
210 m.insert("kind".to_string(), "connector_kind".to_string());
211 m.insert("direction".to_string(), "connector_direction".to_string());
212 m.insert("status".to_string(), "connector_status".to_string());
213 m
214 }
215 fn search_fields() -> &'static [&'static str] {
216 &["provider"]
217 }
218}
219
220#[derive(Debug, Clone, Default)]
225pub struct IntegrationConnectorBuilder {
226 provider: Option<String>,
227 kind: Option<ConnectorKind>,
228 direction: Option<ConnectorDirection>,
229 status: Option<ConnectorStatus>,
230}
231
232impl IntegrationConnectorBuilder {
233 pub fn provider(mut self, value: String) -> Self {
235 self.provider = Some(value);
236 self
237 }
238
239 pub fn kind(mut self, value: ConnectorKind) -> Self {
241 self.kind = Some(value);
242 self
243 }
244
245 pub fn direction(mut self, value: ConnectorDirection) -> Self {
247 self.direction = Some(value);
248 self
249 }
250
251 pub fn status(mut self, value: ConnectorStatus) -> Self {
253 self.status = Some(value);
254 self
255 }
256
257 pub fn build(self) -> Result<IntegrationConnector, String> {
261 let provider = self.provider.ok_or_else(|| "provider is required".to_string())?;
262 let kind = self.kind.ok_or_else(|| "kind is required".to_string())?;
263
264 Ok(IntegrationConnector {
265 id: Uuid::new_v4(),
266 provider,
267 kind,
268 direction: self.direction.unwrap_or_default(),
269 status: self.status.unwrap_or_default(),
270 metadata: AuditMetadata::default(),
271 })
272 }
273}