use crate::{
core::{
api_req::ApiRequest, api_resp::ApiResponseTrait, config::Config,
constants::AccessTokenType, http::Transport, req_option::RequestOption,
standard_response::StandardResponse, trait_system::executable_builder::ExecutableBuilder,
SDKResult,
},
service::contact::models::*,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
pub struct UserService {
config: Config,
}
impl UserService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create(
&self,
req: &CreateUserRequest,
) -> crate::core::SDKResult<CreateUserResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/users".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<CreateUserResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn patch(
&self,
user_id: &str,
req: &PatchUserRequest,
) -> crate::core::SDKResult<PatchUserResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::PATCH,
api_path: format!("/open-apis/contact/v3/users/{user_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<PatchUserResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn update_user_id(
&self,
user_id: &str,
req: &UpdateUserIdRequest,
) -> crate::core::SDKResult<UpdateUserIdResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::PATCH,
api_path: format!("/open-apis/contact/v3/users/{user_id}/update_user_id"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<UpdateUserIdResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn get(
&self,
user_id: &str,
_req: &GetUserRequest,
) -> crate::core::SDKResult<GetUserResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: format!("/open-apis/contact/v3/users/{user_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp = Transport::<GetUserResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn batch(
&self,
req: &BatchGetUsersRequest,
) -> crate::core::SDKResult<BatchGetUsersResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/users/batch".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<BatchGetUsersResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn find_by_department(
&self,
_req: &FindUsersByDepartmentRequest,
) -> crate::core::SDKResult<FindUsersByDepartmentResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: "/open-apis/contact/v3/users/find_by_department".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::<FindUsersByDepartmentResponse>::request(api_req, &self.config, None)
.await?;
resp.into_result()
}
pub async fn batch_get_id(
&self,
req: &BatchGetUserIdRequest,
) -> crate::core::SDKResult<BatchGetUserIdResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/users/batch_get_id".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp =
Transport::<BatchGetUserIdResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn search(
&self,
req: &SearchUsersRequest,
) -> crate::core::SDKResult<SearchUsersResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: "/open-apis/contact/v3/users/search".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<SearchUsersResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn delete(
&self,
user_id: &str,
_req: &DeleteUserRequest,
) -> crate::core::SDKResult<DeleteUserResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::DELETE,
api_path: format!("/open-apis/contact/v3/users/{user_id}"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: Vec::new(),
query_params: std::collections::HashMap::new(),
..Default::default()
};
let resp = Transport::<DeleteUserResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn resurrect(
&self,
user_id: &str,
req: &ResurrectUserRequest,
) -> crate::core::SDKResult<ResurrectUserResponse> {
let api_req = ApiRequest {
http_method: reqwest::Method::POST,
api_path: format!("/open-apis/contact/v3/users/{user_id}/resurrect"),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(req)?,
..Default::default()
};
let resp = Transport::<ResurrectUserResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub async fn list(&self, req: &ListUsersRequest) -> crate::core::SDKResult<ListUsersResponse> {
let mut query_params = std::collections::HashMap::new();
if let Some(page_size) = req.page_size {
query_params.insert("page_size".to_string(), page_size.to_string());
}
if let Some(page_token) = &req.page_token {
query_params.insert("page_token".to_string(), page_token.clone());
}
if let Some(user_id_type) = &req.user_id_type {
query_params.insert("user_id_type".to_string(), user_id_type.clone());
}
if let Some(department_id_type) = &req.department_id_type {
query_params.insert("department_id_type".to_string(), department_id_type.clone());
}
let api_req = ApiRequest {
http_method: reqwest::Method::GET,
api_path: "/open-apis/contact/v3/users".to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: Vec::new(),
query_params,
..Default::default()
};
let resp = Transport::<ListUsersResponse>::request(api_req, &self.config, None).await?;
resp.into_result()
}
pub fn create_user_builder(&self) -> CreateUserBuilder {
CreateUserBuilder::new()
}
}
#[derive(Default)]
pub struct CreateUserBuilder {
user: Option<User>,
user_id_type: Option<String>,
department_id_type: Option<String>,
}
impl CreateUserBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn user(mut self, user: User) -> Self {
self.user = Some(user);
self
}
pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
self.user_id_type = Some(user_id_type.to_string());
self
}
pub fn department_id_type(mut self, department_id_type: impl ToString) -> Self {
self.department_id_type = Some(department_id_type.to_string());
self
}
pub fn build(self) -> CreateUserRequest {
CreateUserRequest {
user: self.user.unwrap_or_default(),
user_id_type: self.user_id_type,
department_id_type: self.department_id_type,
}
}
}
#[async_trait]
impl ExecutableBuilder<UserService, CreateUserRequest, CreateUserResponse> for CreateUserBuilder {
fn build(self) -> CreateUserRequest {
self.build()
}
async fn execute(self, service: &UserService) -> SDKResult<CreateUserResponse> {
let req = self.build();
service.create(&req).await
}
async fn execute_with_options(
self,
service: &UserService,
_option: RequestOption,
) -> SDKResult<CreateUserResponse> {
let req = self.build();
service.create(&req).await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateUserRequest {
pub user: User,
#[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 CreateUserResponse {
pub user: User,
}
impl ApiResponseTrait for CreateUserResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchUserRequest {
pub user: User,
#[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 PatchUserResponse {
pub user: User,
}
impl ApiResponseTrait for PatchUserResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateUserIdRequest {
pub new_user_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateUserIdResponse {}
impl ApiResponseTrait for UpdateUserIdResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetUserRequest {
#[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 GetUserResponse {
pub user: User,
}
impl ApiResponseTrait for GetUserResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchGetUsersRequest {
pub user_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 BatchGetUsersResponse {
pub items: Vec<User>,
}
impl ApiResponseTrait for BatchGetUsersResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FindUsersByDepartmentRequest {
#[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>,
#[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 FindUsersByDepartmentResponse {
pub items: Vec<User>,
#[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 FindUsersByDepartmentResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchGetUserIdRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub emails: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mobiles: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_resigned: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BatchGetUserIdResponse {
pub user_list: Vec<UserIdInfo>,
}
impl ApiResponseTrait for BatchGetUserIdResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserIdInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mobile: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchUsersRequest {
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 SearchUsersResponse {
pub items: Vec<User>,
#[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 SearchUsersResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeleteUserRequest {
#[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 DeleteUserResponse {}
impl ApiResponseTrait for DeleteUserResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResurrectUserRequest {
#[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 ResurrectUserResponse {
pub user: User,
}
impl ApiResponseTrait for ResurrectUserResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListUsersRequest {
#[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 ListUsersResponse {
pub items: Vec<User>,
#[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 ListUsersResponse {
fn data_format() -> crate::core::api_resp::ResponseFormat {
crate::core::api_resp::ResponseFormat::Data
}
}