backbone_inventory/domain/entity/
landed_cost_line.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5use rust_decimal::Decimal;
6
7use super::LandedCostSplitMethod;
8use super::AuditMetadata;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct LandedCostLineId(pub Uuid);
14
15impl LandedCostLineId {
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 LandedCostLineId {
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 LandedCostLineId {
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 LandedCostLineId {
35 fn from(id: Uuid) -> Self { Self(id) }
36}
37
38impl From<LandedCostLineId> for Uuid {
39 fn from(id: LandedCostLineId) -> Self { id.0 }
40}
41
42impl AsRef<Uuid> for LandedCostLineId {
43 fn as_ref(&self) -> &Uuid { &self.0 }
44}
45
46impl std::ops::Deref for LandedCostLineId {
47 type Target = Uuid;
48 fn deref(&self) -> &Self::Target { &self.0 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
52pub struct LandedCostLine {
53 pub id: Uuid,
54 pub lc_id: Uuid,
55 pub name: String,
56 pub account_id: Uuid,
57 pub split_method: LandedCostSplitMethod,
58 pub amount: Decimal,
59 #[serde(default)]
60 #[sqlx(json)]
61 pub metadata: AuditMetadata,
62}
63
64impl LandedCostLine {
65 pub fn builder() -> LandedCostLineBuilder {
67 <LandedCostLineBuilder as Default>::default()
68 }
69
70 pub fn new(lc_id: Uuid, name: String, account_id: Uuid, split_method: LandedCostSplitMethod, amount: Decimal) -> Self {
72 Self {
73 id: Uuid::new_v4(),
74 lc_id,
75 name,
76 account_id,
77 split_method,
78 amount,
79 metadata: AuditMetadata::default(),
80 }
81 }
82
83 pub fn id(&self) -> &Uuid {
85 &self.id
86 }
87
88 pub fn typed_id(&self) -> LandedCostLineId {
90 LandedCostLineId(self.id)
91 }
92
93 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
95 self.metadata.created_at.as_ref()
96 }
97
98 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
100 self.metadata.updated_at.as_ref()
101 }
102
103 pub fn is_deleted(&self) -> bool {
105 self.metadata.deleted_at.is_some()
106 }
107
108 pub fn is_active(&self) -> bool {
110 self.metadata.deleted_at.is_none()
111 }
112
113 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
115 self.metadata.deleted_at.as_ref()
116 }
117
118 pub fn created_by(&self) -> Option<&Uuid> {
120 self.metadata.created_by.as_ref()
121 }
122
123 pub fn updated_by(&self) -> Option<&Uuid> {
125 self.metadata.updated_by.as_ref()
126 }
127
128 pub fn deleted_by(&self) -> Option<&Uuid> {
130 self.metadata.deleted_by.as_ref()
131 }
132
133
134 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
140 for (key, value) in fields {
141 match key.as_str() {
142 "lc_id" => {
143 if let Ok(v) = serde_json::from_value(value) { self.lc_id = v; }
144 }
145 "name" => {
146 if let Ok(v) = serde_json::from_value(value) { self.name = v; }
147 }
148 "account_id" => {
149 if let Ok(v) = serde_json::from_value(value) { self.account_id = v; }
150 }
151 "split_method" => {
152 if let Ok(v) = serde_json::from_value(value) { self.split_method = v; }
153 }
154 "amount" => {
155 if let Ok(v) = serde_json::from_value(value) { self.amount = v; }
156 }
157 _ => {} }
159 }
160 }
161
162}
163
164impl super::Entity for LandedCostLine {
165 type Id = Uuid;
166
167 fn entity_id(&self) -> &Self::Id {
168 &self.id
169 }
170
171 fn entity_type() -> &'static str {
172 "LandedCostLine"
173 }
174}
175
176impl backbone_core::PersistentEntity for LandedCostLine {
177 fn entity_id(&self) -> String {
178 self.id.to_string()
179 }
180 fn set_entity_id(&mut self, id: String) {
181 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
182 self.id = uuid;
183 }
184 }
185 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
186 self.metadata.created_at
187 }
188 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
189 self.metadata.created_at = Some(ts);
190 }
191 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
192 self.metadata.updated_at
193 }
194 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
195 self.metadata.updated_at = Some(ts);
196 }
197 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
198 self.metadata.deleted_at
199 }
200 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
201 self.metadata.deleted_at = ts;
202 }
203}
204
205impl backbone_orm::EntityRepoMeta for LandedCostLine {
206 fn column_types() -> std::collections::HashMap<String, String> {
207 let mut m = std::collections::HashMap::new();
208 m.insert("id".to_string(), "uuid".to_string());
209 m.insert("lc_id".to_string(), "uuid".to_string());
210 m.insert("account_id".to_string(), "uuid".to_string());
211 m.insert("split_method".to_string(), "landed_cost_split_method".to_string());
212 m
213 }
214 fn search_fields() -> &'static [&'static str] {
215 &["name"]
216 }
217 fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
218 &[("landedCost", "landed_costs", "lcId"), ("lc", "landed_costs", "lcId")]
219 }
220}
221
222#[derive(Debug, Clone, Default)]
227pub struct LandedCostLineBuilder {
228 lc_id: Option<Uuid>,
229 name: Option<String>,
230 account_id: Option<Uuid>,
231 split_method: Option<LandedCostSplitMethod>,
232 amount: Option<Decimal>,
233}
234
235impl LandedCostLineBuilder {
236 pub fn lc_id(mut self, value: Uuid) -> Self {
238 self.lc_id = Some(value);
239 self
240 }
241
242 pub fn name(mut self, value: String) -> Self {
244 self.name = Some(value);
245 self
246 }
247
248 pub fn account_id(mut self, value: Uuid) -> Self {
250 self.account_id = Some(value);
251 self
252 }
253
254 pub fn split_method(mut self, value: LandedCostSplitMethod) -> Self {
256 self.split_method = Some(value);
257 self
258 }
259
260 pub fn amount(mut self, value: Decimal) -> Self {
262 self.amount = Some(value);
263 self
264 }
265
266 pub fn build(self) -> Result<LandedCostLine, String> {
270 let lc_id = self.lc_id.ok_or_else(|| "lc_id is required".to_string())?;
271 let name = self.name.ok_or_else(|| "name is required".to_string())?;
272 let account_id = self.account_id.ok_or_else(|| "account_id is required".to_string())?;
273 let amount = self.amount.ok_or_else(|| "amount is required".to_string())?;
274
275 Ok(LandedCostLine {
276 id: Uuid::new_v4(),
277 lc_id,
278 name,
279 account_id,
280 split_method: self.split_method.unwrap_or_default(),
281 amount,
282 metadata: AuditMetadata::default(),
283 })
284 }
285}