restflow_core/services/
triggers.rs

1use crate::{
2    AppCore,
3    engine::trigger_manager::{TriggerStatus, WebhookResponse},
4    models::ActiveTrigger,
5};
6use anyhow::{Context, Result};
7use serde_json::Value;
8use std::collections::HashMap;
9use std::sync::Arc;
10use uuid::Uuid;
11
12// Trigger management functions
13
14pub fn generate_test_execution_id() -> String {
15    format!("test-{}", Uuid::new_v4())
16}
17
18pub async fn activate_workflow(core: &Arc<AppCore>, workflow_id: &str) -> Result<()> {
19    core.trigger_manager
20        .activate_workflow(workflow_id)
21        .await
22        .with_context(|| format!("Failed to activate workflow {}", workflow_id))?;
23    Ok(())
24}
25
26pub async fn deactivate_workflow(core: &Arc<AppCore>, workflow_id: &str) -> Result<()> {
27    core.trigger_manager
28        .deactivate_workflow(workflow_id)
29        .await
30        .with_context(|| format!("Failed to deactivate workflow {}", workflow_id))
31}
32
33pub async fn get_workflow_trigger_status(
34    core: &Arc<AppCore>,
35    workflow_id: &str,
36) -> Result<Option<TriggerStatus>> {
37    core.trigger_manager
38        .get_trigger_status(workflow_id)
39        .await
40        .with_context(|| format!("Failed to get trigger status for workflow {}", workflow_id))
41}
42
43pub async fn test_workflow_trigger(
44    core: &Arc<AppCore>,
45    workflow_id: &str,
46    test_input: Value,
47) -> Result<String> {
48    let test_execution_id = generate_test_execution_id();
49    core.executor
50        .submit_with_execution_id(workflow_id.to_string(), test_input, test_execution_id)
51        .await
52        .with_context(|| format!("Test execution failed for workflow {}", workflow_id))
53}
54
55pub async fn handle_webhook_trigger(
56    core: &Arc<AppCore>,
57    webhook_id: &str,
58    method: &str,
59    headers: HashMap<String, String>,
60    body: Value,
61) -> Result<WebhookResponse> {
62    // Use the trigger manager to handle webhook properly
63    core.trigger_manager
64        .handle_webhook(webhook_id, method, headers, body)
65        .await
66        .with_context(|| format!("Webhook handling failed for {}", webhook_id))
67}
68
69pub async fn list_active_triggers(core: &Arc<AppCore>) -> Result<Vec<ActiveTrigger>> {
70    core.storage
71        .triggers
72        .list_active_triggers()
73        .context("Failed to list active triggers")
74}