openlark_platform/app_engine/apaas/v1/user_task/
cc.rs1use openlark_core::{
7 SDKResult,
8 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9 config::Config,
10 http::Transport,
11 req_option::RequestOption,
12};
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone)]
17pub struct CcTaskRequestBuilder {
18 config: Config,
19 task_id: String,
21 user_ids: Vec<String>,
23 reason: Option<String>,
25}
26
27impl CcTaskRequestBuilder {
28 pub fn new(config: Config, task_id: impl Into<String>) -> Self {
30 Self {
31 config,
32 task_id: task_id.into(),
33 user_ids: Vec::new(),
34 reason: None,
35 }
36 }
37
38 pub fn user_id(mut self, user_id: impl Into<String>) -> Self {
40 self.user_ids.push(user_id.into());
41 self
42 }
43
44 pub fn user_ids(mut self, user_ids: impl IntoIterator<Item = impl Into<String>>) -> Self {
46 self.user_ids.extend(user_ids.into_iter().map(Into::into));
47 self
48 }
49
50 pub fn reason(mut self, reason: impl Into<String>) -> Self {
52 self.reason = Some(reason.into());
53 self
54 }
55
56 pub async fn execute(self) -> SDKResult<CcTaskResponse> {
58 self.execute_with_options(RequestOption::default()).await
59 }
60
61 pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<CcTaskResponse> {
63 let url = format!("/open-apis/apaas/v1/user_tasks/{}/cc", self.task_id);
64
65 let request = CcTaskRequest {
66 user_ids: self.user_ids,
67 reason: self.reason,
68 };
69
70 let req: ApiRequest<CcTaskResponse> =
71 ApiRequest::post(&url).body(serde_json::to_value(&request)?);
72 Transport::request_typed(req, &self.config, Some(option), "Operation").await
73 }
74}
75
76#[derive(Debug, Clone, Deserialize, Serialize)]
78struct CcTaskRequest {
79 #[serde(rename = "user_ids")]
81 user_ids: Vec<String>,
82 #[serde(rename = "reason", skip_serializing_if = "Option::is_none")]
84 reason: Option<String>,
85}
86
87#[derive(Debug, Clone, Deserialize, Serialize)]
89pub struct CcTaskResponse {
90 #[serde(rename = "task_id")]
92 pub task_id: String,
93 #[serde(rename = "cc_id")]
95 pub cc_id: String,
96 #[serde(rename = "message")]
98 pub message: String,
99}
100
101impl ApiResponseTrait for CcTaskResponse {
102 fn data_format() -> ResponseFormat {
103 ResponseFormat::Data
104 }
105}
106
107#[deprecated(note = "renamed to CcTaskRequestBuilder, will be removed in v1.0 (#271)")]
109pub type CcTaskBuilder = CcTaskRequestBuilder;
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[tokio::test]
117 async fn test_cc_user_task_returns_data_on_success() {
118 use serde_json::json;
119 use wiremock::MockServer;
120 use wiremock::matchers::{method, path};
121 use wiremock::{Mock, ResponseTemplate};
122
123 let server = MockServer::start().await;
124 Mock::given(method("POST"))
125 .and(path("/open-apis/apaas/v1/user_tasks/task_001/cc"))
126 .respond_with(ResponseTemplate::new(200).set_body_json(json!({
127 "code": 0,
128 "msg": "success",
129 "data": {
130 "task_id": "task_001",
131 "cc_id": "cc_001",
132 "message": "抄送成功"
133 }
134 })))
135 .mount(&server)
136 .await;
137
138 let config = Config::builder()
139 .app_id("ci_app_id")
140 .app_secret("ci_app_secret")
141 .base_url(server.uri())
142 .enable_token_cache(false)
143 .build();
144
145 let resp = CcTaskRequestBuilder::new(config, "task_001")
146 .user_ids(vec!["u_001".to_string(), "u_002".to_string()])
147 .reason("请知会")
148 .execute()
149 .await
150 .expect("抄送人工任务应成功");
151 assert_eq!(resp.task_id, "task_001");
152 assert_eq!(resp.cc_id, "cc_001");
153 assert_eq!(resp.message, "抄送成功");
154
155 let received = server.received_requests().await.unwrap_or_default();
156 assert_eq!(received.len(), 1);
157 assert_eq!(
158 received[0].url.path(),
159 "/open-apis/apaas/v1/user_tasks/task_001/cc"
160 );
161 }
162}