webhook

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArticleWebhookData

type ArticleWebhookData struct {
	ID        int       `json:"id"`
	TicketID  int       `json:"ticket_id"`
	From      string    `json:"from"`
	To        string    `json:"to"`
	Subject   string    `json:"subject"`
	Body      string    `json:"body"`
	Type      string    `json:"type"`
	Visible   bool      `json:"visible"`
	CreatedAt time.Time `json:"created_at"`
}

ArticleWebhookData represents article data in webhook payloads

type DeliveryRepository

type DeliveryRepository interface {
	Create(delivery *WebhookDelivery) error
	GetByID(id uint) (*WebhookDelivery, error)
	ListByWebhookID(webhookID uint, limit int) ([]*WebhookDelivery, error)
	Update(delivery *WebhookDelivery) error
	GetPendingRetries() ([]*WebhookDelivery, error)
	CleanupOldDeliveries(olderThan time.Time) error
}

DeliveryRepository interface for webhook delivery storage operations

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles webhook operations

func NewManager

func NewManager(webhookRepo WebhookRepository, deliveryRepo DeliveryRepository) *Manager

NewManager creates a new webhook manager

func (*Manager) CreateWebhook

func (m *Manager) CreateWebhook(req WebhookRequest) (*Webhook, error)

CreateWebhook creates a new webhook

func (*Manager) DeleteWebhook

func (m *Manager) DeleteWebhook(id uint) error

DeleteWebhook removes a webhook

func (*Manager) GetDeliveries

func (m *Manager) GetDeliveries(webhookID uint, limit int) ([]*WebhookDelivery, error)

GetDeliveries returns delivery history for a webhook

func (*Manager) GetWebhook

func (m *Manager) GetWebhook(id uint) (*Webhook, error)

GetWebhook retrieves a webhook by ID

func (*Manager) GetWebhookStatistics

func (m *Manager) GetWebhookStatistics(webhookID uint) (*WebhookStatistics, error)

GetWebhookStatistics returns statistics for a webhook

func (*Manager) ListWebhooks

func (m *Manager) ListWebhooks() ([]*Webhook, error)

ListWebhooks returns all webhooks

func (*Manager) Stop

func (m *Manager) Stop()

Stop gracefully shuts down the webhook manager

func (*Manager) TestWebhook

func (m *Manager) TestWebhook(webhookID uint) (*WebhookTestResult, error)

TestWebhook tests a webhook by sending a test payload

func (*Manager) TriggerEvent

func (m *Manager) TriggerEvent(event WebhookEvent, data interface{}, previousData interface{}) error

TriggerEvent triggers webhooks for a specific event

func (*Manager) UpdateWebhook

func (m *Manager) UpdateWebhook(id uint, req WebhookRequest) (*Webhook, error)

UpdateWebhook updates an existing webhook

type QueueWebhookData

type QueueWebhookData struct {
	ID          int       `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description,omitempty"`
	GroupID     int       `json:"group_id"`
	Active      bool      `json:"active"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

QueueWebhookData represents queue data in webhook payloads

type TicketWebhookData

type TicketWebhookData struct {
	ID            int        `json:"id"`
	Number        string     `json:"number"`
	Title         string     `json:"title"`
	Description   string     `json:"description,omitempty"`
	Status        string     `json:"status"`
	Priority      string     `json:"priority"`
	QueueID       int        `json:"queue_id"`
	QueueName     string     `json:"queue_name"`
	AssignedTo    *int       `json:"assigned_to"`
	AssignedName  *string    `json:"assigned_name"`
	CustomerID    string     `json:"customer_id"`
	CustomerEmail string     `json:"customer_email"`
	Tags          []string   `json:"tags"`
	CreatedAt     time.Time  `json:"created_at"`
	UpdatedAt     time.Time  `json:"updated_at"`
	SLADue        *time.Time `json:"sla_due,omitempty"`
}

TicketWebhookData represents ticket data in webhook payloads

type UserWebhookData

type UserWebhookData struct {
	ID        int       `json:"id"`
	Email     string    `json:"email"`
	FirstName string    `json:"first_name"`
	LastName  string    `json:"last_name"`
	Role      string    `json:"role"`
	Active    bool      `json:"active"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

UserWebhookData represents user data in webhook payloads

type Webhook

type Webhook struct {
	ID          uint           `json:"id" db:"id"`
	Name        string         `json:"name" db:"name"`
	URL         string         `json:"url" db:"url"`
	Secret      string         `json:"secret,omitempty" db:"secret"` // HMAC secret for signature verification
	Events      []WebhookEvent `json:"events" db:"events"`
	Status      WebhookStatus  `json:"status" db:"status"`
	Description string         `json:"description" db:"description"`

	// Configuration
	RetryCount    int           `json:"retry_count" db:"retry_count"`
	RetryInterval time.Duration `json:"retry_interval" db:"retry_interval"`
	Timeout       time.Duration `json:"timeout" db:"timeout"`

	// Headers to include in webhook requests
	Headers map[string]string `json:"headers,omitempty" db:"headers"`

	// Filters for conditional webhook execution
	Filters WebhookFilters `json:"filters,omitempty" db:"filters"`

	// Statistics
	TotalDeliveries      int        `json:"total_deliveries" db:"total_deliveries"`
	SuccessfulDeliveries int        `json:"successful_deliveries" db:"successful_deliveries"`
	FailedDeliveries     int        `json:"failed_deliveries" db:"failed_deliveries"`
	LastDeliveryAt       *time.Time `json:"last_delivery_at,omitempty" db:"last_delivery_at"`
	LastSuccessAt        *time.Time `json:"last_success_at,omitempty" db:"last_success_at"`
	LastFailureAt        *time.Time `json:"last_failure_at,omitempty" db:"last_failure_at"`

	// Metadata
	CreatedBy uint      `json:"created_by" db:"created_by"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

Webhook represents a webhook endpoint configuration

type WebhookDelivery

type WebhookDelivery struct {
	ID        uint                  `json:"id" db:"id"`
	WebhookID uint                  `json:"webhook_id" db:"webhook_id"`
	Event     WebhookEvent          `json:"event" db:"event"`
	Payload   interface{}           `json:"payload" db:"payload"`
	Status    WebhookDeliveryStatus `json:"status" db:"status"`

	// Request details
	RequestURL     string            `json:"request_url" db:"request_url"`
	RequestHeaders map[string]string `json:"request_headers" db:"request_headers"`
	RequestBody    string            `json:"request_body" db:"request_body"`

	// Response details
	ResponseStatusCode int               `json:"response_status_code,omitempty" db:"response_status_code"`
	ResponseHeaders    map[string]string `json:"response_headers,omitempty" db:"response_headers"`
	ResponseBody       string            `json:"response_body,omitempty" db:"response_body"`

	// Timing
	AttemptCount int           `json:"attempt_count" db:"attempt_count"`
	Duration     time.Duration `json:"duration" db:"duration"`
	NextRetryAt  *time.Time    `json:"next_retry_at,omitempty" db:"next_retry_at"`

	// Error information
	ErrorMessage string `json:"error_message,omitempty" db:"error_message"`

	// Metadata
	CreatedAt time.Time `json:"created_at" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

WebhookDelivery represents a webhook delivery attempt

type WebhookDeliveryStatus

type WebhookDeliveryStatus string

WebhookDeliveryStatus represents the status of a webhook delivery attempt

const (
	DeliveryPending  WebhookDeliveryStatus = "pending"
	DeliverySuccess  WebhookDeliveryStatus = "success"
	DeliveryFailed   WebhookDeliveryStatus = "failed"
	DeliveryRetrying WebhookDeliveryStatus = "retrying"
	DeliveryExpired  WebhookDeliveryStatus = "expired"
)

type WebhookEvent

type WebhookEvent string

WebhookEvent represents the type of event that triggers a webhook

const (
	EventTicketCreated         WebhookEvent = "ticket.created"
	EventTicketUpdated         WebhookEvent = "ticket.updated"
	EventTicketClosed          WebhookEvent = "ticket.closed"
	EventTicketReopened        WebhookEvent = "ticket.reopened"
	EventTicketAssigned        WebhookEvent = "ticket.assigned"
	EventTicketEscalated       WebhookEvent = "ticket.escalated"
	EventTicketPriorityChanged WebhookEvent = "ticket.priority_changed"
	EventTicketStatusChanged   WebhookEvent = "ticket.status_changed"
	EventTicketQueueMoved      WebhookEvent = "ticket.queue_moved"

	EventArticleAdded   WebhookEvent = "article.added"
	EventArticleUpdated WebhookEvent = "article.updated"

	EventUserCreated     WebhookEvent = "user.created"
	EventUserUpdated     WebhookEvent = "user.updated"
	EventUserActivated   WebhookEvent = "user.activated"
	EventUserDeactivated WebhookEvent = "user.deactivated"

	EventQueueCreated WebhookEvent = "queue.created"
	EventQueueUpdated WebhookEvent = "queue.updated"
	EventQueueDeleted WebhookEvent = "queue.deleted"

	EventAttachmentUploaded WebhookEvent = "attachment.uploaded"
	EventAttachmentDeleted  WebhookEvent = "attachment.deleted"

	EventSystemMaintenance WebhookEvent = "system.maintenance"
	EventSystemBackup      WebhookEvent = "system.backup"
	EventSystemAlert       WebhookEvent = "system.alert"
)

type WebhookFilters

type WebhookFilters struct {
	// Queue filters - only trigger for specific queues
	QueueIDs []int `json:"queue_ids,omitempty"`

	// Priority filters - only trigger for specific priorities
	Priorities []string `json:"priorities,omitempty"`

	// Status filters - only trigger for specific statuses
	Statuses []string `json:"statuses,omitempty"`

	// User filters - only trigger for specific users
	UserIDs []int `json:"user_ids,omitempty"`

	// Custom field filters
	CustomFields map[string]interface{} `json:"custom_fields,omitempty"`
}

WebhookFilters defines conditions for when webhooks should be triggered

type WebhookPayload

type WebhookPayload struct {
	// Event information
	Event     WebhookEvent `json:"event"`
	Timestamp time.Time    `json:"timestamp"`
	ID        string       `json:"id"` // Unique delivery ID

	// Source information
	Source struct {
		Name    string `json:"name"`
		Version string `json:"version"`
		URL     string `json:"url"`
	} `json:"source"`

	// Event data - the actual payload varies by event type
	Data interface{} `json:"data"`

	// Previous data (for update events)
	PreviousData interface{} `json:"previous_data,omitempty"`
}

WebhookPayload represents the structure of webhook payloads sent to external systems

type WebhookRepository

type WebhookRepository interface {
	Create(webhook *Webhook) error
	GetByID(id uint) (*Webhook, error)
	List() ([]*Webhook, error)
	ListActive() ([]*Webhook, error)
	Update(webhook *Webhook) error
	Delete(id uint) error
	UpdateStatistics(id uint, stats WebhookStatistics) error
}

WebhookRepository interface for webhook storage operations

type WebhookRequest

type WebhookRequest struct {
	Name        string            `json:"name" binding:"required"`
	URL         string            `json:"url" binding:"required,url"`
	Events      []WebhookEvent    `json:"events" binding:"required,min=1"`
	Secret      string            `json:"secret"`
	Description string            `json:"description"`
	Headers     map[string]string `json:"headers"`
	Filters     WebhookFilters    `json:"filters"`

	// Configuration
	RetryCount    int `json:"retry_count"`
	RetryInterval int `json:"retry_interval"` // seconds
	Timeout       int `json:"timeout"`        // seconds
}

WebhookRequest represents a request to create or update a webhook

type WebhookStatistics

type WebhookStatistics struct {
	WebhookID            uint       `json:"webhook_id"`
	TotalDeliveries      int        `json:"total_deliveries"`
	SuccessfulDeliveries int        `json:"successful_deliveries"`
	FailedDeliveries     int        `json:"failed_deliveries"`
	SuccessRate          float64    `json:"success_rate"`
	AverageResponseTime  int        `json:"average_response_time_ms"`
	LastDeliveryAt       *time.Time `json:"last_delivery_at"`
	LastSuccessAt        *time.Time `json:"last_success_at"`
	LastFailureAt        *time.Time `json:"last_failure_at"`
}

WebhookStatistics represents webhook usage statistics

type WebhookStatus

type WebhookStatus string

WebhookStatus represents the status of a webhook endpoint

const (
	StatusActive   WebhookStatus = "active"
	StatusInactive WebhookStatus = "inactive"
	StatusFailed   WebhookStatus = "failed"
	StatusDisabled WebhookStatus = "disabled"
)

type WebhookTestResult

type WebhookTestResult struct {
	Success      bool          `json:"success"`
	StatusCode   int           `json:"status_code"`
	ResponseTime time.Duration `json:"response_time_ms"`
	ResponseBody string        `json:"response_body"`
	ErrorMessage string        `json:"error_message,omitempty"`
	TestedAt     time.Time     `json:"tested_at"`
}

WebhookTestResult represents the result of testing a webhook

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL