use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
validate_required,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Default)]
pub struct CreateSubtaskBody {
pub summary: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub assignee: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub due: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub completed_at: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CreateSubtaskResponse {
pub task_guid: String,
pub summary: String,
#[serde(default)]
pub description: Option<String>,
pub status: String,
pub parent_task_guid: String,
pub created_at: String,
pub updated_at: String,
}
#[derive(Debug, Clone)]
pub struct CreateSubtaskRequest {
config: Arc<Config>,
task_guid: String,
body: CreateSubtaskBody,
}
impl CreateSubtaskRequest {
pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
Self {
config,
task_guid: task_guid.into(),
body: CreateSubtaskBody::default(),
}
}
pub fn summary(mut self, summary: impl Into<String>) -> Self {
self.body.summary = summary.into();
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.body.description = Some(description.into());
self
}
pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
self.body.assignee = Some(assignee.into());
self
}
pub fn due(mut self, due: impl Into<String>) -> Self {
self.body.due = Some(due.into());
self
}
pub fn completed_at(mut self, completed_at: impl Into<String>) -> Self {
self.body.completed_at = Some(completed_at.into());
self
}
pub async fn execute(self) -> SDKResult<CreateSubtaskResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<CreateSubtaskResponse> {
validate_required!(self.body.summary.trim(), "子任务标题不能为空");
validate_required!(self.task_guid.trim(), "父任务 GUID 不能为空");
let api_endpoint = TaskApiV2::SubtaskCreate(self.task_guid.clone());
let mut request = ApiRequest::<CreateSubtaskResponse>::post(api_endpoint.to_url());
let request_body = &self.body;
request = request.body(serialize_params(request_body, "创建子任务")?);
let response =
openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
extract_response_data(response, "创建子任务")
}
}
impl ApiResponseTrait for CreateSubtaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_create_subtask_builder() {
let config = Arc::new(
openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build(),
);
let request = CreateSubtaskRequest::new(config, "parent_task_123")
.summary("测试子任务")
.description("子任务描述")
.assignee("user_123");
assert_eq!(request.task_guid, "parent_task_123");
assert_eq!(request.body.summary, "测试子任务");
assert_eq!(request.body.description, Some("子任务描述".to_string()));
assert_eq!(request.body.assignee, Some("user_123".to_string()));
}
#[test]
fn test_subtask_api_v2_url() {
let endpoint = TaskApiV2::SubtaskCreate("task_123".to_string());
assert_eq!(
endpoint.to_url(),
"/open-apis/task/v2/tasks/task_123/subtasks"
);
}
}