All files / web/src/composables/triggers useWorkflowTriggers.ts

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

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                                                                                                                                                                                                                                                             
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed, ref } from 'vue'
import * as triggersApi from '../../api/triggers'
import type { TriggerStatus } from '@/types/generated/TriggerStatus'
import { isNodeATrigger } from '../node/useNodeHelpers'
import { useWorkflowStore } from '../../stores/workflowStore'
import { SUCCESS_MESSAGES, LOADING_MESSAGES } from '@/constants'
 
export function useWorkflowTriggers() {
  const workflowStore = useWorkflowStore()
  const loading = ref(false)
  // Use Record for reactive trigger status management
  const triggerStatusMap = ref<Record<string, TriggerStatus | null>>({})
 
  // Check if workflow has trigger nodes - using unified helper
  const hasTriggerNode = computed(() => {
    return workflowStore.nodes.some(isNodeATrigger)
  })
 
  // Unified error handling helper
  const handleError = (error: any, defaultMessage: string) => {
    const message = 
      error?.response?.data?.error || 
      error?.message || 
      defaultMessage
    console.error(defaultMessage, error)
    ElMessage.error(message)
    return message
  }
 
  const fetchTriggerStatus = async (workflowId: string) => {
    if (!workflowId) return
 
    try {
      loading.value = true
      const response = await triggersApi.getTriggerStatus(workflowId)
      if (response) {
        triggerStatusMap.value = { ...triggerStatusMap.value, [workflowId]: response }
      }
      return response
    } catch (error) {
      handleError(error, 'Failed to fetch trigger status')
      return null
    } finally {
      loading.value = false
    }
  }
 
  const activateTrigger = async (workflowId: string) => {
    if (!workflowId) {
      ElMessage.warning(LOADING_MESSAGES.SAVE_FIRST)
      return false
    }
 
    try {
      loading.value = true
      await triggersApi.activateWorkflow(workflowId)
      ElMessage.success(SUCCESS_MESSAGES.WORKFLOW_ACTIVATED)
 
      // Fetch the detailed status
      await fetchTriggerStatus(workflowId)
 
      return true
    } catch (error) {
      handleError(error, 'Failed to activate workflow')
      return false
    } finally {
      loading.value = false
    }
  }
 
  const deactivateTrigger = async (workflowId: string) => {
    if (!workflowId) return false
 
    try {
      const result = await ElMessageBox.confirm(
        'Are you sure you want to deactivate the workflow? It will not be automatically executed after deactivation.',
        'Deactivate Workflow',
        {
          confirmButtonText: 'Confirm',
          cancelButtonText: 'Cancel',
          type: 'warning',
        },
      )
 
      if (result === 'confirm') {
        loading.value = true
        await triggersApi.deactivateWorkflow(workflowId)
        ElMessage.success(SUCCESS_MESSAGES.WORKFLOW_DEACTIVATED)
        
        // Fetch the updated status
        await fetchTriggerStatus(workflowId)
 
        return true
      }
      return false
    } catch (error) {
      if (error !== 'cancel') {
        handleError(error, 'Failed to deactivate trigger')
      }
      return false
    } finally {
      loading.value = false
    }
  }
 
  const toggleTriggerStatus = async (workflowId: string) => {
    const currentStatus = triggerStatusMap.value[workflowId]
    if (!currentStatus) {
      await fetchTriggerStatus(workflowId)
    }
 
    const status = triggerStatusMap.value[workflowId]
    if (status?.is_active) {
      return await deactivateTrigger(workflowId)
    } else {
      return await activateTrigger(workflowId)
    }
  }
 
  const getTriggerStatus = (workflowId: string): TriggerStatus | undefined => {
    return triggerStatusMap.value[workflowId] || undefined
  }
 
  const fetchAllTriggerStatuses = async (workflowIds: string[]) => {
    // Use Promise.allSettled to handle partial failures gracefully
    const promises = workflowIds.map((id) => fetchTriggerStatus(id))
    await Promise.allSettled(promises)
  }
 
  return {
    loading,
    triggerStatusMap,
    hasTriggerNode,
    fetchTriggerStatus,
    fetchAllTriggerStatuses,
    getTriggerStatus,
    activateTrigger,
    deactivateTrigger,
    toggleTriggerStatus,
  }
}