babelforce_manager_sdk/resources/
businesshours.rs1use 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
10pub struct BusinessHoursResource {
12 pub(crate) cfg: Arc<Configuration>,
13 pub(crate) retry: RetryPolicy,
14}
15
16impl BusinessHoursResource {
17 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 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 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 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 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 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 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 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 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 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 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}