feat(auth,prompt-rule): add password change with session revocation and scope prompt rules by brand
Add self-service password change that re-hashes the credential and bumps a per-user session version so all existing access/refresh tokens are rejected. Session version is tracked in Redis with an in-memory fallback and enforced in middleware and refresh. Scope prompt rules and rule groups to a brand: new brand_id column (migrated to a "未归属" fallback brand for existing rows), brand-filtered queries/indexes, and brand-aware admin-web tabs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -107,23 +107,31 @@ type PromptRuleSimple struct {
|
||||
|
||||
func (s *PromptRuleService) ListGroups(ctx context.Context) ([]PromptRuleGroupResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleGroupsCacheKey(actor.TenantID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleGroupResponse, error) {
|
||||
return s.loadPromptRuleGroups(loadCtx, actor.TenantID)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleGroupsCacheKey(actor.TenantID, brandID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleGroupResponse, error) {
|
||||
return s.loadPromptRuleGroups(loadCtx, actor.TenantID, brandID)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) CreateGroup(ctx context.Context, req PromptRuleGroupRequest) (*PromptRuleGroupResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sortOrder := 0
|
||||
if req.SortOrder != nil {
|
||||
sortOrder = *req.SortOrder
|
||||
}
|
||||
var id int64
|
||||
var ca interface{}
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
INSERT INTO prompt_rule_groups (tenant_id, name, sort_order)
|
||||
VALUES ($1, $2, $3) RETURNING id, created_at
|
||||
`, actor.TenantID, req.Name, sortOrder).Scan(&id, &ca)
|
||||
err = s.pool.QueryRow(ctx, `
|
||||
INSERT INTO prompt_rule_groups (tenant_id, brand_id, name, sort_order)
|
||||
VALUES ($1, $2, $3, $4) RETURNING id, created_at
|
||||
`, actor.TenantID, brandID, req.Name, sortOrder).Scan(&id, &ca)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "create_failed", "failed to create group")
|
||||
}
|
||||
@@ -144,40 +152,48 @@ func (s *PromptRuleService) CreateGroup(ctx context.Context, req PromptRuleGroup
|
||||
Result: &result,
|
||||
})
|
||||
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, nil)
|
||||
return &PromptRuleGroupResponse{ID: id, Name: req.Name, SortOrder: sortOrder, CreatedAt: fmt.Sprintf("%v", ca)}, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) UpdateGroup(ctx context.Context, id int64, req PromptRuleGroupRequest) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sortOrder := 0
|
||||
if req.SortOrder != nil {
|
||||
sortOrder = *req.SortOrder
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE prompt_rule_groups SET name = $1, sort_order = $2, updated_at = NOW()
|
||||
WHERE id = $3 AND tenant_id = $4 AND deleted_at IS NULL
|
||||
`, req.Name, sortOrder, id, actor.TenantID)
|
||||
WHERE id = $3 AND tenant_id = $4 AND brand_id = $5 AND deleted_at IS NULL
|
||||
`, req.Name, sortOrder, id, actor.TenantID, brandID)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40430, "group_not_found", "prompt rule group not found")
|
||||
}
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) DeleteGroup(ctx context.Context, id int64) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Move rules in this group to ungrouped
|
||||
_, _ = s.pool.Exec(ctx, `
|
||||
UPDATE prompt_rules SET group_id = NULL, updated_at = NOW()
|
||||
WHERE group_id = $1 AND tenant_id = $2 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID)
|
||||
WHERE group_id = $1 AND tenant_id = $2 AND brand_id = $3 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID, brandID)
|
||||
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE prompt_rule_groups SET deleted_at = NOW(), updated_at = NOW()
|
||||
WHERE id = $1 AND tenant_id = $2 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID)
|
||||
WHERE id = $1 AND tenant_id = $2 AND brand_id = $3 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID, brandID)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40430, "group_not_found", "prompt rule group not found")
|
||||
}
|
||||
@@ -197,7 +213,7 @@ func (s *PromptRuleService) DeleteGroup(ctx context.Context, id int64) error {
|
||||
AfterJSON: afterJSON,
|
||||
Result: &result,
|
||||
})
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, nil)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -205,15 +221,23 @@ func (s *PromptRuleService) DeleteGroup(ctx context.Context, id int64) error {
|
||||
|
||||
func (s *PromptRuleService) List(ctx context.Context, params PromptRuleListParams) (*PromptRuleListResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleListCacheKey(actor.TenantID, params), defaultCacheTTL(), func(loadCtx context.Context) (*PromptRuleListResponse, error) {
|
||||
return s.loadPromptRules(loadCtx, actor.TenantID, params)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleListCacheKey(actor.TenantID, brandID, params), defaultCacheTTL(), func(loadCtx context.Context) (*PromptRuleListResponse, error) {
|
||||
return s.loadPromptRules(loadCtx, actor.TenantID, brandID, params)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) Detail(ctx context.Context, id int64) (*PromptRuleResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
record, found, err := sharedcache.LoadJSONWithEmpty(ctx, s.cache, &s.cacheGroup, promptRuleDetailCacheKey(actor.TenantID, id), defaultCacheTTL(), defaultCacheEmptyTTL(), func(loadCtx context.Context) (*PromptRuleResponse, bool, error) {
|
||||
return s.loadPromptRuleDetail(loadCtx, actor.TenantID, id)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
record, found, err := sharedcache.LoadJSONWithEmpty(ctx, s.cache, &s.cacheGroup, promptRuleDetailCacheKey(actor.TenantID, brandID, id), defaultCacheTTL(), defaultCacheEmptyTTL(), func(loadCtx context.Context) (*PromptRuleResponse, bool, error) {
|
||||
return s.loadPromptRuleDetail(loadCtx, actor.TenantID, brandID, id)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -226,10 +250,17 @@ func (s *PromptRuleService) Detail(ctx context.Context, id int64) (*PromptRuleRe
|
||||
|
||||
func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (*PromptRuleResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.validatePromptRuleGroup(ctx, actor.TenantID, brandID, req.GroupID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -240,9 +271,9 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
|
||||
var id int64
|
||||
var ca interface{}
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO prompt_rules (tenant_id, group_id, name, prompt_content, scene, default_tone, default_word_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id, created_at
|
||||
`, actor.TenantID, req.GroupID, req.Name, req.PromptContent, req.Scene, req.DefaultTone, req.DefaultWordCount).Scan(&id, &ca)
|
||||
INSERT INTO prompt_rules (tenant_id, brand_id, group_id, name, prompt_content, scene, default_tone, default_word_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, created_at
|
||||
`, actor.TenantID, brandID, req.GroupID, req.Name, req.PromptContent, req.Scene, req.DefaultTone, req.DefaultWordCount).Scan(&id, &ca)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "create_failed", "failed to create prompt rule")
|
||||
}
|
||||
@@ -270,7 +301,7 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
|
||||
Result: &result,
|
||||
})
|
||||
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, &id)
|
||||
return &PromptRuleResponse{
|
||||
ID: id,
|
||||
GroupID: req.GroupID,
|
||||
@@ -290,10 +321,17 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
|
||||
|
||||
func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRuleRequest) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.validatePromptRuleGroup(ctx, actor.TenantID, brandID, req.GroupID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -304,8 +342,8 @@ func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRule
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE prompt_rules SET name = $1, group_id = $2, prompt_content = $3,
|
||||
scene = $4, default_tone = $5, default_word_count = $6, updated_at = NOW()
|
||||
WHERE id = $7 AND tenant_id = $8 AND deleted_at IS NULL
|
||||
`, req.Name, req.GroupID, req.PromptContent, req.Scene, req.DefaultTone, req.DefaultWordCount, id, actor.TenantID)
|
||||
WHERE id = $7 AND tenant_id = $8 AND brand_id = $9 AND deleted_at IS NULL
|
||||
`, req.Name, req.GroupID, req.PromptContent, req.Scene, req.DefaultTone, req.DefaultWordCount, id, actor.TenantID, brandID)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
|
||||
}
|
||||
@@ -315,16 +353,20 @@ func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRule
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return response.ErrInternal(50010, "update_failed", "failed to commit prompt rule")
|
||||
}
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, &id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) Delete(ctx context.Context, id int64) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE prompt_rules SET deleted_at = NOW(), updated_at = NOW()
|
||||
WHERE id = $1 AND tenant_id = $2 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID)
|
||||
WHERE id = $1 AND tenant_id = $2 AND brand_id = $3 AND deleted_at IS NULL
|
||||
`, id, actor.TenantID, brandID)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
|
||||
}
|
||||
@@ -344,36 +386,45 @@ func (s *PromptRuleService) Delete(ctx context.Context, id int64) error {
|
||||
AfterJSON: afterJSON,
|
||||
Result: &result,
|
||||
})
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, &id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) ToggleStatus(ctx context.Context, id int64, req PromptRuleStatusRequest) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE prompt_rules SET status = $1, updated_at = NOW()
|
||||
WHERE id = $2 AND tenant_id = $3 AND deleted_at IS NULL
|
||||
`, req.Status, id, actor.TenantID)
|
||||
WHERE id = $2 AND tenant_id = $3 AND brand_id = $4 AND deleted_at IS NULL
|
||||
`, req.Status, id, actor.TenantID, brandID)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
|
||||
}
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, &id)
|
||||
invalidatePromptRuleCaches(ctx, s.cache, actor.TenantID, brandID, &id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) ListSimple(ctx context.Context) ([]PromptRuleSimple, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleSimpleCacheKey(actor.TenantID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleSimple, error) {
|
||||
return s.loadPromptRuleSimple(loadCtx, actor.TenantID)
|
||||
brandID, err := requireCurrentBrandID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sharedcache.LoadJSON(ctx, s.cache, &s.cacheGroup, promptRuleSimpleCacheKey(actor.TenantID, brandID), defaultCacheTTL(), func(loadCtx context.Context) ([]PromptRuleSimple, error) {
|
||||
return s.loadPromptRuleSimple(loadCtx, actor.TenantID, brandID)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) loadPromptRuleGroups(ctx context.Context, tenantID int64) ([]PromptRuleGroupResponse, error) {
|
||||
func (s *PromptRuleService) loadPromptRuleGroups(ctx context.Context, tenantID, brandID int64) ([]PromptRuleGroupResponse, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT id, name, sort_order, created_at
|
||||
FROM prompt_rule_groups WHERE tenant_id = $1 AND deleted_at IS NULL
|
||||
FROM prompt_rule_groups
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL
|
||||
ORDER BY sort_order, created_at
|
||||
`, tenantID)
|
||||
`, tenantID, brandID)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "query_failed", "failed to list groups")
|
||||
}
|
||||
@@ -392,7 +443,7 @@ func (s *PromptRuleService) loadPromptRuleGroups(ctx context.Context, tenantID i
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64, params PromptRuleListParams) (*PromptRuleListResponse, error) {
|
||||
func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID, brandID int64, params PromptRuleListParams) (*PromptRuleListResponse, error) {
|
||||
if params.Page < 1 {
|
||||
params.Page = 1
|
||||
}
|
||||
@@ -407,8 +458,8 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64,
|
||||
if params.Ungrouped {
|
||||
if err := s.pool.QueryRow(ctx, `
|
||||
SELECT COUNT(*) FROM prompt_rules
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL AND group_id IS NULL
|
||||
`, tenantID).Scan(&total); err != nil {
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL AND group_id IS NULL
|
||||
`, tenantID, brandID).Scan(&total); err != nil {
|
||||
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
|
||||
}
|
||||
|
||||
@@ -418,12 +469,12 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64,
|
||||
pr.created_at, pr.updated_at,
|
||||
COUNT(a.id)::INT AS article_count
|
||||
FROM prompt_rules pr
|
||||
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
|
||||
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL AND pr.group_id IS NULL
|
||||
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.brand_id = pr.brand_id AND a.deleted_at IS NULL
|
||||
WHERE pr.tenant_id = $1 AND pr.brand_id = $2 AND pr.deleted_at IS NULL AND pr.group_id IS NULL
|
||||
GROUP BY pr.id
|
||||
ORDER BY pr.created_at DESC
|
||||
LIMIT $2 OFFSET $3
|
||||
`, tenantID, params.PageSize, offset)
|
||||
LIMIT $3 OFFSET $4
|
||||
`, tenantID, brandID, params.PageSize, offset)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
|
||||
}
|
||||
@@ -431,11 +482,11 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64,
|
||||
} else {
|
||||
if err := s.pool.QueryRow(ctx, `
|
||||
SELECT COUNT(*) FROM prompt_rules
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL
|
||||
AND ($2::bigint IS NULL OR group_id = $2)
|
||||
AND ($3::text IS NULL OR status = $3)
|
||||
AND ($4::text IS NULL OR name ILIKE '%' || $4 || '%')
|
||||
`, tenantID, params.GroupID, params.Status, params.Keyword).Scan(&total); err != nil {
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL
|
||||
AND ($3::bigint IS NULL OR group_id = $3)
|
||||
AND ($4::text IS NULL OR status = $4)
|
||||
AND ($5::text IS NULL OR name ILIKE '%' || $5 || '%')
|
||||
`, tenantID, brandID, params.GroupID, params.Status, params.Keyword).Scan(&total); err != nil {
|
||||
return nil, response.ErrInternal(50010, "count_failed", "failed to count rules")
|
||||
}
|
||||
|
||||
@@ -445,15 +496,15 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64,
|
||||
pr.created_at, pr.updated_at,
|
||||
COUNT(a.id)::INT AS article_count
|
||||
FROM prompt_rules pr
|
||||
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
|
||||
WHERE pr.tenant_id = $1 AND pr.deleted_at IS NULL
|
||||
AND ($2::bigint IS NULL OR pr.group_id = $2)
|
||||
AND ($3::text IS NULL OR pr.status = $3)
|
||||
AND ($4::text IS NULL OR pr.name ILIKE '%' || $4 || '%')
|
||||
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.brand_id = pr.brand_id AND a.deleted_at IS NULL
|
||||
WHERE pr.tenant_id = $1 AND pr.brand_id = $2 AND pr.deleted_at IS NULL
|
||||
AND ($3::bigint IS NULL OR pr.group_id = $3)
|
||||
AND ($4::text IS NULL OR pr.status = $4)
|
||||
AND ($5::text IS NULL OR pr.name ILIKE '%' || $5 || '%')
|
||||
GROUP BY pr.id
|
||||
ORDER BY pr.created_at DESC
|
||||
LIMIT $5 OFFSET $6
|
||||
`, tenantID, params.GroupID, params.Status, params.Keyword, params.PageSize, offset)
|
||||
LIMIT $6 OFFSET $7
|
||||
`, tenantID, brandID, params.GroupID, params.Status, params.Keyword, params.PageSize, offset)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
|
||||
}
|
||||
@@ -496,7 +547,7 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID int64,
|
||||
return &PromptRuleListResponse{Items: items, Total: total}, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID, ruleID int64) (*PromptRuleResponse, bool, error) {
|
||||
func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID, brandID, ruleID int64) (*PromptRuleResponse, bool, error) {
|
||||
var item PromptRuleResponse
|
||||
var createdAt interface{}
|
||||
var updatedAt interface{}
|
||||
@@ -505,10 +556,10 @@ func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID,
|
||||
SELECT pr.id, pr.group_id, pr.name, pr.prompt_content,
|
||||
pr.scene, pr.default_tone, pr.default_word_count, pr.status,
|
||||
pr.created_at, pr.updated_at,
|
||||
(SELECT COUNT(*) FROM articles WHERE prompt_rule_id = pr.id AND deleted_at IS NULL)::INT
|
||||
(SELECT COUNT(*) FROM articles WHERE prompt_rule_id = pr.id AND brand_id = pr.brand_id AND deleted_at IS NULL)::INT
|
||||
FROM prompt_rules pr
|
||||
WHERE pr.id = $1 AND pr.tenant_id = $2 AND pr.deleted_at IS NULL
|
||||
`, ruleID, tenantID).Scan(&item.ID, &item.GroupID, &item.Name, &item.PromptContent,
|
||||
WHERE pr.id = $1 AND pr.tenant_id = $2 AND pr.brand_id = $3 AND pr.deleted_at IS NULL
|
||||
`, ruleID, tenantID, brandID).Scan(&item.ID, &item.GroupID, &item.Name, &item.PromptContent,
|
||||
&item.Scene, &item.DefaultTone, &item.DefaultWordCount, &item.Status,
|
||||
&createdAt, &updatedAt, &articleCount)
|
||||
if err != nil {
|
||||
@@ -530,12 +581,12 @@ func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID,
|
||||
return &item, true, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) loadPromptRuleSimple(ctx context.Context, tenantID int64) ([]PromptRuleSimple, error) {
|
||||
func (s *PromptRuleService) loadPromptRuleSimple(ctx context.Context, tenantID, brandID int64) ([]PromptRuleSimple, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT id, name FROM prompt_rules
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL AND status = 'enabled'
|
||||
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL AND status = 'enabled'
|
||||
ORDER BY name
|
||||
`, tenantID)
|
||||
`, tenantID, brandID)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "query_failed", "failed to list rules")
|
||||
}
|
||||
@@ -552,6 +603,30 @@ func (s *PromptRuleService) loadPromptRuleSimple(ctx context.Context, tenantID i
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) validatePromptRuleGroup(ctx context.Context, tenantID, brandID int64, groupID *int64) error {
|
||||
if groupID == nil || *groupID <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var exists bool
|
||||
if err := s.pool.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM prompt_rule_groups
|
||||
WHERE id = $1
|
||||
AND tenant_id = $2
|
||||
AND brand_id = $3
|
||||
AND deleted_at IS NULL
|
||||
)
|
||||
`, *groupID, tenantID, brandID).Scan(&exists); err != nil {
|
||||
return response.ErrInternal(50010, "group_query_failed", "failed to validate prompt rule group")
|
||||
}
|
||||
if !exists {
|
||||
return response.ErrNotFound(40430, "group_not_found", "prompt rule group not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) validatePromptRuleKnowledgeGroups(
|
||||
ctx context.Context,
|
||||
tenantID int64,
|
||||
|
||||
Reference in New Issue
Block a user