backbone_banking/domain/entity/
fx_gain_loss.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5use rust_decimal::Decimal;
6
7use super::FxDirection;
8use super::AuditMetadata;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct FxGainLossId(pub Uuid);
14
15impl FxGainLossId {
16 pub fn new(id: Uuid) -> Self { Self(id) }
17 pub fn generate() -> Self { Self(Uuid::new_v4()) }
18 pub fn into_inner(self) -> Uuid { self.0 }
19}
20
21impl std::fmt::Display for FxGainLossId {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 write!(f, "{}", self.0)
24 }
25}
26
27impl std::str::FromStr for FxGainLossId {
28 type Err = uuid::Error;
29 fn from_str(s: &str) -> Result<Self, Self::Err> {
30 Ok(Self(Uuid::parse_str(s)?))
31 }
32}
33
34impl From<Uuid> for FxGainLossId {
35 fn from(id: Uuid) -> Self { Self(id) }
36}
37
38impl From<FxGainLossId> for Uuid {
39 fn from(id: FxGainLossId) -> Self { id.0 }
40}
41
42impl AsRef<Uuid> for FxGainLossId {
43 fn as_ref(&self) -> &Uuid { &self.0 }
44}
45
46impl std::ops::Deref for FxGainLossId {
47 type Target = Uuid;
48 fn deref(&self) -> &Self::Target { &self.0 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
52pub struct FxGainLoss {
53 pub id: Uuid,
54 pub bank_clearance_id: Option<Uuid>,
55 pub matched_source_id: Uuid,
56 pub currency: String,
57 pub original_rate: Decimal,
58 pub realised_rate: Decimal,
59 pub base_amount_delta: Decimal,
60 pub direction: FxDirection,
61 pub fx_account_id: Uuid,
62 #[serde(default)]
63 #[sqlx(json)]
64 pub metadata: AuditMetadata,
65}
66
67impl FxGainLoss {
68 pub fn builder() -> FxGainLossBuilder {
70 <FxGainLossBuilder as Default>::default()
71 }
72
73 pub fn new(matched_source_id: Uuid, currency: String, original_rate: Decimal, realised_rate: Decimal, base_amount_delta: Decimal, direction: FxDirection, fx_account_id: Uuid) -> Self {
75 Self {
76 id: Uuid::new_v4(),
77 bank_clearance_id: None,
78 matched_source_id,
79 currency,
80 original_rate,
81 realised_rate,
82 base_amount_delta,
83 direction,
84 fx_account_id,
85 metadata: AuditMetadata::default(),
86 }
87 }
88
89 pub fn id(&self) -> &Uuid {
91 &self.id
92 }
93
94 pub fn typed_id(&self) -> FxGainLossId {
96 FxGainLossId(self.id)
97 }
98
99 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
101 self.metadata.created_at.as_ref()
102 }
103
104 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
106 self.metadata.updated_at.as_ref()
107 }
108
109 pub fn is_deleted(&self) -> bool {
111 self.metadata.deleted_at.is_some()
112 }
113
114 pub fn is_active(&self) -> bool {
116 self.metadata.deleted_at.is_none()
117 }
118
119 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
121 self.metadata.deleted_at.as_ref()
122 }
123
124 pub fn created_by(&self) -> Option<&Uuid> {
126 self.metadata.created_by.as_ref()
127 }
128
129 pub fn updated_by(&self) -> Option<&Uuid> {
131 self.metadata.updated_by.as_ref()
132 }
133
134 pub fn deleted_by(&self) -> Option<&Uuid> {
136 self.metadata.deleted_by.as_ref()
137 }
138
139
140 pub fn with_bank_clearance_id(mut self, value: Uuid) -> Self {
146 self.bank_clearance_id = Some(value);
147 self
148 }
149
150 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
156 for (key, value) in fields {
157 match key.as_str() {
158 "bank_clearance_id" => {
159 if let Ok(v) = serde_json::from_value(value) { self.bank_clearance_id = v; }
160 }
161 "matched_source_id" => {
162 if let Ok(v) = serde_json::from_value(value) { self.matched_source_id = v; }
163 }
164 "currency" => {
165 if let Ok(v) = serde_json::from_value(value) { self.currency = v; }
166 }
167 "original_rate" => {
168 if let Ok(v) = serde_json::from_value(value) { self.original_rate = v; }
169 }
170 "realised_rate" => {
171 if let Ok(v) = serde_json::from_value(value) { self.realised_rate = v; }
172 }
173 "base_amount_delta" => {
174 if let Ok(v) = serde_json::from_value(value) { self.base_amount_delta = v; }
175 }
176 "direction" => {
177 if let Ok(v) = serde_json::from_value(value) { self.direction = v; }
178 }
179 "fx_account_id" => {
180 if let Ok(v) = serde_json::from_value(value) { self.fx_account_id = v; }
181 }
182 _ => {} }
184 }
185 }
186
187}
188
189impl super::Entity for FxGainLoss {
190 type Id = Uuid;
191
192 fn entity_id(&self) -> &Self::Id {
193 &self.id
194 }
195
196 fn entity_type() -> &'static str {
197 "FxGainLoss"
198 }
199}
200
201impl backbone_core::PersistentEntity for FxGainLoss {
202 fn entity_id(&self) -> String {
203 self.id.to_string()
204 }
205 fn set_entity_id(&mut self, id: String) {
206 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
207 self.id = uuid;
208 }
209 }
210 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
211 self.metadata.created_at
212 }
213 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
214 self.metadata.created_at = Some(ts);
215 }
216 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
217 self.metadata.updated_at
218 }
219 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
220 self.metadata.updated_at = Some(ts);
221 }
222 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
223 self.metadata.deleted_at
224 }
225 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
226 self.metadata.deleted_at = ts;
227 }
228}
229
230impl backbone_orm::EntityRepoMeta for FxGainLoss {
231 fn column_types() -> std::collections::HashMap<String, String> {
232 let mut m = std::collections::HashMap::new();
233 m.insert("id".to_string(), "uuid".to_string());
234 m.insert("bank_clearance_id".to_string(), "uuid".to_string());
235 m.insert("matched_source_id".to_string(), "uuid".to_string());
236 m.insert("fx_account_id".to_string(), "uuid".to_string());
237 m.insert("direction".to_string(), "fx_direction".to_string());
238 m
239 }
240 fn search_fields() -> &'static [&'static str] {
241 &["currency"]
242 }
243}
244
245#[derive(Debug, Clone, Default)]
250pub struct FxGainLossBuilder {
251 bank_clearance_id: Option<Uuid>,
252 matched_source_id: Option<Uuid>,
253 currency: Option<String>,
254 original_rate: Option<Decimal>,
255 realised_rate: Option<Decimal>,
256 base_amount_delta: Option<Decimal>,
257 direction: Option<FxDirection>,
258 fx_account_id: Option<Uuid>,
259}
260
261impl FxGainLossBuilder {
262 pub fn bank_clearance_id(mut self, value: Uuid) -> Self {
264 self.bank_clearance_id = Some(value);
265 self
266 }
267
268 pub fn matched_source_id(mut self, value: Uuid) -> Self {
270 self.matched_source_id = Some(value);
271 self
272 }
273
274 pub fn currency(mut self, value: String) -> Self {
276 self.currency = Some(value);
277 self
278 }
279
280 pub fn original_rate(mut self, value: Decimal) -> Self {
282 self.original_rate = Some(value);
283 self
284 }
285
286 pub fn realised_rate(mut self, value: Decimal) -> Self {
288 self.realised_rate = Some(value);
289 self
290 }
291
292 pub fn base_amount_delta(mut self, value: Decimal) -> Self {
294 self.base_amount_delta = Some(value);
295 self
296 }
297
298 pub fn direction(mut self, value: FxDirection) -> Self {
300 self.direction = Some(value);
301 self
302 }
303
304 pub fn fx_account_id(mut self, value: Uuid) -> Self {
306 self.fx_account_id = Some(value);
307 self
308 }
309
310 pub fn build(self) -> Result<FxGainLoss, String> {
314 let matched_source_id = self.matched_source_id.ok_or_else(|| "matched_source_id is required".to_string())?;
315 let currency = self.currency.ok_or_else(|| "currency is required".to_string())?;
316 let original_rate = self.original_rate.ok_or_else(|| "original_rate is required".to_string())?;
317 let realised_rate = self.realised_rate.ok_or_else(|| "realised_rate is required".to_string())?;
318 let base_amount_delta = self.base_amount_delta.ok_or_else(|| "base_amount_delta is required".to_string())?;
319 let direction = self.direction.ok_or_else(|| "direction is required".to_string())?;
320 let fx_account_id = self.fx_account_id.ok_or_else(|| "fx_account_id is required".to_string())?;
321
322 Ok(FxGainLoss {
323 id: Uuid::new_v4(),
324 bank_clearance_id: self.bank_clearance_id,
325 matched_source_id,
326 currency,
327 original_rate,
328 realised_rate,
329 base_amount_delta,
330 direction,
331 fx_account_id,
332 metadata: AuditMetadata::default(),
333 })
334 }
335}