Skip to main content

zlayer_types/api/
tasks.rs

1//! Task CRUD and execution DTOs.
2
3use serde::{Deserialize, Serialize};
4
5use crate::storage::TaskKind;
6
7/// Query for `GET /api/v1/tasks`.
8#[derive(Debug, Deserialize, Default)]
9pub struct ListTasksQuery {
10    /// Filter by project id. When omitted, all tasks (global + project-scoped)
11    /// are returned.
12    #[serde(default)]
13    pub project_id: Option<String>,
14}
15
16/// Body for `POST /api/v1/tasks`.
17#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
18pub struct CreateTaskRequest {
19    /// Task name.
20    pub name: String,
21    /// Script type.
22    pub kind: TaskKind,
23    /// The script/command body.
24    pub body: String,
25    /// Project id scope. `None` = global task.
26    #[serde(default)]
27    pub project_id: Option<String>,
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_create_request_deserialize_minimum() {
36        let req: CreateTaskRequest =
37            serde_json::from_str(r#"{"name":"build","kind":"bash","body":"cargo build"}"#).unwrap();
38        assert_eq!(req.name, "build");
39        assert_eq!(req.kind, TaskKind::Bash);
40        assert_eq!(req.body, "cargo build");
41        assert!(req.project_id.is_none());
42    }
43
44    #[test]
45    fn test_create_request_deserialize_full() {
46        let req: CreateTaskRequest = serde_json::from_str(
47            r#"{"name":"build","kind":"bash","body":"cargo build","project_id":"proj-1"}"#,
48        )
49        .unwrap();
50        assert_eq!(req.name, "build");
51        assert_eq!(req.kind, TaskKind::Bash);
52        assert_eq!(req.body, "cargo build");
53        assert_eq!(req.project_id.as_deref(), Some("proj-1"));
54    }
55
56    #[test]
57    fn test_list_query_default_is_empty() {
58        let q = ListTasksQuery::default();
59        assert!(q.project_id.is_none());
60    }
61}