Files
gochat/backend/internal/model/contact.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
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.
2026-07-07 14:44:12 +08:00

89 lines
3.5 KiB
Go

package model
import (
"encoding/json"
"strings"
"gorm.io/datatypes"
"gorm.io/gorm"
)
// Contact represents a customer/end-user across channels.
// Reference: Chatwoot app/models/contact.rb — P2B M4 spec
type Contact struct {
Base
AccountID uint `gorm:"index;not null" json:"account_id"`
Name string `gorm:"size:255" json:"name"`
MiddleName string `gorm:"size:255;default:''" json:"middle_name,omitempty"`
LastName string `gorm:"size:255;default:''" json:"last_name,omitempty"`
Email string `gorm:"size:255;index" json:"email,omitempty"`
PhoneNumber string `gorm:"size:50;index" json:"phone_number,omitempty"`
AvatarURL string `gorm:"size:512" json:"avatar_url,omitempty"`
Identifier string `gorm:"size:255;index" json:"identifier,omitempty"`
CountryCode string `gorm:"size:10;default:''" json:"country_code,omitempty"`
Location string `gorm:"size:255;default:''" json:"location,omitempty"`
AdditionalAttributes datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"additional_attributes,omitempty"`
CustomAttributes datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"custom_attributes,omitempty"`
Blocked bool `gorm:"default:false" json:"blocked"`
ContactType string `gorm:"size:50;default:''" json:"contact_type,omitempty"`
SourceID string `gorm:"size:255" json:"source_id,omitempty"`
CompanyID *uint `json:"company_id,omitempty"`
LastActivityAt *int64 `gorm:"index" json:"last_activity_at,omitempty"`
Labels []string `gorm:"-" json:"labels,omitempty"`
}
func (Contact) TableName() string { return "contacts" }
// BeforeSave syncs location/country_code from additional_attributes and upgrades contact_type.
// Reference: Chatwoot before_save :sync_contact_attributes → Contacts::SyncAttributes
func (c *Contact) BeforeSave(tx *gorm.DB) error {
c.syncLocationAndCountryCode()
c.syncContactType()
return nil
}
// syncLocationAndCountryCode copies city → Location and country → CountryCode from additional_attributes.
// Reference: Chatwoot Contacts::SyncAttributes#update_contact_location_and_country_code
func (c *Contact) syncLocationAndCountryCode() {
if c.AdditionalAttributes != nil {
attrs := make(map[string]interface{})
if err := json.Unmarshal(c.AdditionalAttributes, &attrs); err == nil {
if city, ok := attrs["city"].(string); ok && city != "" {
c.Location = city
}
if country, ok := attrs["country"].(string); ok && country != "" {
c.CountryCode = country
}
}
}
}
// syncContactType defaults blank contacts to visitor and upgrades visitor to lead if email/phone/social details are present.
// Reference: Chatwoot Contacts::SyncAttributes#set_contact_type
func (c *Contact) syncContactType() {
if strings.TrimSpace(c.ContactType) == "" {
c.ContactType = "visitor"
}
if c.ContactType != "visitor" {
return
}
if c.Email != "" || c.PhoneNumber != "" {
c.ContactType = "lead"
return
}
// Check social details in additional_attributes (social_* keys)
if c.AdditionalAttributes != nil {
attrs := make(map[string]interface{})
if err := json.Unmarshal(c.AdditionalAttributes, &attrs); err == nil {
for key, val := range attrs {
if strings.HasPrefix(key, "social_") {
if s, ok := val.(string); ok && s != "" {
c.ContactType = "lead"
return
}
}
}
}
}
}