Scope knowledge and image libraries by brand
Frontend CI / Frontend (push) Successful in 2m59s
Backend CI / Backend (push) Successful in 14m58s

This commit is contained in:
2026-06-30 12:30:10 +08:00
parent 95ddb3057f
commit ced0c4ec0f
35 changed files with 1136 additions and 326 deletions
@@ -254,7 +254,7 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
if err != nil {
return nil, err
}
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, brandID, req.KnowledgeGroupIDs)
if err != nil {
return nil, err
}
@@ -325,7 +325,7 @@ func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRule
if err != nil {
return err
}
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, brandID, req.KnowledgeGroupIDs)
if err != nil {
return err
}
@@ -535,7 +535,7 @@ func (s *PromptRuleService) loadPromptRules(ctx context.Context, tenantID, brand
item.KnowledgeGroups = []PromptRuleKnowledgeGroup{}
items = append(items, item)
}
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, collectPromptRuleIDs(items))
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, brandID, collectPromptRuleIDs(items))
if err != nil {
return nil, err
}
@@ -572,7 +572,7 @@ func (s *PromptRuleService) loadPromptRuleDetail(ctx context.Context, tenantID,
item.UpdatedAt = fmt.Sprintf("%v", updatedAt)
item.ArticleCount = articleCount
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, []int64{ruleID})
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, tenantID, brandID, []int64{ruleID})
if err != nil {
return nil, false, err
}
@@ -630,6 +630,7 @@ func (s *PromptRuleService) validatePromptRuleGroup(ctx context.Context, tenantI
func (s *PromptRuleService) validatePromptRuleKnowledgeGroups(
ctx context.Context,
tenantID int64,
brandID int64,
groupIDs []int64,
) ([]PromptRuleKnowledgeGroup, error) {
groupIDs = normalizeInt64IDs(groupIDs)
@@ -640,9 +641,9 @@ func (s *PromptRuleService) validatePromptRuleKnowledgeGroups(
rows, err := s.pool.Query(ctx, `
SELECT id, name
FROM knowledge_groups
WHERE tenant_id = $1 AND id = ANY($2::bigint[]) AND deleted_at IS NULL
WHERE tenant_id = $1 AND brand_id = $2 AND id = ANY($3::bigint[]) AND deleted_at IS NULL
ORDER BY sort_order, created_at, id
`, tenantID, groupIDs)
`, tenantID, brandID, groupIDs)
if err != nil {
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", "failed to query knowledge groups")
}
@@ -669,6 +670,7 @@ func (s *PromptRuleService) validatePromptRuleKnowledgeGroups(
func (s *PromptRuleService) loadPromptRuleKnowledgeGroupMap(
ctx context.Context,
tenantID int64,
brandID int64,
ruleIDs []int64,
) (map[int64][]PromptRuleKnowledgeGroup, error) {
result := make(map[int64][]PromptRuleKnowledgeGroup, len(ruleIDs))
@@ -683,9 +685,10 @@ func (s *PromptRuleService) loadPromptRuleKnowledgeGroupMap(
JOIN knowledge_groups kg ON kg.id = prkg.knowledge_group_id
WHERE prkg.prompt_rule_id = ANY($1::bigint[])
AND kg.tenant_id = $2
AND kg.brand_id = $3
AND kg.deleted_at IS NULL
ORDER BY kg.sort_order, kg.created_at, kg.id
`, ruleIDs, tenantID)
`, ruleIDs, tenantID, brandID)
if err != nil {
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", "failed to query prompt rule knowledge groups")
}