166 lines
4.2 KiB
Rust
166 lines
4.2 KiB
Rust
use crate::db::{Model, Storable, IndexKey}; // Import Model trait and IndexKey from db module
|
|
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Customer represents a customer who can purchase products or services
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Customer {
|
|
pub id: u32,
|
|
pub name: String,
|
|
pub description: String,
|
|
pub pubkey: String,
|
|
pub contact_ids: Vec<u32>,
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
}
|
|
|
|
impl Customer {
|
|
/// Create a new customer with default timestamps
|
|
pub fn new(
|
|
id: u32,
|
|
name: String,
|
|
description: String,
|
|
pubkey: String,
|
|
) -> Self {
|
|
let now = Utc::now();
|
|
Self {
|
|
id,
|
|
name,
|
|
description,
|
|
pubkey,
|
|
contact_ids: Vec::new(),
|
|
created_at: now,
|
|
updated_at: now,
|
|
}
|
|
}
|
|
|
|
/// Add a contact ID to the customer
|
|
pub fn add_contact(&mut self, contact_id: u32) {
|
|
if !self.contact_ids.contains(&contact_id) {
|
|
self.contact_ids.push(contact_id);
|
|
self.updated_at = Utc::now();
|
|
}
|
|
}
|
|
|
|
/// Remove a contact ID from the customer
|
|
pub fn remove_contact(&mut self, contact_id: u32) -> bool {
|
|
let len = self.contact_ids.len();
|
|
self.contact_ids.retain(|&id| id != contact_id);
|
|
|
|
if self.contact_ids.len() < len {
|
|
self.updated_at = Utc::now();
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Builder for Customer
|
|
pub struct CustomerBuilder {
|
|
id: Option<u32>,
|
|
name: Option<String>,
|
|
description: Option<String>,
|
|
pubkey: Option<String>,
|
|
contact_ids: Vec<u32>,
|
|
created_at: Option<DateTime<Utc>>,
|
|
updated_at: Option<DateTime<Utc>>,
|
|
}
|
|
|
|
impl CustomerBuilder {
|
|
/// Create a new CustomerBuilder with all fields set to None
|
|
pub fn new() -> Self {
|
|
Self {
|
|
id: None,
|
|
name: None,
|
|
description: None,
|
|
pubkey: None,
|
|
contact_ids: Vec::new(),
|
|
created_at: None,
|
|
updated_at: None,
|
|
}
|
|
}
|
|
|
|
/// Set the id
|
|
pub fn id(mut self, id: u32) -> Self {
|
|
self.id = Some(id);
|
|
self
|
|
}
|
|
|
|
/// Set the name
|
|
pub fn name<S: Into<String>>(mut self, name: S) -> Self {
|
|
self.name = Some(name.into());
|
|
self
|
|
}
|
|
|
|
/// Set the description
|
|
pub fn description<S: Into<String>>(mut self, description: S) -> Self {
|
|
self.description = Some(description.into());
|
|
self
|
|
}
|
|
|
|
/// Set the pubkey
|
|
pub fn pubkey<S: Into<String>>(mut self, pubkey: S) -> Self {
|
|
self.pubkey = Some(pubkey.into());
|
|
self
|
|
}
|
|
|
|
/// Add a contact ID
|
|
pub fn add_contact(mut self, contact_id: u32) -> Self {
|
|
self.contact_ids.push(contact_id);
|
|
self
|
|
}
|
|
|
|
/// Set multiple contact IDs
|
|
pub fn contact_ids(mut self, contact_ids: Vec<u32>) -> Self {
|
|
self.contact_ids = contact_ids;
|
|
self
|
|
}
|
|
|
|
/// Build the Customer object
|
|
pub fn build(self) -> Result<Customer, &'static str> {
|
|
let now = Utc::now();
|
|
|
|
Ok(Customer {
|
|
id: self.id.ok_or("id is required")?,
|
|
name: self.name.ok_or("name is required")?,
|
|
description: self.description.ok_or("description is required")?,
|
|
pubkey: self.pubkey.ok_or("pubkey is required")?,
|
|
contact_ids: self.contact_ids,
|
|
created_at: self.created_at.unwrap_or(now),
|
|
updated_at: self.updated_at.unwrap_or(now),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Storable for Customer {}
|
|
|
|
|
|
// Implement Model trait
|
|
impl Model for Customer {
|
|
fn get_id(&self) -> u32 {
|
|
self.id
|
|
}
|
|
|
|
fn db_prefix() -> &'static str {
|
|
"customer"
|
|
}
|
|
|
|
fn db_keys(&self) -> Vec<IndexKey> {
|
|
let mut keys = Vec::new();
|
|
|
|
// Add an index for the name
|
|
keys.push(IndexKey {
|
|
name: "name",
|
|
value: self.name.clone(),
|
|
});
|
|
|
|
// Add an index for the pubkey
|
|
keys.push(IndexKey {
|
|
name: "pubkey",
|
|
value: self.pubkey.clone(),
|
|
});
|
|
|
|
keys
|
|
}
|
|
} |