feat(agents): align delete scope

This commit is contained in:
2026-06-06 02:36:18 +08:00
parent 8b30cc78a0
commit ec1bb257dc
3 changed files with 34 additions and 6 deletions
+7 -2
View File
@@ -245,10 +245,15 @@ func (r *AgentRepo) UpdateAgent(ctx context.Context, userID, accountID uint, nam
// Reference: Chatwoot agents_controller.rb#destroy → current_account_user.destroy!
// If the user has no other account memberships, deletes the user record too.
func (r *AgentRepo) DeleteAgent(ctx context.Context, userID, accountID uint) error {
// Delete the AccountUser
var accountUser model.AccountUser
if err := r.db.WithContext(ctx).
Where("account_id = ? AND user_id = ?", accountID, userID).
Delete(&model.AccountUser{}).Error; err != nil {
First(&accountUser).Error; err != nil {
return err
}
// Delete the AccountUser after the scoped lookup succeeds.
if err := r.db.WithContext(ctx).Delete(&accountUser).Error; err != nil {
return err
}