babelforce-manager-sdk 0.46.0

Rust SDK for the babelforce manager APIs — auth, user & agent management, call reporting, metrics, and task automations.
Documentation
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::business_hour_api as api;
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;

/// Business hours — `/api/v2/business-hours`.
pub struct BusinessHoursResource {
    pub(crate) cfg: SharedCfg<Configuration>,
    pub(crate) retry: RetryPolicy,
}

impl BusinessHoursResource {
    /// List all business-hour profiles (auto-paginated).
    pub async fn list_all(&self) -> Result<Vec<models::BusinessHour>, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        collect_all(&self.retry, map_manager_err, |page| async move {
            let r = api::list_business_hours(cfg, Some(page), None).await?;
            Ok((
                r.items,
                r.pagination.pages.unwrap_or(1),
                r.pagination.current.unwrap_or(1),
            ))
        })
        .await
    }

    /// Create a business-hour profile.
    pub async fn create(
        &self,
        body: models::RestCreateBusinessHour,
    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::create_business_hour(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a business-hour profile by id.
    pub async fn get(&self, id: &str) -> Result<models::BusinessHourItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || api::get_business_hour(cfg, id))
            .await
            .map_err(map_manager_err)
    }

    /// Update a business-hour profile.
    pub async fn update(
        &self,
        id: &str,
        body: models::RestUpdateBusinessHour,
    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::update_business_hour(cfg, id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Delete a business-hour profile.
    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || api::delete_business_hour(cfg, id))
            .await
            .map_err(map_manager_err)?;
        Ok(())
    }

    /// Add (replace) the time ranges of a business-hour profile.
    pub async fn add_ranges(
        &self,
        id: &str,
        body: models::BusinessHourRangesBody,
    ) -> Result<models::BusinessHourItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::add_business_hour_ranges(cfg, id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// List a business-hour profile's time ranges.
    pub async fn list_ranges(
        &self,
        id: &str,
    ) -> Result<models::BusinessHourRangeListResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || {
            api::list_business_hour_ranges(cfg, id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a single time range of a business-hour profile.
    pub async fn get_range(
        &self,
        id: &str,
        range_id: &str,
    ) -> Result<models::BusinessHourRangeItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || {
            api::get_business_hour_range(cfg, id, range_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Remove a time range from a business-hour profile.
    pub async fn remove_range(
        &self,
        id: &str,
        range_id: &str,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::remove_business_hour_range(cfg, id, range_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-delete business-hour profiles by id.
    pub async fn bulk_delete(
        &self,
        ids: Vec<String>,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        let ids = ids
            .iter()
            .map(|id| {
                uuid::Uuid::parse_str(id)
                    .map_err(|e| ManagerError::InvalidArgument(format!("ids: {e}")))
            })
            .collect::<Result<Vec<_>, _>>()?;
        let body = models::BulkIdsRequest { ids };
        with_retry(&self.retry, false, || {
            api::bulk_delete_business_hours(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-update business-hour profiles.
    pub async fn bulk_update(
        &self,
        body: models::BusinessHourBulkUpdateRequest,
    ) -> Result<models::BusinessHourListResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::bulk_update_business_hours(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }
}