Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
195 lines
7.6 KiB
Go
195 lines
7.6 KiB
Go
package automation
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
// ===========================
|
|
// JSON types for conditions and actions
|
|
// ===========================
|
|
|
|
// Condition represents a single filter condition in an automation rule.
|
|
// Reference: Chatwoot automation_rule conditions — {attribute_key, filter_operator, values, query_operator}
|
|
type Condition struct {
|
|
Attribute string `json:"attribute,omitempty"`
|
|
AttributeKey string `json:"attribute_key,omitempty" gorm:"-"`
|
|
FilterOperator string `json:"filter_operator"`
|
|
Values []string `json:"values"`
|
|
QueryOperator string `json:"query_operator,omitempty"` // "and" or "or"
|
|
CustomAttributeType string `json:"custom_attribute_type,omitempty"`
|
|
}
|
|
|
|
// Conditions is a slice of Condition that implements GORM's JSON-valued column type.
|
|
type Conditions []Condition
|
|
|
|
func (c Conditions) Value() (driver.Value, error) {
|
|
if c == nil {
|
|
return json.Marshal([]Condition{})
|
|
}
|
|
return json.Marshal(c)
|
|
}
|
|
|
|
func (c *Conditions) Scan(value interface{}) error {
|
|
if value == nil {
|
|
*c = Conditions{}
|
|
return nil
|
|
}
|
|
bytes, ok := value.([]byte)
|
|
if !ok {
|
|
return fmt.Errorf("failed to unmarshal Conditions value: %v", value)
|
|
}
|
|
return json.Unmarshal(bytes, c)
|
|
}
|
|
|
|
// Action represents a single action to execute when an automation rule matches.
|
|
// Reference: Chatwoot automation_rule actions — {action_name, action_params}
|
|
type Action struct {
|
|
ActionName string `json:"action_name"`
|
|
ActionParams map[string]interface{} `json:"action_params"`
|
|
}
|
|
|
|
// Actions is a slice of Action that implements GORM's JSON-valued column type.
|
|
type Actions []Action
|
|
|
|
func (a Actions) Value() (driver.Value, error) {
|
|
if a == nil {
|
|
return json.Marshal([]Action{})
|
|
}
|
|
return json.Marshal(a)
|
|
}
|
|
|
|
func (a *Actions) Scan(value interface{}) error {
|
|
if value == nil {
|
|
*a = Actions{}
|
|
return nil
|
|
}
|
|
bytes, ok := value.([]byte)
|
|
if !ok {
|
|
return fmt.Errorf("failed to unmarshal Actions value: %v", value)
|
|
}
|
|
return json.Unmarshal(bytes, a)
|
|
}
|
|
|
|
// ===========================
|
|
// AutomationRule model
|
|
// ===========================
|
|
|
|
// AutomationRule represents an automation rule that triggers actions on events.
|
|
// Reference: Chatwoot AutomationRule — account_id, event_name, conditions, actions, active, name, description
|
|
type AutomationRule struct {
|
|
model.Base
|
|
AccountID uint `gorm:"index;not null" json:"account_id"`
|
|
EventName string `gorm:"size:100;index;not null" json:"event_name"` // e.g. "conversation_created", "message_created"
|
|
Name string `gorm:"size:255;not null" json:"name"`
|
|
Description string `gorm:"type:text" json:"description,omitempty"`
|
|
Conditions Conditions `gorm:"type:jsonb;default:'[]'" json:"conditions"`
|
|
Actions Actions `gorm:"type:jsonb;default:'[]'" json:"actions"`
|
|
Active bool `gorm:"not null" json:"active"`
|
|
ActiveAt *time.Time `gorm:"index" json:"active_at,omitempty"`
|
|
InactiveAt *time.Time `gorm:"index" json:"inactive_at,omitempty"`
|
|
Files []AutomationRuleFile `gorm:"-" json:"files,omitempty"`
|
|
}
|
|
|
|
func (AutomationRule) TableName() string { return "automation_rules" }
|
|
|
|
type AutomationRuleFile struct {
|
|
ID uint `json:"id"`
|
|
AutomationRuleID uint `json:"automation_rule_id"`
|
|
FileType string `json:"file_type"`
|
|
AccountID uint `json:"account_id"`
|
|
FileURL string `json:"file_url"`
|
|
BlobID uint `json:"blob_id"`
|
|
Filename string `json:"filename"`
|
|
}
|
|
|
|
// ===========================
|
|
// Macro model
|
|
// ===========================
|
|
|
|
// MacroVisibility defines the visibility level of a macro.
|
|
// Reference: Chatwoot Macro visibility — 0=personal, 1=global
|
|
type MacroVisibility int
|
|
|
|
const (
|
|
MacroVisibilityPersonal MacroVisibility = 0
|
|
MacroVisibilityGlobal MacroVisibility = 1
|
|
)
|
|
|
|
// Macro represents a user-defined macro that executes a set of actions on a conversation.
|
|
// Reference: Chatwoot Macro — account_id, name, actions, visibility, created_by_id, updated_by_id
|
|
type Macro struct {
|
|
model.Base
|
|
AccountID uint `gorm:"index;not null" json:"account_id"`
|
|
Name string `gorm:"size:255;not null" json:"name"`
|
|
Actions Actions `gorm:"type:jsonb;default:'[]'" json:"actions"`
|
|
Visibility MacroVisibility `gorm:"default:0" json:"visibility"` // 0=personal, 1=global
|
|
Active bool `gorm:"not null" json:"active"` // enable/disable macro execution
|
|
CreatedByID uint `gorm:"index;not null" json:"created_by_id"`
|
|
UpdatedByID uint `gorm:"index;not null" json:"updated_by_id"`
|
|
CreatedBy *model.User `gorm:"foreignKey:CreatedByID" json:"created_by,omitempty"`
|
|
UpdatedBy *model.User `gorm:"foreignKey:UpdatedByID" json:"updated_by,omitempty"`
|
|
Files []MacroFile `gorm:"-" json:"files,omitempty"`
|
|
}
|
|
|
|
func (Macro) TableName() string { return "macros" }
|
|
|
|
type MacroFile struct {
|
|
ID uint `json:"id"`
|
|
MacroID uint `json:"macro_id"`
|
|
FileType string `json:"file_type"`
|
|
AccountID uint `json:"account_id"`
|
|
FileURL string `json:"file_url"`
|
|
BlobID uint `json:"blob_id"`
|
|
Filename string `json:"filename"`
|
|
}
|
|
|
|
// ===========================
|
|
// CsatSurveyResponse model
|
|
// ===========================
|
|
|
|
// CsatSurveyResponse captures customer satisfaction feedback after a conversation is resolved.
|
|
// Reference: Chatwoot CsatSurveyResponse — rating, feedback_message, conversation_id, account_id, assigned_agent_id
|
|
type CsatSurveyResponse struct {
|
|
model.Base
|
|
AccountID uint `gorm:"index;not null" json:"account_id"`
|
|
ConversationID uint `gorm:"index;not null" json:"conversation_id"`
|
|
ContactID uint `gorm:"index" json:"contact_id"`
|
|
MessageID *uint `gorm:"uniqueIndex" json:"message_id,omitempty"`
|
|
AssignedAgentID *uint `gorm:"index" json:"assigned_agent_id,omitempty"`
|
|
Rating int `gorm:"not null" json:"rating"` // 1-5 scale
|
|
FeedbackMessage string `gorm:"type:text" json:"feedback_message,omitempty"`
|
|
CsatReviewNotes string `gorm:"type:text" json:"csat_review_notes,omitempty"`
|
|
ReviewNotesUpdatedByID *uint `gorm:"index" json:"review_notes_updated_by_id,omitempty"`
|
|
ReviewNotesUpdatedAt *time.Time `json:"review_notes_updated_at,omitempty"`
|
|
}
|
|
|
|
func (CsatSurveyResponse) TableName() string { return "csat_survey_responses" }
|
|
|
|
// ===========================
|
|
// AutomationExecution model
|
|
// ===========================
|
|
|
|
// AutomationExecution records an automation rule execution event for audit trail.
|
|
// Reference: Chatwoot does not explicitly store automation execution logs —
|
|
// gochat adds this for debugging, monitoring, and compliance.
|
|
type AutomationExecution struct {
|
|
model.Base
|
|
AccountID uint `gorm:"index;not null" json:"account_id"`
|
|
RuleID uint `gorm:"index;not null" json:"rule_id"`
|
|
ConversationID uint `gorm:"index;not null" json:"conversation_id"`
|
|
EventName string `gorm:"size:100;index" json:"event_name,omitempty"`
|
|
Status string `gorm:"size:50;not null" json:"status"` // success, partial, failed, skipped
|
|
ActionsExecuted int `gorm:"default:0" json:"actions_executed"`
|
|
ActionsFailed int `gorm:"default:0" json:"actions_failed"`
|
|
ActionResults datatypes.JSON `gorm:"type:jsonb" json:"action_results,omitempty"`
|
|
ErrorMessage string `gorm:"type:text" json:"error_message,omitempty"`
|
|
}
|
|
|
|
func (AutomationExecution) TableName() string { return "automation_executions" }
|