Skip to main content

babelforce_manager_sdk/resources/
businesshours.rs

1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::business_hour_api as api;
5use crate::gen::manager::apis::configuration::Configuration;
6use crate::gen::manager::models;
7use crate::http::collect_all;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Business hours — `/api/v2/business-hours`.
11pub struct BusinessHoursResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl BusinessHoursResource {
17    /// List all business-hour profiles (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::BusinessHour>, ManagerError> {
19        collect_all(&self.retry, map_manager_err, |page| async move {
20            let r = api::list_business_hours(self.cfg.as_ref(), Some(page), None).await?;
21            Ok((r.items, r.pagination.pages, r.pagination.current))
22        })
23        .await
24    }
25
26    /// Create a business-hour profile.
27    pub async fn create(
28        &self,
29        body: models::RestCreateBusinessHour,
30    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
31        with_retry(&self.retry, false, || {
32            api::create_business_hour(self.cfg.as_ref(), body.clone())
33        })
34        .await
35        .map_err(map_manager_err)
36    }
37
38    /// Get a business-hour profile by id.
39    pub async fn get(&self, id: &str) -> Result<models::BusinessHourItemResponse, ManagerError> {
40        with_retry(&self.retry, true, || {
41            api::get_business_hour(self.cfg.as_ref(), id)
42        })
43        .await
44        .map_err(map_manager_err)
45    }
46
47    /// Update a business-hour profile.
48    pub async fn update(
49        &self,
50        id: &str,
51        body: models::RestUpdateBusinessHour,
52    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
53        with_retry(&self.retry, false, || {
54            api::update_business_hour(self.cfg.as_ref(), id, body.clone())
55        })
56        .await
57        .map_err(map_manager_err)
58    }
59
60    /// Delete a business-hour profile.
61    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
62        with_retry(&self.retry, false, || {
63            api::delete_business_hour(self.cfg.as_ref(), id)
64        })
65        .await
66        .map_err(map_manager_err)?;
67        Ok(())
68    }
69
70    /// Add (replace) the time ranges of a business-hour profile.
71    pub async fn add_ranges(
72        &self,
73        id: &str,
74        body: models::BusinessHourRangesBody,
75    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
76        with_retry(&self.retry, false, || {
77            api::add_business_hour_ranges(self.cfg.as_ref(), id, body.clone())
78        })
79        .await
80        .map_err(map_manager_err)
81    }
82
83    /// List a business-hour profile's time ranges.
84    pub async fn list_ranges(
85        &self,
86        id: &str,
87    ) -> Result<models::BusinessHourRangeListResponse, ManagerError> {
88        with_retry(&self.retry, true, || {
89            api::list_business_hour_ranges(self.cfg.as_ref(), id)
90        })
91        .await
92        .map_err(map_manager_err)
93    }
94
95    /// Get a single time range of a business-hour profile.
96    pub async fn get_range(
97        &self,
98        id: &str,
99        range_id: &str,
100    ) -> Result<models::BusinessHourRangeItemResponse, ManagerError> {
101        with_retry(&self.retry, true, || {
102            api::get_business_hour_range(self.cfg.as_ref(), id, range_id)
103        })
104        .await
105        .map_err(map_manager_err)
106    }
107
108    /// Remove a time range from a business-hour profile.
109    pub async fn remove_range(
110        &self,
111        id: &str,
112        range_id: &str,
113    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
114        with_retry(&self.retry, false, || {
115            api::remove_business_hour_range(self.cfg.as_ref(), id, range_id)
116        })
117        .await
118        .map_err(map_manager_err)
119    }
120
121    /// Bulk-delete business-hour profiles by id.
122    pub async fn bulk_delete(
123        &self,
124        ids: Vec<String>,
125    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
126        let ids = ids
127            .iter()
128            .map(|id| {
129                uuid::Uuid::parse_str(id)
130                    .map_err(|e| ManagerError::InvalidArgument(format!("ids: {e}")))
131            })
132            .collect::<Result<Vec<_>, _>>()?;
133        let body = models::BulkIdsRequest { ids };
134        with_retry(&self.retry, false, || {
135            api::bulk_delete_business_hours(self.cfg.as_ref(), body.clone())
136        })
137        .await
138        .map_err(map_manager_err)
139    }
140
141    /// Bulk-update business-hour profiles.
142    pub async fn bulk_update(
143        &self,
144        body: models::BusinessHourBulkUpdateRequest,
145    ) -> Result<models::BusinessHourListResponse, ManagerError> {
146        with_retry(&self.retry, false, || {
147            api::bulk_update_business_hours(self.cfg.as_ref(), body.clone())
148        })
149        .await
150        .map_err(map_manager_err)
151    }
152}