feat(account-users): align inviter id

This commit is contained in:
2026-06-06 11:45:56 +08:00
parent fbc0bb833d
commit 2acd93831f
7 changed files with 43 additions and 18 deletions
@@ -180,7 +180,12 @@ func (s *AgentHandlerTestSuite) TestCreateAgent() {
assert.Equal(s.T(), float64(7), data["custom_role_id"])
assert.Contains(s.T(), data, "confirmed")
assert.NotContains(s.T(), data, "invited_by")
assert.NotContains(s.T(), data, "inviter_id")
assert.NotContains(s.T(), data, "account_user_id")
var membership model.AccountUser
s.Require().NoError(s.db.Where("account_id = ? AND user_id = ?", s.account.ID, uint(data["id"].(float64))).First(&membership).Error)
assert.Equal(s.T(), s.user.ID, membership.InvitedBy)
}
func (s *AgentHandlerTestSuite) TestCreateAgentDefaultsBlankNameFromEmail() {
+1 -1
View File
@@ -24,7 +24,7 @@ type AccountUser struct {
Availability string `gorm:"size:50;default:'offline'" json:"availability"` // online/offline/busy
AutoOffline bool `gorm:"default:true" json:"auto_offline"` // auto-offline when idle
ActiveAt *time.Time `json:"active_at,omitempty"` // last active timestamp in this account
InvitedBy uint `gorm:"default:0" json:"invited_by"` // UserID of the user who invited this member
InvitedBy uint `gorm:"column:inviter_id;default:0" json:"inviter_id"` // Chatwoot: inviter_id of the user who invited this member
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"`
+1
View File
@@ -126,6 +126,7 @@ func TestAccountUserDefaultValues(t *testing.T) {
assert.Equal(t, "agent", fetched.Role, "Role should default to 'agent'")
assert.Equal(t, "offline", fetched.Availability, "Availability should default to 'offline'")
assert.Equal(t, uint(0), fetched.InvitedBy, "InvitedBy should default to 0")
assert.True(t, db.Migrator().HasColumn(&model.AccountUser{}, "inviter_id"), "AccountUser should use Chatwoot inviter_id column")
}
// TestCustomRoleDefaultValues verifies CustomRole model default field values.
+13 -13
View File
@@ -21,11 +21,11 @@ import (
// after_save (update_presence_in_redis on availability change),
// validates user_id uniqueness scoped to account_id
type AccountUserService struct {
repo *repository.AccountUserRepo
notificationRepo *repository.NotificationSettingRepo
accountRepo *repository.AccountRepo
userRepo *repository.UserRepo
eventBus *pubsub.EventBus
repo *repository.AccountUserRepo
notificationRepo *repository.NotificationSettingRepo
accountRepo *repository.AccountRepo
userRepo *repository.UserRepo
eventBus *pubsub.EventBus
}
// NewAccountUserService creates a new AccountUser service.
@@ -78,10 +78,10 @@ func (s *AccountUserService) AddUserToAccount(ctx context.Context, accountID uin
}
au := &model.AccountUser{
AccountID: accountID,
UserID: req.UserID,
Role: req.Role,
InvitedBy: req.InviterID,
AccountID: accountID,
UserID: req.UserID,
Role: req.Role,
InvitedBy: req.InviterID,
Availability: "offline", // 1:1 Chatwoot: default availability
AutoOffline: true, // 1:1 Chatwoot: default auto_offline
}
@@ -104,7 +104,7 @@ func (s *AccountUserService) AddUserToAccount(ctx context.Context, accountID uin
"account_id": accountID,
"user_id": req.UserID,
"role": req.Role,
"invited_by": req.InviterID,
"inviter_id": req.InviterID,
})
if err := s.eventBus.Publish(pubsub.TopicAccountUserCreated, payload); err != nil {
applogger.L().Errorf("Failed to publish AccountUserCreated event: %v", err)
@@ -211,8 +211,8 @@ func (s *AccountUserService) createDefaultNotificationSetting(ctx context.Contex
// 1:1 Chatwoot: email_conversation_assignment = bit 1 → 2
// push_conversation_assignment = bit 1 → 2
setting := &model.NotificationSetting{
UserID: userID,
AccountID: accountID,
UserID: userID,
AccountID: accountID,
EmailFlags: 2, // bit 1 = email_conversation_assignment
PushFlags: 2, // bit 1 = push_conversation_assignment
}
@@ -234,4 +234,4 @@ func (s *AccountUserService) SetAutoOffline(ctx context.Context, accountID, user
// FindOnlineAgents returns all online agents for an account.
func (s *AccountUserService) FindOnlineAgents(ctx context.Context, accountID uint) ([]model.AccountUser, error) {
return s.repo.FindOnlineAgentsByAccount(ctx, accountID)
}
}