Skip to main content

stateset_embedded/
channels.rs

1//! Sales / fulfillment channel operations.
2
3use stateset_core::{
4    Channel, ChannelFilter, ChannelId, ChannelProductMapping, ChannelProductSyncItem,
5    CreateChannel, Result, UpdateChannel,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10/// Channel operations (sales / fulfillment / end-to-end).
11pub struct Channels {
12    db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Channels {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("Channels").finish_non_exhaustive()
18    }
19}
20
21impl Channels {
22    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23        Self { db }
24    }
25
26    /// Whether channels are supported by the active backend.
27    #[must_use]
28    pub fn is_supported(&self) -> bool {
29        self.db.supports_capability(DatabaseCapability::Channels)
30    }
31
32    fn ensure(&self) -> Result<()> {
33        self.db.ensure_capability(DatabaseCapability::Channels)
34    }
35
36    /// Create a new channel.
37    pub fn create(&self, input: CreateChannel) -> Result<Channel> {
38        self.ensure()?;
39        self.db.channels().create(input)
40    }
41
42    /// Get a channel by ID.
43    pub fn get(&self, id: ChannelId) -> Result<Option<Channel>> {
44        self.ensure()?;
45        self.db.channels().get(id)
46    }
47
48    /// Update a channel.
49    pub fn update(&self, id: ChannelId, input: UpdateChannel) -> Result<Channel> {
50        self.ensure()?;
51        self.db.channels().update(id, input)
52    }
53
54    /// List channels with optional filtering.
55    pub fn list(&self, filter: ChannelFilter) -> Result<Vec<Channel>> {
56        self.ensure()?;
57        self.db.channels().list(filter)
58    }
59
60    /// Soft-delete a channel.
61    pub fn delete(&self, id: ChannelId) -> Result<()> {
62        self.ensure()?;
63        self.db.channels().delete(id)
64    }
65
66    /// Lock or unlock a channel against external mutations.
67    pub fn set_lock(&self, id: ChannelId, locked: bool) -> Result<Channel> {
68        self.ensure()?;
69        self.db.channels().set_lock(id, locked)
70    }
71
72    /// Bulk upsert/delete channel SKU mappings. Returns the affected count.
73    pub fn sync_products(&self, id: ChannelId, items: Vec<ChannelProductSyncItem>) -> Result<u64> {
74        self.ensure()?;
75        self.db.channels().sync_products(id, items)
76    }
77
78    /// List a channel's SKU mappings.
79    pub fn list_product_mappings(&self, id: ChannelId) -> Result<Vec<ChannelProductMapping>> {
80        self.ensure()?;
81        self.db.channels().list_product_mappings(id)
82    }
83}