feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -38,15 +39,19 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip
|
||||
if principal.Type != "admin" {
|
||||
return ErrPermissionDenied
|
||||
}
|
||||
query, err := adminConversationScope(ctx, tx, principal.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var conversation model.ChatConversation
|
||||
if err := tx.First(&conversation, conversationID).Error; err != nil {
|
||||
if err := query.Table("chat_conversations AS c").Select("c.*").Where("c.id = ?", conversationID).First(&conversation).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConversationNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
// 验证目标客服存在、活跃且拥有客服角色,避免转接给超级管理员。
|
||||
if !adminIsSupport(tx, toAdminID) {
|
||||
if !adminIsSupport(tx, toAdminID, conversation.TenantID) {
|
||||
return fmt.Errorf("目标客服不存在、已禁用或不是客服角色")
|
||||
}
|
||||
// 检查目标客服是否已有该会话
|
||||
@@ -94,8 +99,9 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip
|
||||
return nil
|
||||
})
|
||||
if err == nil && r.hub != nil {
|
||||
r.hub.NotifyConversation(conversationID, &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||
r.hub.NotifyAllAdmins(&chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||
event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -108,11 +114,16 @@ func (r *Repository) GetAvailableSupportAdmins(ctx context.Context) ([]SupportAd
|
||||
SupportStatus string
|
||||
}
|
||||
var admins []adminRow
|
||||
err := db.Table("admin_users AS au").
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
query := db.Table("admin_users AS au").
|
||||
Select("au.id, COALESCE(NULLIF(au.nickname, ''), au.username) AS nickname, au.support_status").
|
||||
Joins("JOIN admin_user_roles AS aur ON aur.admin_user_id = au.id").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("au.status = ? AND r.code = ?", "active", defaultSupportRoleCode).
|
||||
Where("au.status = ? AND r.code = ?", "active", defaultSupportRoleCode)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("aur.tenant_id = ?", tenantID)
|
||||
}
|
||||
err := query.
|
||||
Order("CASE au.support_status WHEN 'online' THEN 0 WHEN 'busy' THEN 1 ELSE 2 END, au.id ASC").
|
||||
Scan(&admins).Error
|
||||
if err != nil {
|
||||
@@ -305,6 +316,12 @@ func (r *Repository) adminConversationBase(ctx context.Context, principal Princi
|
||||
Joins("LEFT JOIN chat_admin_conversation_states AS cas ON cas.conversation_id = c.id AND cas.admin_user_id = ?", principal.ID).
|
||||
Joins("LEFT JOIN rental_orders AS explicit_lo ON explicit_lo.id = c.order_id").
|
||||
Joins("LEFT JOIN rental_listings AS l ON l.id = COALESCE(explicit_lo.listing_id, c.listing_id)")
|
||||
scoped, err := adminConversationScope(ctx, db, principal.ID)
|
||||
if err == nil {
|
||||
db = scoped
|
||||
} else {
|
||||
db = tenantConversationScope(ctx, db)
|
||||
}
|
||||
if keyword != "" {
|
||||
upper := strings.ToUpper(keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
|
||||
Reference in New Issue
Block a user