b31d8d0096
- Added migration to harden task audit tracking by modifying audit_logs and related tables. - Introduced operator_id to several tables for better tracking of actions. - Updated article_templates with new prompt templates for various article types, enhancing content generation. - Created prompt_rules and schedule_tasks tables to manage content generation rules and scheduling. - Added foreign key constraints to articles for better data integrity.
117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/contentstats"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type WorkspaceRecentArticle struct {
|
|
ID int64
|
|
GenerateStatus string
|
|
PublishStatus string
|
|
SourceType string
|
|
CreatedAt time.Time
|
|
Title *string
|
|
WordCount int
|
|
SourceLabel *string
|
|
TemplateName *string
|
|
}
|
|
|
|
type WorkspacePlan struct {
|
|
PlanCode string
|
|
PlanName string
|
|
QuotaPolicyJSON []byte
|
|
StartAt time.Time
|
|
EndAt time.Time
|
|
}
|
|
|
|
type WorkspaceRepository interface {
|
|
CountArticlesByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
CountPublishedArticlesByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
GetRecentArticles(ctx context.Context, tenantID int64) ([]WorkspaceRecentArticle, error)
|
|
GetQuotaSummary(ctx context.Context, tenantID int64) (int, error)
|
|
GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error)
|
|
}
|
|
|
|
type workspaceRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewWorkspaceRepository(db generated.DBTX) WorkspaceRepository {
|
|
return &workspaceRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *workspaceRepository) CountArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountArticlesByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) CountPublishedArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountPublishedArticlesByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountBrandsByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) GetRecentArticles(ctx context.Context, tenantID int64) ([]WorkspaceRecentArticle, error) {
|
|
rows, err := r.q.GetRecentArticles(ctx, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
articles := make([]WorkspaceRecentArticle, 0, len(rows))
|
|
for _, row := range rows {
|
|
articles = append(articles, WorkspaceRecentArticle{
|
|
ID: row.ID,
|
|
GenerateStatus: row.GenerateStatus,
|
|
PublishStatus: row.PublishStatus,
|
|
SourceType: row.SourceType,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
Title: nullableText(row.Title),
|
|
WordCount: resolveWorkspaceWordCount(row.MarkdownContent, intFromInt4(row.WordCount)),
|
|
SourceLabel: nullableText(row.SourceLabel),
|
|
TemplateName: nullableText(row.TemplateName),
|
|
})
|
|
}
|
|
|
|
return articles, nil
|
|
}
|
|
|
|
func (r *workspaceRepository) GetQuotaSummary(ctx context.Context, tenantID int64) (int, error) {
|
|
balance, err := r.q.GetQuotaSummary(ctx, tenantID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(balance), nil
|
|
}
|
|
|
|
func (r *workspaceRepository) GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error) {
|
|
row, err := r.q.GetActivePlanForTenant(ctx, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &WorkspacePlan{
|
|
PlanCode: row.PlanCode,
|
|
PlanName: row.PlanName,
|
|
QuotaPolicyJSON: row.QuotaPolicyJson,
|
|
StartAt: timeFromTimestamp(row.StartAt),
|
|
EndAt: timeFromTimestamp(row.EndAt),
|
|
}, nil
|
|
}
|
|
|
|
func resolveWorkspaceWordCount(markdown pgtype.Text, stored int) int {
|
|
if markdown.Valid {
|
|
if counted := contentstats.CountWords(markdown.String); counted > 0 {
|
|
return counted
|
|
}
|
|
}
|
|
return stored
|
|
}
|