Skip to main content

babelforce_manager_sdk/resources/
queues.rs

1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::{manager_api, queue_api, queue_selection_api};
6use crate::gen::manager::models;
7use crate::http::{collect_all, fetch_page, Page};
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Queues — `/api/v2/queues`, with a nested `selections` sub-resource.
11pub struct QueuesResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14    /// Queue selections — `/api/v2/queues/{queueId}/selections`.
15    pub selections: QueueSelectionsResource,
16}
17
18impl QueuesResource {
19    /// List all queues (auto-paginated).
20    pub async fn list_all(&self) -> Result<Vec<models::Queue>, ManagerError> {
21        collect_all(&self.retry, map_manager_err, |page| async move {
22            let r = queue_api::list_queues(self.cfg.as_ref(), Some(page), None).await?;
23            Ok((r.items, r.pagination.pages, r.pagination.current))
24        })
25        .await
26    }
27
28    /// List one page of queues.
29    pub async fn list_page(
30        &self,
31        page: i32,
32        per_page: Option<i32>,
33    ) -> Result<Page<models::Queue>, ManagerError> {
34        fetch_page(&self.retry, map_manager_err, || async move {
35            let r = queue_api::list_queues(self.cfg.as_ref(), Some(page), per_page).await?;
36            Ok((
37                r.items,
38                r.pagination.pages,
39                r.pagination.current,
40                Some(r.pagination.total),
41            ))
42        })
43        .await
44    }
45
46    /// Create a queue.
47    pub async fn create(
48        &self,
49        body: models::RestCreateQueue,
50    ) -> Result<models::QueueItemResponse, ManagerError> {
51        with_retry(&self.retry, false, || {
52            queue_api::create_queue(self.cfg.as_ref(), body.clone())
53        })
54        .await
55        .map_err(map_manager_err)
56    }
57
58    /// Get a queue by id.
59    pub async fn get(&self, id: &str) -> Result<models::QueueItemResponse, ManagerError> {
60        with_retry(&self.retry, true, || {
61            queue_api::get_queue(self.cfg.as_ref(), id)
62        })
63        .await
64        .map_err(map_manager_err)
65    }
66
67    /// Update a queue.
68    pub async fn update(
69        &self,
70        id: &str,
71        body: models::RestUpdateQueue,
72    ) -> Result<models::QueueItemResponse, ManagerError> {
73        with_retry(&self.retry, false, || {
74            queue_api::update_queue(self.cfg.as_ref(), id, body.clone())
75        })
76        .await
77        .map_err(map_manager_err)
78    }
79
80    /// Delete a queue.
81    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
82        with_retry(&self.retry, false, || {
83            queue_api::delete_queue(self.cfg.as_ref(), id)
84        })
85        .await
86        .map_err(map_manager_err)?;
87        Ok(())
88    }
89
90    /// List the triggers attached to a queue.
91    pub async fn list_triggers(
92        &self,
93        queue_id: &str,
94    ) -> Result<models::QueueTriggerListResponse, ManagerError> {
95        with_retry(&self.retry, true, || {
96            queue_api::list_queue_triggers(self.cfg.as_ref(), queue_id)
97        })
98        .await
99        .map_err(map_manager_err)
100    }
101
102    /// Bulk-update queues; returns the updated queues.
103    pub async fn bulk_update(
104        &self,
105        body: models::QueueBulkUpdateRequest,
106    ) -> Result<models::QueueListResponse, ManagerError> {
107        with_retry(&self.retry, false, || {
108            queue_api::bulk_update_queues(self.cfg.as_ref(), body.clone())
109        })
110        .await
111        .map_err(map_manager_err)
112    }
113
114    /// List queue selections across all queues.
115    pub async fn global_selections(
116        &self,
117    ) -> Result<models::GlobalQueueSelectionListResponse, ManagerError> {
118        with_retry(&self.retry, true, || {
119            queue_api::list_global_queue_selections(self.cfg.as_ref(), None, None, None, None, None)
120        })
121        .await
122        .map_err(map_manager_err)
123    }
124}
125
126/// Queue selections (routing rules) and their agent/group/tag membership.
127pub struct QueueSelectionsResource {
128    pub(crate) cfg: Arc<Configuration>,
129    pub(crate) retry: RetryPolicy,
130}
131
132impl QueueSelectionsResource {
133    /// List all selections for a queue (auto-paginated).
134    pub async fn list_all(
135        &self,
136        queue_id: &str,
137    ) -> Result<Vec<models::QueueSelection>, ManagerError> {
138        collect_all(&self.retry, map_manager_err, |page| async move {
139            let r = queue_selection_api::list_queue_selections(
140                self.cfg.as_ref(),
141                queue_id,
142                Some(page),
143                None,
144            )
145            .await?;
146            Ok((r.items, r.pagination.pages, r.pagination.current))
147        })
148        .await
149    }
150
151    /// Create a selection on a queue.
152    pub async fn create(
153        &self,
154        queue_id: &str,
155        body: models::RestCreateQueueSelection,
156    ) -> Result<models::QueueSelectionItemResponse, ManagerError> {
157        with_retry(&self.retry, false, || {
158            queue_selection_api::create_queue_selection(self.cfg.as_ref(), queue_id, body.clone())
159        })
160        .await
161        .map_err(map_manager_err)
162    }
163
164    /// Get a selection.
165    pub async fn get(
166        &self,
167        queue_id: &str,
168        id: &str,
169    ) -> Result<models::QueueSelectionItemResponse, ManagerError> {
170        with_retry(&self.retry, true, || {
171            queue_selection_api::get_queue_selection(self.cfg.as_ref(), queue_id, id)
172        })
173        .await
174        .map_err(map_manager_err)
175    }
176
177    /// Update a selection.
178    pub async fn update(
179        &self,
180        queue_id: &str,
181        id: &str,
182        body: models::RestUpdateQueueSelection,
183    ) -> Result<models::QueueSelectionItemResponse, ManagerError> {
184        with_retry(&self.retry, false, || {
185            queue_selection_api::update_queue_selection(
186                self.cfg.as_ref(),
187                queue_id,
188                id,
189                body.clone(),
190            )
191        })
192        .await
193        .map_err(map_manager_err)
194    }
195
196    /// Delete a selection.
197    pub async fn delete(&self, queue_id: &str, id: &str) -> Result<(), ManagerError> {
198        with_retry(&self.retry, false, || {
199            queue_selection_api::delete_queue_selection(self.cfg.as_ref(), queue_id, id)
200        })
201        .await
202        .map_err(map_manager_err)?;
203        Ok(())
204    }
205
206    /// Resolve the agents currently selected for a queue.
207    pub async fn select_agents(
208        &self,
209        queue_id: &str,
210    ) -> Result<models::QueueSelectionResponse, ManagerError> {
211        with_retry(&self.retry, true, || {
212            manager_api::get_agents_for_queue_selection(self.cfg.as_ref(), queue_id, None)
213        })
214        .await
215        .map_err(map_manager_err)
216    }
217
218    /// Add an agent to a selection.
219    pub async fn add_agent(
220        &self,
221        queue_id: &str,
222        selection_id: &str,
223        agent_id: &str,
224    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
225        with_retry(&self.retry, false, || {
226            manager_api::add_agent_to_queue_selection(
227                self.cfg.as_ref(),
228                queue_id,
229                selection_id,
230                Some(addition(agent_id)),
231            )
232        })
233        .await
234        .map_err(map_manager_err)
235    }
236
237    /// Remove an agent from a selection.
238    pub async fn remove_agent(
239        &self,
240        queue_id: &str,
241        selection_id: &str,
242        id: &str,
243    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
244        with_retry(&self.retry, false, || {
245            manager_api::remove_agent_from_queue_selection(
246                self.cfg.as_ref(),
247                queue_id,
248                selection_id,
249                id,
250            )
251        })
252        .await
253        .map_err(map_manager_err)
254    }
255
256    /// Add an agent group to a selection.
257    pub async fn add_group(
258        &self,
259        queue_id: &str,
260        selection_id: &str,
261        group_id: &str,
262    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
263        with_retry(&self.retry, false, || {
264            manager_api::add_group_to_queue_selection(
265                self.cfg.as_ref(),
266                queue_id,
267                selection_id,
268                Some(addition(group_id)),
269            )
270        })
271        .await
272        .map_err(map_manager_err)
273    }
274
275    /// Remove an agent group from a selection.
276    pub async fn remove_group(
277        &self,
278        queue_id: &str,
279        selection_id: &str,
280        id: &str,
281    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
282        with_retry(&self.retry, false, || {
283            manager_api::remove_group_from_queue_selection(
284                self.cfg.as_ref(),
285                queue_id,
286                selection_id,
287                id,
288            )
289        })
290        .await
291        .map_err(map_manager_err)
292    }
293
294    /// Add a tag to a selection.
295    pub async fn add_tag(
296        &self,
297        queue_id: &str,
298        selection_id: &str,
299        tag_id: &str,
300    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
301        with_retry(&self.retry, false, || {
302            manager_api::add_tag_to_queue_selection(
303                self.cfg.as_ref(),
304                queue_id,
305                selection_id,
306                Some(addition(tag_id)),
307            )
308        })
309        .await
310        .map_err(map_manager_err)
311    }
312
313    /// Remove a tag from a selection.
314    pub async fn remove_tag(
315        &self,
316        queue_id: &str,
317        selection_id: &str,
318        id: &str,
319    ) -> Result<models::QueueSelectionModificationResponse, ManagerError> {
320        with_retry(&self.retry, false, || {
321            manager_api::remove_tag_from_queue_selection(
322                self.cfg.as_ref(),
323                queue_id,
324                selection_id,
325                id,
326            )
327        })
328        .await
329        .map_err(map_manager_err)
330    }
331
332    /// Set the priority order of a queue's selections.
333    pub async fn set_priority(
334        &self,
335        queue_id: &str,
336        items: Vec<models::QueueSelectionPriorityItem>,
337    ) -> Result<models::QueueSelectionListResponse, ManagerError> {
338        with_retry(&self.retry, false, || {
339            queue_api::set_queue_selections_priority(self.cfg.as_ref(), queue_id, items.clone())
340        })
341        .await
342        .map_err(map_manager_err)
343    }
344}
345
346fn addition(id: &str) -> models::QueueSelectionAddition {
347    models::QueueSelectionAddition {
348        id: Some(id.to_string()),
349    }
350}