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 }
166
167impl super::Entity for IntegrationConnector {
168 type Id = Uuid;
169
170 fn entity_id(&self) -> &Self::Id {
171 &self.id
172 }
173
174 fn entity_type() -> &'static str {
175 "IntegrationConnector"
176 }
177}
178
179impl backbone_core::PersistentEntity for IntegrationConnector {
180 fn entity_id(&self) -> String {
181 self.id.to_string()
182 }
183 fn set_entity_id(&mut self, id: String) {
184 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
185 self.id = uuid;
186 }
187 }
188 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
189 self.metadata.created_at
190 }
191 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
192 self.metadata.created_at = Some(ts);
193 }
194 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
195 self.metadata.updated_at
196 }
197 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
198 self.metadata.updated_at = Some(ts);
199 }
200 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
201 self.metadata.deleted_at
202 }
203 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
204 self.metadata.deleted_at = ts;
205 }
206}
207
208impl backbone_orm::EntityRepoMeta for IntegrationConnector {
209 fn column_types() -> std::collections::HashMap<String, String> {
210 let mut m = std::collections::HashMap::new();
211 m.insert("id".to_string(), "uuid".to_string());
212 m.insert("kind".to_string(), "connector_kind".to_string());
213 m.insert("direction".to_string(), "connector_direction".to_string());
214 m.insert("status".to_string(), "connector_status".to_string());
215 m
216 }
217 fn search_fields() -> &'static [&'static str] {
218 &["provider"]
219 }
220}
221
222#[derive(Debug, Clone, Default)]
227pub struct IntegrationConnectorBuilder {
228 provider: Option<String>,
229 kind: Option<ConnectorKind>,
230 direction: Option<ConnectorDirection>,
231 status: Option<ConnectorStatus>,
232}
233
234impl IntegrationConnectorBuilder {
235 pub fn provider(mut self, value: String) -> Self {
237 self.provider = Some(value);
238 self
239 }
240
241 pub fn kind(mut self, value: ConnectorKind) -> Self {
243 self.kind = Some(value);
244 self
245 }
246
247 pub fn direction(mut self, value: ConnectorDirection) -> Self {
249 self.direction = Some(value);
250 self
251 }
252
253 pub fn status(mut self, value: ConnectorStatus) -> Self {
255 self.status = Some(value);
256 self
257 }
258
259 pub fn build(self) -> Result<IntegrationConnector, String> {
263 let provider = self.provider.ok_or_else(|| "provider is required".to_string())?;
264 let kind = self.kind.ok_or_else(|| "kind is required".to_string())?;
265
266 Ok(IntegrationConnector {
267 id: Uuid::new_v4(),
268 provider,
269 kind,
270 direction: self.direction.unwrap_or_default(),
271 status: self.status.unwrap_or_default(),
272 metadata: AuditMetadata::default(),
273 })
274 }
275}