Files
apskel-pos-backend/internal/entities/voucher.go
T
efrilmandClaude Opus 5.5 798a36bd6c feat(enakgame): game sessions, rewards, vouchers, budgets and events
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>
2026-10-07 20:53:14 +07:00

89 lines
4.4 KiB
Go

package entities
import (
"time"
"github.com/google/uuid"
)
// Voucher is what EnakPoint is redeemed for (docs/rfc-enakgame.md §5.7).
type Voucher struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
OrganizationID uuid.UUID `gorm:"type:uuid;not null" json:"organization_id"`
Name string `gorm:"size:255;not null" json:"name"`
Description *string `gorm:"type:text" json:"description"`
ImageURL *string `gorm:"column:image_url;size:500" json:"image_url"`
VoucherType string `gorm:"size:30;not null" json:"voucher_type"`
// Rupiah: the basis of realized cost.
FaceValue int64 `gorm:"not null" json:"face_value"`
PointCost int64 `gorm:"not null" json:"point_cost"`
// Reporting only.
BusinessCost *int64 `json:"business_cost"`
StockMode string `gorm:"size:20;not null" json:"stock_mode"`
// STATIC only.
Stock *int64 `json:"stock"`
// EXTERNAL only.
Provider *string `gorm:"size:50" json:"provider"`
ProviderRef *string `gorm:"size:255" json:"provider_ref"`
MaxPerCustomer *int `json:"max_per_customer"`
ValidFrom *time.Time `json:"valid_from"`
ValidUntil *time.Time `json:"valid_until"`
Terms JSONDocument `gorm:"type:jsonb;not null;default:'{}'" json:"terms"`
Status string `gorm:"size:20;not null" json:"status"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
func (Voucher) TableName() string { return "vouchers" }
// VoucherCode is one code of a CODE_POOL voucher.
type VoucherCode struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
VoucherID uuid.UUID `gorm:"type:uuid;not null" json:"voucher_id"`
Code string `gorm:"size:255;not null" json:"code"`
Status string `gorm:"size:20;not null" json:"status"`
RedemptionID *uuid.UUID `gorm:"type:uuid" json:"redemption_id"`
ExpiresAt *time.Time `json:"expires_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
func (VoucherCode) TableName() string { return "voucher_codes" }
// VoucherRedemption is one redemption, with the voucher's numbers frozen (P3).
type VoucherRedemption struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
OrganizationID uuid.UUID `gorm:"type:uuid;not null" json:"organization_id"`
CustomerID uuid.UUID `gorm:"type:uuid;not null" json:"customer_id"`
VoucherID uuid.UUID `gorm:"type:uuid;not null" json:"voucher_id"`
IdempotencyKey string `gorm:"size:100;not null" json:"idempotency_key"`
Status string `gorm:"size:20;not null" json:"status"`
FaceValue int64 `gorm:"not null" json:"face_value"`
PointCost int64 `gorm:"not null" json:"point_cost"`
VoucherCodeID *uuid.UUID `gorm:"type:uuid" json:"voucher_code_id"`
ExternalCode *string `gorm:"size:255" json:"external_code"`
ExternalRef *string `gorm:"size:255" json:"external_ref"`
DebitTransactionID uuid.UUID `gorm:"type:uuid;not null" json:"debit_transaction_id"`
RefundTransactionID *uuid.UUID `gorm:"type:uuid" json:"refund_transaction_id"`
FailureReason *string `gorm:"size:255" json:"failure_reason"`
Attempts int `gorm:"not null;default:0" json:"attempts"`
CompletedAt *time.Time `json:"completed_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
func (VoucherRedemption) TableName() string { return "voucher_redemptions" }
// VoucherRedemptionCost is the part of a redemption's face value one source of its
// EnakPoint accounts for (D5). BudgetID is nil for EnakPoint not from EnakGame.
type VoucherRedemptionCost struct {
RedemptionID uuid.UUID `gorm:"type:uuid;not null" json:"redemption_id"`
BudgetID *uuid.UUID `gorm:"type:uuid" json:"budget_id"`
SourceType string `gorm:"size:30;not null" json:"source_type"`
Points int64 `gorm:"not null" json:"points"`
Cost int64 `gorm:"not null" json:"cost"`
RecognizedAt time.Time `gorm:"not null" json:"recognized_at"`
}
func (VoucherRedemptionCost) TableName() string { return "voucher_redemption_costs" }