Skip to main content

stateset_embedded/
companies.rs

1//! B2B company (account) operations.
2
3use stateset_core::{
4    Company, CompanyFilter, CompanyId, CompanyPriceOverride, CompanyShippingAddress, Contact,
5    ContactId, CreateCompany, CreateContact, Result, UpdateCompany,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10/// B2B company operations.
11pub struct Companies {
12    db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Companies {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("Companies").finish_non_exhaustive()
18    }
19}
20
21impl Companies {
22    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23        Self { db }
24    }
25
26    /// Whether companies are supported by the active backend.
27    #[must_use]
28    pub fn is_supported(&self) -> bool {
29        self.db.supports_capability(DatabaseCapability::Companies)
30    }
31
32    fn ensure(&self) -> Result<()> {
33        self.db.ensure_capability(DatabaseCapability::Companies)
34    }
35
36    /// Create a new company.
37    pub fn create(&self, input: CreateCompany) -> Result<Company> {
38        self.ensure()?;
39        self.db.companies().create(input)
40    }
41
42    /// Get a company by ID.
43    pub fn get(&self, id: CompanyId) -> Result<Option<Company>> {
44        self.ensure()?;
45        self.db.companies().get(id)
46    }
47
48    /// Update a company.
49    pub fn update(&self, id: CompanyId, input: UpdateCompany) -> Result<Company> {
50        self.ensure()?;
51        self.db.companies().update(id, input)
52    }
53
54    /// List companies with optional filtering.
55    pub fn list(&self, filter: CompanyFilter) -> Result<Vec<Company>> {
56        self.ensure()?;
57        self.db.companies().list(filter)
58    }
59
60    /// Delete a company.
61    pub fn delete(&self, id: CompanyId) -> Result<()> {
62        self.ensure()?;
63        self.db.companies().delete(id)
64    }
65
66    /// List a company's shipping addresses.
67    pub fn list_addresses(&self, id: CompanyId) -> Result<Vec<CompanyShippingAddress>> {
68        self.ensure()?;
69        self.db.companies().list_addresses(id)
70    }
71
72    /// List a company's product price overrides.
73    pub fn list_price_overrides(&self, id: CompanyId) -> Result<Vec<CompanyPriceOverride>> {
74        self.ensure()?;
75        self.db.companies().list_price_overrides(id)
76    }
77
78    /// Create a contact linked to one or more companies.
79    pub fn create_contact(&self, input: CreateContact) -> Result<Contact> {
80        self.ensure()?;
81        self.db.companies().create_contact(input)
82    }
83
84    /// Get a contact by ID.
85    pub fn get_contact(&self, id: ContactId) -> Result<Option<Contact>> {
86        self.ensure()?;
87        self.db.companies().get_contact(id)
88    }
89
90    /// List contacts for a company.
91    pub fn list_contacts(&self, company_id: CompanyId) -> Result<Vec<Contact>> {
92        self.ensure()?;
93        self.db.companies().list_contacts(company_id)
94    }
95}