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