e90d6a185f
GetRecentArticlesByBrand now folds the latest publish record per target into an aggregated publish_status (publishing / partial_success / success / failed), falling back to the stored status when no records exist. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
279 lines
9.6 KiB
Go
279 lines
9.6 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: workspace.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const countArticlesByTenantAndBrand = `-- name: CountArticlesByTenantAndBrand :one
|
|
SELECT COUNT(*) FROM articles
|
|
WHERE tenant_id = $1 AND brand_id = $2 AND deleted_at IS NULL
|
|
`
|
|
|
|
type CountArticlesByTenantAndBrandParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
BrandID int64 `json:"brand_id"`
|
|
}
|
|
|
|
func (q *Queries) CountArticlesByTenantAndBrand(ctx context.Context, arg CountArticlesByTenantAndBrandParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countArticlesByTenantAndBrand, arg.TenantID, arg.BrandID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countBrandsByTenant = `-- name: CountBrandsByTenant :one
|
|
SELECT COUNT(*) FROM brands
|
|
WHERE tenant_id = $1 AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countBrandsByTenant, tenantID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countPublishedArticlesByTenantAndBrand = `-- name: CountPublishedArticlesByTenantAndBrand :one
|
|
SELECT COUNT(*) FROM articles
|
|
WHERE tenant_id = $1 AND brand_id = $2 AND publish_status = 'success' AND deleted_at IS NULL
|
|
`
|
|
|
|
type CountPublishedArticlesByTenantAndBrandParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
BrandID int64 `json:"brand_id"`
|
|
}
|
|
|
|
func (q *Queries) CountPublishedArticlesByTenantAndBrand(ctx context.Context, arg CountPublishedArticlesByTenantAndBrandParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countPublishedArticlesByTenantAndBrand, arg.TenantID, arg.BrandID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const getActivePlanForTenant = `-- name: GetActivePlanForTenant :one
|
|
SELECT p.plan_code, p.name AS plan_name, p.quota_policy_json,
|
|
s.start_at, s.end_at
|
|
FROM tenant_plan_subscriptions s
|
|
JOIN plans p ON p.id = s.plan_id
|
|
WHERE s.tenant_id = $1 AND s.status = 'active' AND s.deleted_at IS NULL
|
|
AND s.end_at > NOW()
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetActivePlanForTenantRow struct {
|
|
PlanCode string `json:"plan_code"`
|
|
PlanName string `json:"plan_name"`
|
|
QuotaPolicyJson []byte `json:"quota_policy_json"`
|
|
StartAt pgtype.Timestamptz `json:"start_at"`
|
|
EndAt pgtype.Timestamptz `json:"end_at"`
|
|
}
|
|
|
|
func (q *Queries) GetActivePlanForTenant(ctx context.Context, tenantID int64) (GetActivePlanForTenantRow, error) {
|
|
row := q.db.QueryRow(ctx, getActivePlanForTenant, tenantID)
|
|
var i GetActivePlanForTenantRow
|
|
err := row.Scan(
|
|
&i.PlanCode,
|
|
&i.PlanName,
|
|
&i.QuotaPolicyJson,
|
|
&i.StartAt,
|
|
&i.EndAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getQuotaSummary = `-- name: GetQuotaSummary :one
|
|
SELECT COALESCE(
|
|
(SELECT balance_after FROM tenant_quota_ledgers
|
|
WHERE tenant_id = $1 AND quota_type = 'article_generation'
|
|
ORDER BY created_at DESC LIMIT 1), 0
|
|
)::INT AS balance
|
|
`
|
|
|
|
func (q *Queries) GetQuotaSummary(ctx context.Context, tenantID int64) (int32, error) {
|
|
row := q.db.QueryRow(ctx, getQuotaSummary, tenantID)
|
|
var balance int32
|
|
err := row.Scan(&balance)
|
|
return balance, err
|
|
}
|
|
|
|
const getRecentArticlesByBrand = `-- name: GetRecentArticlesByBrand :many
|
|
SELECT a.id, a.brand_id, a.generate_status,
|
|
CASE
|
|
WHEN COALESCE(publish_status_summary.record_count, 0) = 0 THEN a.publish_status
|
|
WHEN COALESCE(publish_status_summary.has_publishing, false) THEN 'publishing'
|
|
WHEN COALESCE(publish_status_summary.has_success, false)
|
|
AND COALESCE(publish_status_summary.has_failed, false) THEN 'partial_success'
|
|
WHEN COALESCE(publish_status_summary.has_success, false) THEN 'success'
|
|
ELSE 'failed'
|
|
END AS publish_status,
|
|
a.source_type, a.created_at,
|
|
av.title, av.markdown_content, av.word_count, av.source_label,
|
|
t.template_name,
|
|
COALESCE(
|
|
NULLIF(gt.input_params_json ->> 'generation_mode', ''),
|
|
CASE WHEN a.source_type = 'custom_generation' THEN 'instant'::TEXT ELSE NULL::TEXT END
|
|
) AS generation_mode
|
|
FROM articles a
|
|
LEFT JOIN article_versions av ON av.id = a.current_version_id
|
|
LEFT JOIN article_templates t ON t.id = a.template_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT COUNT(*) AS record_count,
|
|
BOOL_OR(latest.status_bucket = 'publishing') AS has_publishing,
|
|
BOOL_OR(latest.status_bucket = 'success') AS has_success,
|
|
BOOL_OR(latest.status_bucket = 'failed') AS has_failed
|
|
FROM (
|
|
SELECT DISTINCT ON (
|
|
CASE
|
|
WHEN COALESCE(pr.target_type, 'platform_account') = 'enterprise_site'
|
|
THEN 'enterprise_site:' || COALESCE(pr.target_connection_id::TEXT, '')
|
|
ELSE 'platform_account:' || pr.platform_id || ':' || COALESCE(NULLIF(pa.platform_uid, ''), pr.platform_account_id::TEXT, '')
|
|
END
|
|
)
|
|
CASE
|
|
WHEN pr.status IN ('success', 'published', 'publish_success') THEN 'success'
|
|
WHEN pr.status IN ('publishing', 'pending', 'queued', 'running') THEN 'publishing'
|
|
ELSE 'failed'
|
|
END AS status_bucket,
|
|
CASE
|
|
WHEN COALESCE(pr.target_type, 'platform_account') = 'enterprise_site'
|
|
THEN 'enterprise_site:' || COALESCE(pr.target_connection_id::TEXT, '')
|
|
ELSE 'platform_account:' || pr.platform_id || ':' || COALESCE(NULLIF(pa.platform_uid, ''), pr.platform_account_id::TEXT, '')
|
|
END AS target_identity
|
|
FROM publish_records pr
|
|
LEFT JOIN platform_accounts pa ON pa.id = pr.platform_account_id
|
|
WHERE pr.tenant_id = a.tenant_id
|
|
AND pr.article_id = a.id
|
|
AND pr.deleted_at IS NULL
|
|
ORDER BY target_identity, pr.created_at DESC, pr.id DESC
|
|
) latest
|
|
) publish_status_summary ON true
|
|
LEFT JOIN LATERAL (
|
|
SELECT input_params_json
|
|
FROM generation_tasks
|
|
WHERE article_id = a.id
|
|
ORDER BY created_at DESC
|
|
LIMIT 1
|
|
) gt ON true
|
|
WHERE a.tenant_id = $1 AND a.brand_id = $2 AND a.deleted_at IS NULL
|
|
ORDER BY a.created_at DESC
|
|
LIMIT 10
|
|
`
|
|
|
|
type GetRecentArticlesByBrandParams struct {
|
|
TenantID int64 `json:"tenant_id"`
|
|
BrandID int64 `json:"brand_id"`
|
|
}
|
|
|
|
type GetRecentArticlesByBrandRow struct {
|
|
ID int64 `json:"id"`
|
|
BrandID int64 `json:"brand_id"`
|
|
GenerateStatus string `json:"generate_status"`
|
|
PublishStatus string `json:"publish_status"`
|
|
SourceType string `json:"source_type"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
Title pgtype.Text `json:"title"`
|
|
MarkdownContent pgtype.Text `json:"markdown_content"`
|
|
WordCount pgtype.Int4 `json:"word_count"`
|
|
SourceLabel pgtype.Text `json:"source_label"`
|
|
TemplateName pgtype.Text `json:"template_name"`
|
|
GenerationMode interface{} `json:"generation_mode"`
|
|
}
|
|
|
|
func (q *Queries) GetRecentArticlesByBrand(ctx context.Context, arg GetRecentArticlesByBrandParams) ([]GetRecentArticlesByBrandRow, error) {
|
|
rows, err := q.db.Query(ctx, getRecentArticlesByBrand, arg.TenantID, arg.BrandID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetRecentArticlesByBrandRow
|
|
for rows.Next() {
|
|
var i GetRecentArticlesByBrandRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BrandID,
|
|
&i.GenerateStatus,
|
|
&i.PublishStatus,
|
|
&i.SourceType,
|
|
&i.CreatedAt,
|
|
&i.Title,
|
|
&i.MarkdownContent,
|
|
&i.WordCount,
|
|
&i.SourceLabel,
|
|
&i.TemplateName,
|
|
&i.GenerationMode,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKolCardsForTenant = `-- name: ListKolCardsForTenant :many
|
|
SELECT sp.id AS subscription_prompt_id, sp.granted_at,
|
|
GREATEST(p.updated_at, k.updated_at)::timestamptz AS updated_at,
|
|
p.name AS prompt_name, p.platform_hint,
|
|
k.name AS package_name, k.cover_url AS package_cover,
|
|
pf.display_name AS kol_display_name
|
|
FROM kol_subscription_prompts sp
|
|
JOIN kol_prompts p ON p.id = sp.prompt_id AND p.deleted_at IS NULL
|
|
JOIN kol_packages k ON k.id = sp.package_id AND k.deleted_at IS NULL
|
|
JOIN kol_profiles pf ON pf.id = k.kol_profile_id
|
|
WHERE sp.tenant_id = $1::bigint
|
|
AND sp.status = 'active'
|
|
AND p.status = 'active'
|
|
AND p.published_revision_no IS NOT NULL
|
|
ORDER BY GREATEST(p.updated_at, k.updated_at)::timestamptz DESC, sp.granted_at DESC
|
|
LIMIT 8
|
|
`
|
|
|
|
type ListKolCardsForTenantRow struct {
|
|
SubscriptionPromptID int64 `json:"subscription_prompt_id"`
|
|
GrantedAt pgtype.Timestamptz `json:"granted_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
PromptName string `json:"prompt_name"`
|
|
PlatformHint pgtype.Text `json:"platform_hint"`
|
|
PackageName string `json:"package_name"`
|
|
PackageCover pgtype.Text `json:"package_cover"`
|
|
KolDisplayName string `json:"kol_display_name"`
|
|
}
|
|
|
|
func (q *Queries) ListKolCardsForTenant(ctx context.Context, tenantID int64) ([]ListKolCardsForTenantRow, error) {
|
|
rows, err := q.db.Query(ctx, listKolCardsForTenant, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListKolCardsForTenantRow
|
|
for rows.Next() {
|
|
var i ListKolCardsForTenantRow
|
|
if err := rows.Scan(
|
|
&i.SubscriptionPromptID,
|
|
&i.GrantedAt,
|
|
&i.UpdatedAt,
|
|
&i.PromptName,
|
|
&i.PlatformHint,
|
|
&i.PackageName,
|
|
&i.PackageCover,
|
|
&i.KolDisplayName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|