Files
gochat/internal/model/sla_policy.go
T

102 lines
5.7 KiB
Go

package model
import (
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
// SlaPolicy represents a SLA policy for an account.
// Reference: Chatwoot enterprise/app/models/sla_policy.rb
// Fields: first_response_time_threshold (FRT), next_response_time_threshold (NRT),
// resolution_time_threshold (RT), only_during_business_hours
type SlaPolicy struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
Name string `gorm:"size:255;not null" json:"name"`
Description string `gorm:"type:text" json:"description"`
FirstResponseTimeThreshold int `gorm:"default:0" json:"first_response_time_threshold"` // FRT in seconds
NextResponseTimeThreshold int `gorm:"default:0" json:"next_response_time_threshold"` // NRT in seconds
ResolutionTimeThreshold int `gorm:"default:0" json:"resolution_time_threshold"` // RT in seconds
OnlyDuringBusinessHours bool `gorm:"default:false" json:"only_during_business_hours"`
PausedAt *time.Time `gorm:"index" json:"paused_at,omitempty"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
AppliedSLAs []AppliedSLA `gorm:"foreignKey:SlaPolicyID" json:"applied_slas,omitempty"`
SlaPolicyInboxes []SlaPolicyInbox `gorm:"foreignKey:SlaPolicyID" json:"inboxes,omitempty"`
}
func (SlaPolicy) TableName() string { return "sla_policies" }
// SlaPolicyInbox represents the join between SlaPolicy and Inbox.
// An SLA policy can be associated with specific inboxes.
// Reference: Chatwoot app/models/sla_policy_inbox.rb
type SlaPolicyInbox struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
InboxID uint `gorm:"not null;uniqueIndex:idx_sla_inbox_unique" json:"inbox_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
SlaPolicy SlaPolicy `gorm:"foreignKey:SlaPolicyID" json:"sla_policy,omitempty"`
Inbox Inbox `gorm:"foreignKey:InboxID" json:"inbox,omitempty"`
}
func (SlaPolicyInbox) TableName() string { return "sla_policy_inboxes" }
// AppliedSLA represents a SLA policy applied to a conversation.
// Reference: Chatwoot app/models/applied_sla.rb
// Tracks SLA status, target timestamps (FRT/NRT/RT), and actual completion times.
type AppliedSLA struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
SLAStatus SLAStatus `gorm:"size:50;not null;default:active" json:"sla_status"`
// Target timestamps for FRT/NRT/RT (computed from policy + created_at)
FRTTargetAt *time.Time `gorm:"index" json:"frt_target_at,omitempty"`
NRTTargetAt *time.Time `gorm:"index" json:"nrt_target_at,omitempty"`
RTTargetAt *time.Time `gorm:"index" json:"rt_target_at,omitempty"`
// Actual timestamps when targets were hit or violated
FRTActualAt *time.Time `json:"frt_actual_at,omitempty"`
NRTActualAt *time.Time `json:"nrt_actual_at,omitempty"`
RTActualAt *time.Time `json:"rt_actual_at,omitempty"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
SlaPolicy SlaPolicy `gorm:"foreignKey:SlaPolicyID" json:"sla_policy,omitempty"`
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
SlaEvents []SlaEvent `gorm:"foreignKey:AppliedSlaID" json:"sla_events,omitempty"`
}
func (AppliedSLA) TableName() string { return "applied_slas" }
// SlaEvent represents an event related to a SLA applied to a conversation.
// Reference: Chatwoot app/models/sla_event.rb
// Fields: event_type (frt/nrt/rt), meta (jsonb), account_id, conversation_id, inbox_id, sla_policy_id
type SlaEvent struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
AppliedSlaID uint `gorm:"not null;index" json:"applied_sla_id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
InboxID uint `gorm:"not null;index" json:"inbox_id"`
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
EventType SLAEventType `gorm:"size:50;not null" json:"event_type"`
Meta datatypes.JSON `gorm:"type:jsonb" json:"meta,omitempty"` // e.g. {"message_id": 123} for NRT events
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
AppliedSLA AppliedSLA `gorm:"foreignKey:AppliedSlaID" json:"applied_sla,omitempty"`
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
}
func (SlaEvent) TableName() string { return "sla_events" }