feat(tenant): dedup article publish status by latest record per target

Aggregate publish status from the latest record per publishing target
using a shared target-identity expression, so retries and multi-record
targets no longer inflate the status buckets.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-08 23:38:59 +08:00
parent 5d29703265
commit 58a9b379bc
3 changed files with 54 additions and 12 deletions
@@ -110,24 +110,18 @@ const articlePublishStatusAggregateJoin = `
BOOL_OR(latest.status_bucket = 'success') AS has_success,
BOOL_OR(latest.status_bucket = 'failed') AS has_failed
FROM (
SELECT DISTINCT ON (
COALESCE(pr.target_type, 'platform_account'),
COALESCE(pr.platform_account_id, pr.target_connection_id)
)
SELECT DISTINCT ON (` + publishRecordTargetIdentityExprSQL + `)
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
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
COALESCE(pr.target_type, 'platform_account'),
COALESCE(pr.platform_account_id, pr.target_connection_id),
pr.created_at DESC,
pr.id DESC
ORDER BY ` + publishRecordTargetIdentityExprSQL + `, pr.created_at DESC, pr.id DESC
) latest
) publish_status_summary ON true`