EnakGame phases 1-8 of docs/tasks-enakgame.md (EG-101 to EG-803), built on the existing EnakPoint/EnakCoin wallet (docs/rfc-enakgame.md). Foundation (phase 1) - Migrations 000103-000106: games extended with organization, slug, status, entry cost and result rules, old games archived (not deleted); budgets, versioned reward configs, sessions and session rewards; the ledger types GAME_SPEND_REFUND, GAME_REWARD and REWARD_REDEEM_REFUND; audit_logs. - AuditLogger writes in the caller's transaction only. - enakgame.limit.user_daily and global_daily organization settings. Games and sessions (phases 2-4) - Admin /marketing/enakgame: games, reward config versions (immutable but for status, one ACTIVE per game), budgets with non-overlapping global periods and a daily job opening the next month. - Customer /customer/enakgame: start (Idempotency-Key, entry cost and config frozen on the session), complete (result validation, reward engine, max_reward cap, daily limits via game_reward_counters, one GAME_REWARD per budget), automatic refunds for system errors and deactivated games, and a session job. - Reward engine: FIXED, SCORE_BASED, OUTCOME_BASED, PROBABILITY (crypto/rand), rounded down. Vouchers and budgets (phases 5-6) - Migration 000108 and 000107: vouchers, codes, redemptions, cost attribution; Economy Guard counters. - STATIC and CODE_POOL redemption in one transaction with the REDEEM PIN action; realized cost traced through the lots to the budget that paid the reward. - Budget metrics: realized cost, forecast, exposure and status. Migrations 000109-000110 add the wallet_lots indexes they need, built CONCURRENTLY. Events (phase 7) - Migration 000111: game events, each with its own EVENT budget. Event extras stack per PRD §16 defaults, with event and per-customer limits. External vouchers (phase 8) - VoucherProvider contract, two-step PENDING redemption and a recovery job, tested with a fake provider. No provider adapter is registered yet, so EXTERNAL vouchers stay out of the catalog. Not yet decided before release: reward rounding, event stacking, budget exhaustion policy and thresholds (RFC §19.2). Migrations 000103-000111 have not been run on any shared database. Also fixes a leftover PAYMENT filter in a wallet test and a data race in a test PIN fake. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
9.8 KiB
Go
234 lines
9.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/entities"
|
|
)
|
|
|
|
var (
|
|
// ErrGameBudgetNotFound means no budget with that id in the organization, or no
|
|
// global budget covering the day asked for.
|
|
ErrGameBudgetNotFound = errors.New("enakgame: budget not found")
|
|
// ErrGameBudgetPeriodTaken means a global budget of the organization already
|
|
// starts on that day.
|
|
ErrGameBudgetPeriodTaken = errors.New("enakgame: a global budget already starts on that day")
|
|
// ErrGameBudgetInUse means rewards or events point at the budget, so it stays.
|
|
ErrGameBudgetInUse = errors.New("enakgame: budget is in use")
|
|
// ErrGameBudgetTxRequired is returned by LockGlobalBudgets outside a transaction,
|
|
// where the lock would be released at once.
|
|
ErrGameBudgetTxRequired = errors.New("enakgame: budget lock must run inside a transaction")
|
|
)
|
|
|
|
// GameBudgetFilter selects an organization's budgets.
|
|
type GameBudgetFilter struct {
|
|
OrganizationID uuid.UUID
|
|
// Empty for both scopes.
|
|
Scope string
|
|
Offset int
|
|
Limit int
|
|
}
|
|
|
|
// GameBudgetRepository stores EnakGame budgets (docs/rfc-enakgame.md §5.6), always
|
|
// scoped to an organization.
|
|
//
|
|
// Period dates are written and compared as dates (YYYY-MM-DD of the time given), so
|
|
// the database session's time zone cannot move them to another day.
|
|
type GameBudgetRepository interface {
|
|
CreateBudget(ctx context.Context, budget *entities.GameBudget) error
|
|
GetBudget(ctx context.Context, organizationID, id uuid.UUID) (*entities.GameBudget, error)
|
|
// UpdateBudget stores the name, period, amount, thresholds and exhaustion policy.
|
|
UpdateBudget(ctx context.Context, budget *entities.GameBudget) error
|
|
// ListBudgets returns a page of budgets, latest period first, and the total.
|
|
ListBudgets(ctx context.Context, filter GameBudgetFilter) ([]entities.GameBudget, int64, error)
|
|
// GetGlobalBudgetOn returns the organization's global budget whose period holds
|
|
// day, a date in Asia/Jakarta.
|
|
GetGlobalBudgetOn(ctx context.Context, organizationID uuid.UUID, day time.Time) (*entities.GameBudget, error)
|
|
// DeleteBudget removes a budget nothing points at; otherwise ErrGameBudgetInUse.
|
|
DeleteBudget(ctx context.Context, organizationID, id uuid.UUID) error
|
|
|
|
// LockGlobalBudgets serializes the writers of an organization's global budgets
|
|
// until the transaction ends, so two of them cannot both find a period free.
|
|
LockGlobalBudgets(ctx context.Context, organizationID uuid.UUID) error
|
|
// GlobalBudgetOverlaps reports whether another global budget of the organization
|
|
// shares a day with start..end. except is the budget being changed, if any.
|
|
GlobalBudgetOverlaps(ctx context.Context, organizationID uuid.UUID, start, end time.Time, except *uuid.UUID) (bool, error)
|
|
// CreateNextGlobalBudgets gives every global budget holding today, whose next day
|
|
// no global budget holds yet, a successor: from that next day to the end of its
|
|
// month, with the same amount, thresholds and policy (§12). It returns what it
|
|
// made.
|
|
CreateNextGlobalBudgets(ctx context.Context, today time.Time) ([]entities.GameBudget, error)
|
|
}
|
|
|
|
type gameBudgetRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewGameBudgetRepository(db *gorm.DB) GameBudgetRepository {
|
|
return &gameBudgetRepository{db: db}
|
|
}
|
|
|
|
func budgetDate(t time.Time) string { return t.Format("2006-01-02") }
|
|
|
|
func (r *gameBudgetRepository) CreateBudget(ctx context.Context, budget *entities.GameBudget) error {
|
|
if budget.ID == uuid.Nil {
|
|
budget.ID = uuid.New()
|
|
}
|
|
thresholds := budget.Thresholds
|
|
if len(thresholds) == 0 {
|
|
thresholds = entities.JSONDocument(`{}`)
|
|
}
|
|
var rows []struct{ CreatedAt, UpdatedAt time.Time }
|
|
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
|
|
INSERT INTO game_budgets (id, organization_id, scope, name, period_start, period_end, amount,
|
|
thresholds, exhaustion_policy, created_by)
|
|
VALUES (?, ?, ?, ?, ?::date, ?::date, ?, ?::jsonb, ?, ?)
|
|
RETURNING created_at, updated_at`,
|
|
budget.ID, budget.OrganizationID, budget.Scope, budget.Name, budgetDate(budget.PeriodStart), budgetDate(budget.PeriodEnd),
|
|
budget.Amount, thresholds, budget.ExhaustionPolicy, budget.CreatedBy).Scan(&rows).Error
|
|
if err != nil {
|
|
if isUniqueViolation(err, "uq_game_budgets_global_period") {
|
|
return ErrGameBudgetPeriodTaken
|
|
}
|
|
return fmt.Errorf("failed to create game budget: %w", err)
|
|
}
|
|
budget.Thresholds = thresholds
|
|
budget.CreatedAt, budget.UpdatedAt = rows[0].CreatedAt, rows[0].UpdatedAt
|
|
return nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) first(q *gorm.DB) (*entities.GameBudget, error) {
|
|
var budget entities.GameBudget
|
|
if err := q.First(&budget).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrGameBudgetNotFound
|
|
}
|
|
return nil, fmt.Errorf("failed to read game budget: %w", err)
|
|
}
|
|
return &budget, nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) GetBudget(ctx context.Context, organizationID, id uuid.UUID) (*entities.GameBudget, error) {
|
|
return r.first(DBFromContext(ctx, r.db).WithContext(ctx).Where("organization_id = ? AND id = ?", organizationID, id))
|
|
}
|
|
|
|
func (r *gameBudgetRepository) UpdateBudget(ctx context.Context, budget *entities.GameBudget) error {
|
|
thresholds := budget.Thresholds
|
|
if len(thresholds) == 0 {
|
|
thresholds = entities.JSONDocument(`{}`)
|
|
}
|
|
result := DBFromContext(ctx, r.db).WithContext(ctx).Exec(`
|
|
UPDATE game_budgets SET name = ?, period_start = ?::date, period_end = ?::date, amount = ?,
|
|
thresholds = ?::jsonb, exhaustion_policy = ?, updated_at = NOW()
|
|
WHERE organization_id = ? AND id = ?`,
|
|
budget.Name, budgetDate(budget.PeriodStart), budgetDate(budget.PeriodEnd), budget.Amount,
|
|
thresholds, budget.ExhaustionPolicy, budget.OrganizationID, budget.ID)
|
|
if result.Error != nil {
|
|
if isUniqueViolation(result.Error, "uq_game_budgets_global_period") {
|
|
return ErrGameBudgetPeriodTaken
|
|
}
|
|
return fmt.Errorf("failed to update game budget: %w", result.Error)
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return ErrGameBudgetNotFound
|
|
}
|
|
budget.Thresholds = thresholds
|
|
return nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) ListBudgets(ctx context.Context, filter GameBudgetFilter) ([]entities.GameBudget, int64, error) {
|
|
q := DBFromContext(ctx, r.db).WithContext(ctx).Model(&entities.GameBudget{}).Where("organization_id = ?", filter.OrganizationID)
|
|
if filter.Scope != "" {
|
|
q = q.Where("scope = ?", filter.Scope)
|
|
}
|
|
var total int64
|
|
if err := q.Count(&total).Error; err != nil {
|
|
return nil, 0, fmt.Errorf("failed to count game budgets: %w", err)
|
|
}
|
|
var budgets []entities.GameBudget
|
|
if err := q.Order("period_start DESC, created_at DESC, id").Offset(filter.Offset).Limit(filter.Limit).Find(&budgets).Error; err != nil {
|
|
return nil, 0, fmt.Errorf("failed to list game budgets: %w", err)
|
|
}
|
|
return budgets, total, nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) GetGlobalBudgetOn(ctx context.Context, organizationID uuid.UUID, day time.Time) (*entities.GameBudget, error) {
|
|
d := budgetDate(day)
|
|
return r.first(DBFromContext(ctx, r.db).WithContext(ctx).
|
|
Where("organization_id = ? AND scope = ? AND period_start <= ?::date AND period_end >= ?::date",
|
|
organizationID, constants.GameBudgetScopeGlobal, d, d).
|
|
Order("period_start DESC"))
|
|
}
|
|
|
|
func (r *gameBudgetRepository) DeleteBudget(ctx context.Context, organizationID, id uuid.UUID) error {
|
|
result := DBFromContext(ctx, r.db).WithContext(ctx).Exec(`DELETE FROM game_budgets WHERE organization_id = ? AND id = ?`, organizationID, id)
|
|
if result.Error != nil {
|
|
if isForeignKeyViolation(result.Error) {
|
|
return ErrGameBudgetInUse
|
|
}
|
|
return fmt.Errorf("failed to delete game budget: %w", result.Error)
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return ErrGameBudgetNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) LockGlobalBudgets(ctx context.Context, organizationID uuid.UUID) error {
|
|
tx, ok := ctx.Value(txKey).(*gorm.DB)
|
|
if !ok || tx == nil {
|
|
return ErrGameBudgetTxRequired
|
|
}
|
|
err := tx.WithContext(ctx).Exec(`SELECT pg_advisory_xact_lock(hashtextextended(?, 0))`, "game_budgets:global:"+organizationID.String()).Error
|
|
if err != nil {
|
|
return fmt.Errorf("failed to lock game budgets: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) GlobalBudgetOverlaps(ctx context.Context, organizationID uuid.UUID, start, end time.Time, except *uuid.UUID) (bool, error) {
|
|
q := DBFromContext(ctx, r.db).WithContext(ctx).Model(&entities.GameBudget{}).
|
|
Where("organization_id = ? AND scope = ? AND period_start <= ?::date AND period_end >= ?::date",
|
|
organizationID, constants.GameBudgetScopeGlobal, budgetDate(end), budgetDate(start))
|
|
if except != nil {
|
|
q = q.Where("id <> ?", *except)
|
|
}
|
|
var count int64
|
|
if err := q.Count(&count).Error; err != nil {
|
|
return false, fmt.Errorf("failed to check game budget periods: %w", err)
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func (r *gameBudgetRepository) CreateNextGlobalBudgets(ctx context.Context, today time.Time) ([]entities.GameBudget, error) {
|
|
d := budgetDate(today)
|
|
var created []entities.GameBudget
|
|
// ON CONFLICT covers another instance creating the same successor at once.
|
|
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
|
|
INSERT INTO game_budgets (organization_id, scope, name, period_start, period_end, amount,
|
|
thresholds, exhaustion_policy, created_by)
|
|
SELECT b.organization_id, b.scope, 'Budget global ' || to_char(b.period_end + 1, 'YYYY-MM'),
|
|
b.period_end + 1,
|
|
(date_trunc('month', b.period_end + 1) + INTERVAL '1 month - 1 day')::date,
|
|
b.amount, b.thresholds, b.exhaustion_policy, b.created_by
|
|
FROM game_budgets b
|
|
WHERE b.scope = ? AND b.period_start <= ?::date AND b.period_end >= ?::date
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM game_budgets n
|
|
WHERE n.organization_id = b.organization_id AND n.scope = b.scope
|
|
AND n.period_start <= b.period_end + 1 AND n.period_end >= b.period_end + 1)
|
|
ON CONFLICT DO NOTHING
|
|
RETURNING *`, constants.GameBudgetScopeGlobal, d, d).Scan(&created).Error
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create next game budgets: %w", err)
|
|
}
|
|
return created, nil
|
|
}
|