zabbix_export:
  version: '6.4'
  media_types:
    - name: 'Race condition test'
      type: WEBHOOK
      parameters:
        - name: event_id
          value: '{EVENT.ID}'
        - name: event_update_status
          value: '{EVENT.UPDATE.STATUS}'
        - name: event_value
          value: '{EVENT.VALUE}'
        - name: external_ticket_id
          value: '{EVENT.TAGS.__zbx_external_ticket_id}'
      max_sessions: '5'
      attempts: '1'
      script: |
        try {
            var params = JSON.parse(value);
            var result = { tags: { sent: 1 }};
        
            if (params.event_value === '1' && params.event_update_status === '0') {
                console.log('[ RACE CONDITION TEST ] NEW TICKET FOR EVENT ID: ' + params.event_id);
                result.tags['__zbx_external_ticket_id'] = params.event_id;
            } else if (params.event_value === '0' && params.event_update_status === '0') {
                console.log('[ RACE CONDITION TEST ] CLOSE TICKET FOR EVENT ID: ' + params.external_ticket_id);
                if (params.external_ticket_id == '{EVENT.TAGS.__zbx_external_ticket_id}') {
                    throw new Error('Ticket ID is missing');
                }
            }
        
            return JSON.stringify(result);
        } catch (error) {
            console.error('[ RACE CONDITION TEST ] ERROR: ' + error.message)
            throw '[ RACE CONDITION TEST ] ERROR: ' + error.message;
        }
      process_tags: 'YES'
      message_templates:
        - event_source: TRIGGERS
          operation_mode: PROBLEM
          subject: '[ZBX#{EVENT.ID}] PROBLEM: {HOST.NAME} - {EVENT.NAME}'
        - event_source: TRIGGERS
          operation_mode: RECOVERY
          subject: '[ZBX#{EVENT.ID}] RESOLVED: {HOST.NAME} - {EVENT.NAME}'
        - event_source: TRIGGERS
          operation_mode: UPDATE
          subject: '[ZBX#{EVENT.ID}] UPDATE: {HOST.NAME} - {EVENT.NAME}'
