backbone_pos/domain/entity/
pos_manager_pin.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5use super::AuditMetadata;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct PosManagerPinId(pub Uuid);
11
12impl PosManagerPinId {
13 pub fn new(id: Uuid) -> Self { Self(id) }
14 pub fn generate() -> Self { Self(Uuid::new_v4()) }
15 pub fn into_inner(self) -> Uuid { self.0 }
16}
17
18impl std::fmt::Display for PosManagerPinId {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 write!(f, "{}", self.0)
21 }
22}
23
24impl std::str::FromStr for PosManagerPinId {
25 type Err = uuid::Error;
26 fn from_str(s: &str) -> Result<Self, Self::Err> {
27 Ok(Self(Uuid::parse_str(s)?))
28 }
29}
30
31impl From<Uuid> for PosManagerPinId {
32 fn from(id: Uuid) -> Self { Self(id) }
33}
34
35impl From<PosManagerPinId> for Uuid {
36 fn from(id: PosManagerPinId) -> Self { id.0 }
37}
38
39impl AsRef<Uuid> for PosManagerPinId {
40 fn as_ref(&self) -> &Uuid { &self.0 }
41}
42
43impl std::ops::Deref for PosManagerPinId {
44 type Target = Uuid;
45 fn deref(&self) -> &Self::Target { &self.0 }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
49pub struct PosManagerPin {
50 pub id: Uuid,
51 pub employee_party_id: Uuid,
52 pub pin_hash: String,
53 pub failed_attempts: i32,
54 pub locked_until: Option<DateTime<Utc>>,
55 pub last_attempt_at: Option<DateTime<Utc>>,
56 pub last_attempt_ip: Option<String>,
57 #[serde(default)]
58 #[sqlx(json)]
59 pub metadata: AuditMetadata,
60}
61
62impl PosManagerPin {
63 pub fn builder() -> PosManagerPinBuilder {
65 <PosManagerPinBuilder as Default>::default()
66 }
67
68 pub fn new(employee_party_id: Uuid, pin_hash: String, failed_attempts: i32) -> Self {
70 Self {
71 id: Uuid::new_v4(),
72 employee_party_id,
73 pin_hash,
74 failed_attempts,
75 locked_until: None,
76 last_attempt_at: None,
77 last_attempt_ip: None,
78 metadata: AuditMetadata::default(),
79 }
80 }
81
82 pub fn id(&self) -> &Uuid {
84 &self.id
85 }
86
87 pub fn typed_id(&self) -> PosManagerPinId {
89 PosManagerPinId(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
133 pub fn with_locked_until(mut self, value: DateTime<Utc>) -> Self {
139 self.locked_until = Some(value);
140 self
141 }
142
143 pub fn with_last_attempt_at(mut self, value: DateTime<Utc>) -> Self {
145 self.last_attempt_at = Some(value);
146 self
147 }
148
149 pub fn with_last_attempt_ip(mut self, value: String) -> Self {
151 self.last_attempt_ip = Some(value);
152 self
153 }
154
155 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
161 for (key, value) in fields {
162 match key.as_str() {
163 "employee_party_id" => {
164 if let Ok(v) = serde_json::from_value(value) { self.employee_party_id = v; }
165 }
166 "pin_hash" => {
167 if let Ok(v) = serde_json::from_value(value) { self.pin_hash = v; }
168 }
169 "failed_attempts" => {
170 if let Ok(v) = serde_json::from_value(value) { self.failed_attempts = v; }
171 }
172 "locked_until" => {
173 if let Ok(v) = serde_json::from_value(value) { self.locked_until = v; }
174 }
175 "last_attempt_at" => {
176 if let Ok(v) = serde_json::from_value(value) { self.last_attempt_at = v; }
177 }
178 "last_attempt_ip" => {
179 if let Ok(v) = serde_json::from_value(value) { self.last_attempt_ip = v; }
180 }
181 _ => {} }
183 }
184 }
185
186}
187
188impl super::Entity for PosManagerPin {
189 type Id = Uuid;
190
191 fn entity_id(&self) -> &Self::Id {
192 &self.id
193 }
194
195 fn entity_type() -> &'static str {
196 "PosManagerPin"
197 }
198}
199
200impl backbone_core::PersistentEntity for PosManagerPin {
201 fn write_protected_fields() -> &'static [&'static str] {
202 &["pin_hash"]
203 }
204 fn entity_id(&self) -> String {
205 self.id.to_string()
206 }
207 fn set_entity_id(&mut self, id: String) {
208 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
209 self.id = uuid;
210 }
211 }
212 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
213 self.metadata.created_at
214 }
215 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
216 self.metadata.created_at = Some(ts);
217 }
218 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
219 self.metadata.updated_at
220 }
221 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
222 self.metadata.updated_at = Some(ts);
223 }
224 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
225 self.metadata.deleted_at
226 }
227 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
228 self.metadata.deleted_at = ts;
229 }
230}
231
232impl backbone_orm::EntityRepoMeta for PosManagerPin {
233 fn column_types() -> std::collections::HashMap<String, String> {
234 let mut m = std::collections::HashMap::new();
235 m.insert("id".to_string(), "uuid".to_string());
236 m.insert("employee_party_id".to_string(), "uuid".to_string());
237 m.insert("locked_until".to_string(), "timestamptz".to_string());
238 m.insert("last_attempt_at".to_string(), "timestamptz".to_string());
239 m
240 }
241 fn search_fields() -> &'static [&'static str] {
242 &[]
243 }
244 fn secret_fields() -> &'static [&'static str] {
245 &["pinHash"]
246 }
247}
248
249#[derive(Debug, Clone, Default)]
254pub struct PosManagerPinBuilder {
255 employee_party_id: Option<Uuid>,
256 pin_hash: Option<String>,
257 failed_attempts: Option<i32>,
258 locked_until: Option<DateTime<Utc>>,
259 last_attempt_at: Option<DateTime<Utc>>,
260 last_attempt_ip: Option<String>,
261}
262
263impl PosManagerPinBuilder {
264 pub fn employee_party_id(mut self, value: Uuid) -> Self {
266 self.employee_party_id = Some(value);
267 self
268 }
269
270 pub fn pin_hash(mut self, value: String) -> Self {
272 self.pin_hash = Some(value);
273 self
274 }
275
276 pub fn failed_attempts(mut self, value: i32) -> Self {
278 self.failed_attempts = Some(value);
279 self
280 }
281
282 pub fn locked_until(mut self, value: DateTime<Utc>) -> Self {
284 self.locked_until = Some(value);
285 self
286 }
287
288 pub fn last_attempt_at(mut self, value: DateTime<Utc>) -> Self {
290 self.last_attempt_at = Some(value);
291 self
292 }
293
294 pub fn last_attempt_ip(mut self, value: String) -> Self {
296 self.last_attempt_ip = Some(value);
297 self
298 }
299
300 pub fn build(self) -> Result<PosManagerPin, String> {
304 let employee_party_id = self.employee_party_id.ok_or_else(|| "employee_party_id is required".to_string())?;
305 let pin_hash = self.pin_hash.ok_or_else(|| "pin_hash is required".to_string())?;
306
307 Ok(PosManagerPin {
308 id: Uuid::new_v4(),
309 employee_party_id,
310 pin_hash,
311 failed_attempts: self.failed_attempts.unwrap_or(0),
312 locked_until: self.locked_until,
313 last_attempt_at: self.last_attempt_at,
314 last_attempt_ip: self.last_attempt_ip,
315 metadata: AuditMetadata::default(),
316 })
317 }
318}