restflow_core/models/
trigger.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use ts_rs::TS;
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TS)]
6#[serde(tag = "type", rename_all = "lowercase")]
7#[ts(export)]
8pub enum TriggerConfig {
9 Manual,
10 Webhook {
11 path: String,
12 method: String, auth: Option<AuthConfig>,
14 },
16 Schedule {
17 cron: String,
18 timezone: Option<String>,
19 #[ts(type = "any")]
20 payload: Option<Value>,
21 },
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TS)]
25#[serde(tag = "type")]
26#[ts(export)]
27pub enum AuthConfig {
28 None,
29 ApiKey {
30 key: String,
31 header_name: Option<String>, },
33 Basic {
34 username: String,
35 password: String,
36 },
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize, TS)]
43#[ts(export)]
44pub struct ActiveTrigger {
45 pub id: String,
46 pub workflow_id: String,
47 pub trigger_config: TriggerConfig,
48 pub activated_at: i64,
49 pub last_triggered_at: Option<i64>,
50 pub trigger_count: u64,
51}
52
53impl ActiveTrigger {
54 pub fn new(workflow_id: String, trigger_config: TriggerConfig) -> Self {
55 Self {
56 id: uuid::Uuid::new_v4().to_string(),
57 workflow_id,
58 trigger_config,
59 activated_at: chrono::Utc::now().timestamp(),
60 last_triggered_at: None,
61 trigger_count: 0,
62 }
63 }
64
65 pub fn record_trigger(&mut self) {
66 self.last_triggered_at = Some(chrono::Utc::now().timestamp());
67 self.trigger_count += 1;
68 }
69}