Skip to main content

openlark_workflow/v2/task/
add_reminders.rs

1//! 添加任务提醒
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/task-add_reminders/create
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7    SDKResult,
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required,
11};
12use serde::{Deserialize, Serialize};
13use std::sync::Arc;
14
15/// 添加任务提醒请求体
16#[derive(Debug, Clone, Serialize, Default)]
17pub struct AddRemindersBody {
18    /// 提醒时间列表(ISO 8601 格式)
19    pub reminders: Vec<String>,
20}
21
22/// 任务提醒信息
23#[derive(Debug, Clone, Deserialize)]
24pub struct TaskReminder {
25    /// 提醒 GUID
26    pub reminder_guid: String,
27    /// 提醒时间
28    pub remind_at: String,
29    /// 创建时间
30    pub created_at: String,
31}
32
33/// 添加任务提醒响应
34#[derive(Debug, Clone, Deserialize)]
35pub struct AddRemindersResponse {
36    /// 任务 GUID
37    pub task_guid: String,
38    /// 添加的提醒列表
39    #[serde(default)]
40    pub reminders: Vec<TaskReminder>,
41}
42
43/// 添加任务提醒请求
44#[derive(Debug, Clone)]
45pub struct AddRemindersRequest {
46    /// 配置信息
47    config: Arc<Config>,
48    /// 任务 GUID
49    task_guid: String,
50    /// 请求体
51    body: AddRemindersBody,
52}
53
54impl AddRemindersRequest {
55    /// 创建新的请求构建器。
56    pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
57        Self {
58            config,
59            task_guid: task_guid.into(),
60            body: AddRemindersBody::default(),
61        }
62    }
63
64    /// 设置提醒时间列表
65    pub fn reminders(mut self, reminders: Vec<String>) -> Self {
66        self.body.reminders = reminders;
67        self
68    }
69
70    /// 添加单个提醒时间
71    pub fn add_reminder(mut self, reminder: impl Into<String>) -> Self {
72        self.body.reminders.push(reminder.into());
73        self
74    }
75
76    /// 执行请求
77    pub async fn execute(self) -> SDKResult<AddRemindersResponse> {
78        self.execute_with_options(openlark_core::req_option::RequestOption::default())
79            .await
80    }
81
82    /// 执行请求(带选项)
83    pub async fn execute_with_options(
84        self,
85        option: openlark_core::req_option::RequestOption,
86    ) -> SDKResult<AddRemindersResponse> {
87        // 验证必填字段
88        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
89        validate_required!(self.body.reminders, "提醒时间列表不能为空");
90
91        let api_endpoint = TaskApiV2::TaskAddReminders(self.task_guid.clone());
92        let mut request = ApiRequest::<AddRemindersResponse>::post(api_endpoint.to_url());
93
94        let request_body = &self.body;
95        request = request.body(serialize_params(request_body, "添加任务提醒")?);
96
97        let response =
98            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
99        extract_response_data(response, "添加任务提醒")
100    }
101}
102
103impl ApiResponseTrait for AddRemindersResponse {
104    fn data_format() -> ResponseFormat {
105        ResponseFormat::Data
106    }
107}
108
109#[cfg(test)]
110#[allow(unused_imports)]
111mod tests {
112    use std::sync::Arc;
113
114    use super::*;
115
116    #[test]
117    fn test_add_reminders_builder() {
118        let config = Arc::new(
119            openlark_core::config::Config::builder()
120                .app_id("test")
121                .app_secret("test")
122                .build(),
123        );
124
125        let request = AddRemindersRequest::new(config, "task_123")
126            .reminders(vec!["2024-01-01T09:00:00Z".to_string()]);
127
128        assert_eq!(request.task_guid, "task_123");
129        assert_eq!(request.body.reminders, vec!["2024-01-01T09:00:00Z"]);
130    }
131
132    #[test]
133    fn test_add_reminder_single() {
134        let config = Arc::new(
135            openlark_core::config::Config::builder()
136                .app_id("test")
137                .app_secret("test")
138                .build(),
139        );
140
141        let request = AddRemindersRequest::new(config, "task_123")
142            .add_reminder("2024-01-01T09:00:00Z")
143            .add_reminder("2024-01-02T09:00:00Z");
144
145        assert_eq!(
146            request.body.reminders,
147            vec!["2024-01-01T09:00:00Z", "2024-01-02T09:00:00Z"]
148        );
149    }
150
151    #[test]
152    fn test_task_add_reminders_api_v2_url() {
153        let endpoint = TaskApiV2::TaskAddReminders("task_123".to_string());
154        assert_eq!(
155            endpoint.to_url(),
156            "/open-apis/task/v2/tasks/task_123/add_reminders"
157        );
158    }
159}