feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -22,6 +23,9 @@ func (r *Repository) UpdateRemark(ctx context.Context, principal Principal, conv
|
||||
return ErrPermissionDenied
|
||||
}
|
||||
db := r.db.WithContext(ctx)
|
||||
if err := ensureAdminConversationAccess(ctx, db, principal.ID, conversationID, false); err != nil {
|
||||
return err
|
||||
}
|
||||
var conversation model.ChatConversation
|
||||
if err := db.Select("id").First(&conversation, conversationID).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -41,7 +45,11 @@ func (r *Repository) UpdateRemark(ctx context.Context, principal Principal, conv
|
||||
}
|
||||
func (r *Repository) ListQuickReplies(ctx context.Context, adminID uint64) ([]QuickReplyDTO, error) {
|
||||
var replies []model.ChatQuickReply
|
||||
err := r.db.WithContext(ctx).Where("admin_user_id = ? OR admin_user_id = 0", adminID).
|
||||
query := r.db.WithContext(ctx).Where("admin_user_id = ? OR admin_user_id = 0", adminID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
err := query.
|
||||
Order("admin_user_id DESC, sort_order ASC, id ASC").
|
||||
Find(&replies).Error
|
||||
if err != nil {
|
||||
@@ -66,6 +74,7 @@ func (r *Repository) CreateQuickReply(ctx context.Context, adminID uint64, req C
|
||||
ownerID = 0
|
||||
}
|
||||
reply := model.ChatQuickReply{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
AdminUserID: ownerID,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
@@ -85,6 +94,9 @@ func (r *Repository) CreateQuickReply(ctx context.Context, adminID uint64, req C
|
||||
}
|
||||
func (r *Repository) UpdateQuickReply(ctx context.Context, adminID uint64, replyID uint64, req UpdateQuickReplyRequest) error {
|
||||
query := r.db.WithContext(ctx).Model(&model.ChatQuickReply{}).Where("id = ? AND (admin_user_id = ? OR admin_user_id = 0)", replyID, adminID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
updates := map[string]interface{}{}
|
||||
if req.Title != "" {
|
||||
updates["title"] = req.Title
|
||||
@@ -101,32 +113,51 @@ func (r *Repository) UpdateQuickReply(ctx context.Context, adminID uint64, reply
|
||||
return query.Updates(updates).Error
|
||||
}
|
||||
func (r *Repository) DeleteQuickReply(ctx context.Context, adminID uint64, replyID uint64) error {
|
||||
return r.db.WithContext(ctx).Where("id = ? AND admin_user_id = ?", replyID, adminID).
|
||||
Delete(&model.ChatQuickReply{}).Error
|
||||
query := r.db.WithContext(ctx).Where("id = ? AND admin_user_id = ?", replyID, adminID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.Delete(&model.ChatQuickReply{}).Error
|
||||
}
|
||||
func (r *Repository) GetAutoWelcomeMessage(ctx context.Context) string {
|
||||
var cfg model.SystemConfig
|
||||
if err := r.db.WithContext(ctx).Where("`key` = ?", autoWelcomeConfigKey).First(&cfg).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("`key` = ?", autoWelcomeConfigKey)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&cfg).Error; err != nil {
|
||||
return defaultAutoWelcomeMessage
|
||||
}
|
||||
return cfg.Value
|
||||
}
|
||||
func (r *Repository) UpdateAutoWelcomeMessage(ctx context.Context, message string) error {
|
||||
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
||||
Where("`key` = ?", autoWelcomeConfigKey).
|
||||
query := r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
||||
Where("`key` = ?", autoWelcomeConfigKey)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.
|
||||
Update("value", message).Error
|
||||
}
|
||||
|
||||
func (r *Repository) GetListingGroupWelcomeMessage(ctx context.Context) string {
|
||||
var cfg model.SystemConfig
|
||||
if err := r.db.WithContext(ctx).Where("`key` = ?", listingGroupWelcomeConfigKey).First(&cfg).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("`key` = ?", listingGroupWelcomeConfigKey)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&cfg).Error; err != nil {
|
||||
return defaultListingGroupWelcome
|
||||
}
|
||||
return cfg.Value
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateListingGroupWelcomeMessage(ctx context.Context, message string) error {
|
||||
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
||||
Where("`key` = ?", listingGroupWelcomeConfigKey).
|
||||
query := r.db.WithContext(ctx).Model(&model.SystemConfig{}).
|
||||
Where("`key` = ?", listingGroupWelcomeConfigKey)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.
|
||||
Update("value", message).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user