feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// conversationTenantAccessCondition 让跨站订单的买方租户和货源租户都能访问同一会话。
|
||||
// 发布群还可能通过后续订单被买方租客加入,因此额外检查 listing_id 关联订单。
|
||||
func conversationTenantAccessCondition(alias string) (string, []any) {
|
||||
condition := `(
|
||||
` + alias + `.tenant_id = ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro
|
||||
WHERE ro.id = ` + alias + `.order_id
|
||||
AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl
|
||||
WHERE rl.id = ` + alias + `.listing_id
|
||||
AND rl.source_tenant_id = ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders rlo
|
||||
WHERE rlo.listing_id = ` + alias + `.listing_id
|
||||
AND rlo.buyer_tenant_id = ?
|
||||
)
|
||||
)`
|
||||
return condition, []any{0, 0, 0, 0, 0}
|
||||
}
|
||||
|
||||
func tenantConversationScope(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return db
|
||||
}
|
||||
condition, args := conversationTenantAccessCondition("c")
|
||||
for index := range args {
|
||||
args[index] = tenantID
|
||||
}
|
||||
return db.Where(condition, args...)
|
||||
}
|
||||
|
||||
func adminHasPlatformScope(db *gorm.DB, adminID uint64) (bool, error) {
|
||||
if db == nil || adminID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
var count int64
|
||||
err := db.Session(&gorm.Session{NewDB: true}).Table("admin_user_roles AS aur").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("aur.admin_user_id = ? AND aur.tenant_id = 0 AND r.code = ?", adminID, "super_admin").
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func adminConversationScope(ctx context.Context, db *gorm.DB, adminID uint64) (*gorm.DB, error) {
|
||||
platform, err := adminHasPlatformScope(db, adminID)
|
||||
if err != nil || platform {
|
||||
return db, err
|
||||
}
|
||||
return tenantConversationScope(ctx, db), nil
|
||||
}
|
||||
|
||||
func ensureAdminConversationAccess(ctx context.Context, tx *gorm.DB, adminID, conversationID uint64, lock bool) error {
|
||||
query, err := adminConversationScope(ctx, tx, adminID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query = query.Table("chat_conversations AS c").Select("c.id").Where("c.id = ?", conversationID)
|
||||
if lock {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
var conversation model.ChatConversation
|
||||
if err := query.First(&conversation).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConversationNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func conversationTenantIDs(tx *gorm.DB, conversationID uint64) ([]uint64, error) {
|
||||
var conversation model.ChatConversation
|
||||
if err := tx.Select("id", "tenant_id", "order_id", "listing_id").First(&conversation, conversationID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := []uint64{conversation.TenantID}
|
||||
if conversation.OrderID != nil {
|
||||
var order model.RentalOrder
|
||||
if err := tx.Select("buyer_tenant_id", "source_tenant_id").First(&order, *conversation.OrderID).Error; err == nil {
|
||||
ids = append(ids, order.BuyerTenantID, order.SourceTenantID)
|
||||
}
|
||||
}
|
||||
if conversation.ListingID != nil {
|
||||
var listing model.RentalListing
|
||||
if err := tx.Select("source_tenant_id").First(&listing, *conversation.ListingID).Error; err == nil {
|
||||
ids = append(ids, listing.SourceTenantID)
|
||||
}
|
||||
}
|
||||
return uniqueIDs(ids), nil
|
||||
}
|
||||
|
||||
func tenantScopedEvent(tx *gorm.DB, event *chathub.ChatEvent) *chathub.ChatEvent {
|
||||
if event == nil || tx == nil || event.ConversationID == 0 {
|
||||
return event
|
||||
}
|
||||
ids, err := conversationTenantIDs(tx, event.ConversationID)
|
||||
if err == nil {
|
||||
event.TenantIDs = ids
|
||||
}
|
||||
return event
|
||||
}
|
||||
Reference in New Issue
Block a user