feat: scope articles by brand

This commit is contained in:
2026-05-20 15:37:25 +08:00
parent 5fb9d0b0dd
commit dd082e2ed1
72 changed files with 3213 additions and 432 deletions
+26 -8
View File
@@ -21,6 +21,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"go.uber.org/zap"
"github.com/geo-platform/tenant-api/internal/shared/auth"
sharedcompliance "github.com/geo-platform/tenant-api/internal/shared/compliance"
"github.com/geo-platform/tenant-api/internal/shared/config"
sharedllm "github.com/geo-platform/tenant-api/internal/shared/llm"
@@ -406,6 +407,7 @@ func (s *Service) GateForPublish(ctx context.Context, req GateForPublishRequest)
}
func (s *Service) ResolvePublishableVersion(ctx context.Context, tenantID, articleID int64, requested *int64) (int64, error) {
brandID := auth.CurrentBrandID(ctx)
if requested != nil && *requested > 0 {
var id int64
err := s.pool.QueryRow(ctx, `
@@ -415,8 +417,9 @@ func (s *Service) ResolvePublishableVersion(ctx context.Context, tenantID, artic
WHERE av.id = $1
AND av.article_id = $2
AND a.tenant_id = $3
AND ($4::BIGINT IS NULL OR a.brand_id = $4)
AND a.deleted_at IS NULL
`, *requested, articleID, tenantID).Scan(&id)
`, *requested, articleID, tenantID, nullablePositiveInt64(brandID)).Scan(&id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, response.ErrBadRequest(41005, "invalid_article_version", "article_version_id does not belong to this article")
@@ -429,8 +432,11 @@ func (s *Service) ResolvePublishableVersion(ctx context.Context, tenantID, artic
err := s.pool.QueryRow(ctx, `
SELECT current_version_id
FROM articles
WHERE id = $1 AND tenant_id = $2 AND deleted_at IS NULL
`, articleID, tenantID).Scan(&versionValue)
WHERE id = $1
AND tenant_id = $2
AND ($3::BIGINT IS NULL OR brand_id = $3)
AND deleted_at IS NULL
`, articleID, tenantID, nullablePositiveInt64(brandID)).Scan(&versionValue)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, response.ErrNotFound(40411, "article_not_found", "article not found")
@@ -811,14 +817,19 @@ func (s *Service) validateAckForPublish(ctx context.Context, req GateForPublishR
}
func (s *Service) LatestCheck(ctx context.Context, tenantID, articleID int64) (*sharedcompliance.CheckResult, error) {
brandID := auth.CurrentBrandID(ctx)
var recordID int64
err := s.pool.QueryRow(ctx, `
SELECT id
FROM compliance_check_records
WHERE tenant_id = $1 AND article_id = $2
ORDER BY checked_at DESC, id DESC
SELECT cr.id
FROM compliance_check_records cr
JOIN articles a ON a.id = cr.article_id AND a.tenant_id = cr.tenant_id
WHERE cr.tenant_id = $1
AND cr.article_id = $2
AND ($3::BIGINT IS NULL OR a.brand_id = $3)
AND a.deleted_at IS NULL
ORDER BY cr.checked_at DESC, cr.id DESC
LIMIT 1
`, tenantID, articleID).Scan(&recordID)
`, tenantID, articleID, nullablePositiveInt64(brandID)).Scan(&recordID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, response.ErrNotFound(40461, "compliance_check_not_found", "no compliance check record found")
@@ -2546,6 +2557,13 @@ func nullableString(value string) *string {
return &value
}
func nullablePositiveInt64(value int64) *int64 {
if value <= 0 {
return nil
}
return &value
}
func int64PtrAny(value *int64) any {
if value == nil {
return nil