All files / web/src/composables/trigger useWorkflowTrigger.ts

0% Statements 0/95
0% Branches 0/1
0% Functions 0/1
0% Lines 0/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                                                                                                                                                                                                                                   
import { ref, computed, type Ref } from 'vue'
import { ElMessage } from 'element-plus'
import * as triggersApi from '../../api/triggers'
import { SUCCESS_MESSAGES } from '@/constants'
import { isNodeATrigger, type AnyNode } from '../node/useNodeHelpers'
 
/**
 * Composable for managing workflow-level trigger activation
 * Handles activation/deactivation of ALL triggers in a workflow
 */
export function useWorkflowTrigger(
  workflowId: Readonly<Ref<string | null | undefined>>,
  nodes: Readonly<Ref<AnyNode[]>>
) {
  const isActive = ref(false)
  const isLoading = ref(false)
  const triggerCount = ref(0)
 
  const resetTriggerState = () => {
    isActive.value = false
    triggerCount.value = 0
  }
 
  const validateWorkflowId = (id: string | null | undefined): string | null => {
    if (!id) {
      ElMessage.error('No workflow selected')
      return null
    }
    return id
  }
 
  const loadTriggerStatus = async () => {
    const currentWorkflowId = workflowId.value
    if (!currentWorkflowId) {
      resetTriggerState()
      return
    }
 
    try {
      const status = await triggersApi.getTriggerStatus(currentWorkflowId)
      isActive.value = status?.is_active || false
      triggerCount.value = Number(status?.trigger_count || 0)
    } catch (error) {
      console.error('Failed to load trigger status:', error)
      resetTriggerState()
    }
  }
 
  /**
   * Unified trigger action handler (activate or deactivate)
   */
  const performTriggerAction = async (
    action: 'activate' | 'deactivate',
    apiCall: (id: string) => Promise<void>,
    successState: boolean
  ) => {
    const currentWorkflowId = validateWorkflowId(workflowId.value)
    if (!currentWorkflowId) return { success: false }
 
    isLoading.value = true
    try {
      await apiCall(currentWorkflowId)
      isActive.value = successState
 
      if (action === 'activate') {
        await loadTriggerStatus()
      } else {
        triggerCount.value = 0
      }
 
      const message = action === 'activate'
        ? SUCCESS_MESSAGES.WORKFLOW_ACTIVATED
        : SUCCESS_MESSAGES.WORKFLOW_DEACTIVATED
      ElMessage.success(message)
 
      return { success: true }
    } catch (error) {
      console.error(`Failed to ${action} workflow:`, error)
      ElMessage.error(`Failed to ${action} workflow triggers`)
      return { success: false, error }
    } finally {
      isLoading.value = false
    }
  }
 
  const activateWorkflow = () =>
    performTriggerAction('activate', triggersApi.activateWorkflow, true)
 
  const deactivateWorkflow = () =>
    performTriggerAction('deactivate', triggersApi.deactivateWorkflow, false)
 
  const toggleActivation = async () => {
    if (isActive.value) {
      return await deactivateWorkflow()
    } else {
      return await activateWorkflow()
    }
  }
 
  const hasTriggers = computed(() => nodes.value.some(isNodeATrigger))
 
  const statusText = computed(() => {
    if (!hasTriggers.value) return 'No triggers'
    return isActive.value ? 'Active' : 'Inactive'
  })
 
  return {
    // State
    isActive,
    isLoading,
    triggerCount,
    hasTriggers,
    statusText,
 
    // Methods
    loadTriggerStatus,
    activateWorkflow,
    deactivateWorkflow,
    toggleActivation,
  }
}