Skip to main content

openlark_workflow/v2/task/
update.rs

1//! 更新任务
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/task/update
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::task::models::{UpdateTaskBody, UpdateTaskResponse};
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use std::sync::Arc;
14
15/// 更新任务请求
16#[derive(Debug, Clone)]
17pub struct UpdateTaskRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 任务 GUID
21    task_guid: String,
22    /// 请求体
23    body: UpdateTaskBody,
24}
25
26impl UpdateTaskRequest {
27    /// 创建新的请求构建器。
28    pub fn new(config: Arc<Config>, task_guid: String) -> Self {
29        Self {
30            config,
31            task_guid,
32            body: UpdateTaskBody::default(),
33        }
34    }
35
36    /// 设置任务标题
37    pub fn summary(mut self, summary: impl Into<String>) -> Self {
38        self.body.summary = Some(summary.into());
39        self
40    }
41
42    /// 设置任务描述
43    pub fn description(mut self, description: impl Into<String>) -> Self {
44        self.body.description = Some(description.into());
45        self
46    }
47
48    /// 设置任务开始时间
49    pub fn start(mut self, start: impl Into<String>) -> Self {
50        self.body.start = Some(start.into());
51        self
52    }
53
54    /// 设置任务截止时间
55    pub fn due(mut self, due: impl Into<String>) -> Self {
56        self.body.due = Some(due.into());
57        self
58    }
59
60    /// 设置任务优先级(1-5)
61    pub fn priority(mut self, priority: i32) -> Self {
62        self.body.priority = Some(priority);
63        self
64    }
65
66    /// 设置自定义字段
67    pub fn custom_fields(mut self, custom_fields: serde_json::Value) -> Self {
68        self.body.custom_fields = Some(custom_fields);
69        self
70    }
71
72    /// 设置任务关注者
73    pub fn followers(mut self, followers: Vec<String>) -> Self {
74        self.body.followers = Some(followers);
75        self
76    }
77
78    /// 设置任务执行者
79    pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
80        self.body.assignee = Some(assignee.into());
81        self
82    }
83
84    /// 设置任务状态
85    pub fn status(mut self, status: impl Into<String>) -> Self {
86        self.body.status = Some(status.into());
87        self
88    }
89
90    /// 执行请求
91    pub async fn execute(self) -> SDKResult<UpdateTaskResponse> {
92        self.execute_with_options(openlark_core::req_option::RequestOption::default())
93            .await
94    }
95
96    /// 执行请求(带选项)
97    pub async fn execute_with_options(
98        self,
99        option: openlark_core::req_option::RequestOption,
100    ) -> SDKResult<UpdateTaskResponse> {
101        // 验证必填字段
102        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
103
104        let api_endpoint = TaskApiV2::TaskUpdate(self.task_guid.clone());
105        let mut request = ApiRequest::<UpdateTaskResponse>::patch(api_endpoint.to_url());
106
107        let request_body = &self.body;
108        request = request.body(serialize_params(request_body, "更新任务")?);
109
110        let response =
111            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
112        extract_response_data(response, "更新任务")
113    }
114}
115
116impl ApiResponseTrait for UpdateTaskResponse {
117    fn data_format() -> ResponseFormat {
118        ResponseFormat::Data
119    }
120}
121
122#[cfg(test)]
123#[allow(unused_imports)]
124mod tests {
125    use std::sync::Arc;
126
127    use super::*;
128
129    #[test]
130    fn test_update_task_builder() {
131        let config = openlark_core::config::Config::builder()
132            .app_id("test")
133            .app_secret("test")
134            .build();
135
136        let request = UpdateTaskRequest::new(Arc::new(config), "task_123".to_string())
137            .summary("更新后的标题")
138            .priority(4);
139
140        assert_eq!(request.task_guid, "task_123");
141        assert_eq!(request.body.summary, Some("更新后的标题".to_string()));
142    }
143}