use crate::{
core::{
api_req::ApiRequest, api_resp::ApiResponseTrait, config::Config,
constants::AccessTokenType, http::Transport,
},
service::contact::models::*,
};
use serde::{Deserialize, Serialize};
pub struct DepartmentService {
config: Config,
}
impl DepartmentService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create(
&self,
req: &CreateDepartmentRequest,
) -> crate::core::SDKResult<CreateDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/departments".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<CreateDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn patch(
&self,
department_id: &str,
req: &PatchDepartmentRequest,
) -> crate::core::SDKResult<PatchDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::PATCH,
api_path: format!("/open-apis/contact/v3/departments/{department_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<PatchDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn update(
&self,
department_id: &str,
req: &UpdateDepartmentRequest,
) -> crate::core::SDKResult<UpdateDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::PUT,
api_path: format!("/open-apis/contact/v3/departments/{department_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<UpdateDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn update_department_id(
&self,
department_id: &str,
req: &UpdateDepartmentIdRequest,
) -> crate::core::SDKResult<UpdateDepartmentIdResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::PATCH,
api_path: format!(
"/open-apis/contact/v3/departments/{department_id}/update_department_id"
),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<UpdateDepartmentIdResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn get(
&self,
department_id: &str,
_req: &GetDepartmentRequest,
) -> crate::core::SDKResult<GetDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: format!("/open-apis/contact/v3/departments/{department_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp = Transport::<GetDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn batch(
&self,
req: &BatchGetDepartmentsRequest,
) -> crate::core::SDKResult<BatchGetDepartmentsResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/departments/batch".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<BatchGetDepartmentsResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn children(
&self,
_req: &GetChildrenDepartmentsRequest,
) -> crate::core::SDKResult<GetChildrenDepartmentsResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: "/open-apis/contact/v3/departments/children".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp =
Transport::<GetChildrenDepartmentsResponse>::request(api_req, &self.config, None)
.await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn parent(
&self,
_req: &GetParentDepartmentRequest,
) -> crate::core::SDKResult<GetParentDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: "/open-apis/contact/v3/departments/parent".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp =
Transport::<GetParentDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn search(
&self,
req: &SearchDepartmentsRequest,
) -> crate::core::SDKResult<SearchDepartmentsResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/departments/search".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<SearchDepartmentsResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
pub async fn delete(
&self,
department_id: &str,
_req: &DeleteDepartmentRequest,
) -> crate::core::SDKResult<DeleteDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::DELETE,
api_path: format!("/open-apis/contact/v3/departments/{department_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp =
Transport::<DeleteDepartmentResponse>::request(api_req, &self.config, None).await?;
Ok(resp.data.unwrap_or_default())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateDepartmentRequest {
pub department: Department,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_token: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateDepartmentResponse {
pub department: Department,
}
impl ApiResponseTrait for CreateDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchDepartmentRequest {
pub department: Department,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PatchDepartmentResponse {
pub department: Department,
}
impl ApiResponseTrait for PatchDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateDepartmentRequest {
pub department: Department,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateDepartmentResponse {
pub department: Department,
}
impl ApiResponseTrait for UpdateDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateDepartmentIdRequest {
pub new_department_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateDepartmentIdResponse {}
impl ApiResponseTrait for UpdateDepartmentIdResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetDepartmentRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GetDepartmentResponse {
pub department: Department,
}
impl ApiResponseTrait for GetDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchGetDepartmentsRequest {
pub department_ids: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BatchGetDepartmentsResponse {
pub items: Vec<Department>,
}
impl ApiResponseTrait for BatchGetDepartmentsResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetChildrenDepartmentsRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_department_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fetch_child: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_token: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GetChildrenDepartmentsResponse {
pub items: Vec<Department>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_more: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_token: Option<String>,
}
impl ApiResponseTrait for GetChildrenDepartmentsResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetParentDepartmentRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GetParentDepartmentResponse {
pub items: Vec<Department>,
}
impl ApiResponseTrait for GetParentDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchDepartmentsRequest {
pub query: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SearchDepartmentsResponse {
pub items: Vec<Department>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_more: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_token: Option<String>,
}
impl ApiResponseTrait for SearchDepartmentsResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeleteDepartmentRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub department_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DeleteDepartmentResponse {}
impl ApiResponseTrait for DeleteDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}