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