feat: rank growth events with grounded evidence
This commit is contained in:
@@ -32,7 +32,10 @@ func ValidateImport(input Import, currentJalaliYear int) error {
|
||||
if len(input.Events) == 0 {
|
||||
return errors.New("event catalog: import contains no events")
|
||||
}
|
||||
return ValidateEvents(input.Events)
|
||||
if err := ValidateEvents(input.Events); err != nil {
|
||||
return err
|
||||
}
|
||||
return ValidateDeclaredCoverage(input.Events, input.CoverageStartJalaliYear, input.CoverageEndJalaliYear)
|
||||
}
|
||||
|
||||
func ValidateApproval(importedBy, reviewedBy string) error {
|
||||
@@ -45,11 +48,22 @@ func ValidateApproval(importedBy, reviewedBy string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateReviewableChecksum(checksum string) error {
|
||||
normalized := strings.ToLower(strings.TrimSpace(checksum))
|
||||
if normalized == "" || strings.Contains(normalized, "review-required") || strings.Contains(normalized, "pending-review") || strings.Contains(normalized, "placeholder") {
|
||||
return errors.New("event catalog: source checksum is not independently reviewed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Repository struct{ pool *pgxpool.Pool }
|
||||
|
||||
func NewRepository(pool *pgxpool.Pool) *Repository { return &Repository{pool: pool} }
|
||||
|
||||
func (r *Repository) ImportDraft(ctx context.Context, input Import) error {
|
||||
if err := ValidateEvents(input.Events); err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := r.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("starting calendar import: %w", err)
|
||||
@@ -62,9 +76,6 @@ func (r *Repository) ImportDraft(ctx context.Context, input Import) error {
|
||||
return fmt.Errorf("creating calendar catalog version: %w", err)
|
||||
}
|
||||
for _, event := range input.Events {
|
||||
if event.OccasionKey == "" {
|
||||
event.OccasionKey = event.ID
|
||||
}
|
||||
if event.Variant == "" {
|
||||
event.Variant = "PRIMARY"
|
||||
}
|
||||
@@ -111,12 +122,16 @@ func (r *Repository) Approve(ctx context.Context, version, reviewer string) erro
|
||||
defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }()
|
||||
var id int64
|
||||
var importedBy string
|
||||
if err := tx.QueryRow(ctx, `SELECT id,imported_by FROM calendar_catalog_versions WHERE version=$1 AND status='DRAFT' FOR UPDATE`, version).Scan(&id, &importedBy); err != nil {
|
||||
var sourceChecksum string
|
||||
if err := tx.QueryRow(ctx, `SELECT id,imported_by,source_checksum FROM calendar_catalog_versions WHERE version=$1 AND status='DRAFT' FOR UPDATE`, version).Scan(&id, &importedBy, &sourceChecksum); err != nil {
|
||||
return fmt.Errorf("loading draft calendar catalog: %w", err)
|
||||
}
|
||||
if err := ValidateApproval(importedBy, reviewer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ValidateReviewableChecksum(sourceChecksum); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE calendar_catalog_versions SET status='APPROVED',reviewed_by=$2,reviewed_at=now() WHERE id=$1`, id, reviewer); err != nil {
|
||||
return fmt.Errorf("approving calendar catalog: %w", err)
|
||||
}
|
||||
@@ -136,9 +151,13 @@ func (r *Repository) Activate(ctx context.Context, version, actor string) error
|
||||
return fmt.Errorf("locking calendar activation: %w", err)
|
||||
}
|
||||
var id int64
|
||||
if err := tx.QueryRow(ctx, `SELECT id FROM calendar_catalog_versions WHERE version=$1 AND status='APPROVED' FOR UPDATE`, version).Scan(&id); err != nil {
|
||||
var sourceChecksum string
|
||||
if err := tx.QueryRow(ctx, `SELECT id,source_checksum FROM calendar_catalog_versions WHERE version=$1 AND status='APPROVED' FOR UPDATE`, version).Scan(&id, &sourceChecksum); err != nil {
|
||||
return fmt.Errorf("loading approved calendar catalog: %w", err)
|
||||
}
|
||||
if err := ValidateReviewableChecksum(sourceChecksum); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE calendar_catalog_versions SET status='RETIRED' WHERE status='ACTIVE'`); err != nil {
|
||||
return fmt.Errorf("retiring active calendar catalog: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user