Files
apskel-pos-backend/migrations/000112_add_budget_controller_to_reward_configs.up.sql
T
efrilmandClaude Opus 5.5 296708244e feat(enakgame): budget controller recommendations and analytics
EnakGame phase 9 of docs/tasks-enakgame.md (EG-901 to EG-903).

Budget Controller (EG-901, EG-902)
- GET /marketing/enakgame/budgets/:id/recommendation, GLOBAL budgets only: the
  multiplier (budget − realized) / (forecast − realized), within one step of 1,
  rounded down to two decimals, either way. Shows each game's new rules.
- POST .../recommendation/accept with the multiplier the admin saw: recomputed in the
  transaction, then one new ACTIVE version per game, the old one RETIRED, audited
  with source budget_controller and RECOMMENDATION_ACCEPTED on the budget.
- Migration 000112: base_config_id, multiplier and budget_id on
  game_reward_configs. Rules are always scaled from the admin's last version, so
  rounding does not compound and min/max are against what the admin set.
- Guardrails in game_budgets.thresholds: max_step_percent 10, min/max multiplier
  50-150%, cooldown_days 7 per organization. Provisional pending RFC §19.2 #4.
- RewardCalculator.Scale for the four reward types: amounts only, rounded down.

Analytics (EG-903)
- GET /marketing/enakgame/analytics/games and /analytics/economy over a range of
  Asia/Jakarta days (at most 366), from game_sessions and the wallet ledger.
- Migrations 000113 (game_sessions by organization and start) and 000114
  (wallet_transactions by organization and time, CONCURRENTLY).

The Postgres tests for accepting and analytics were not run: no test database here.
Migrations 000112-000114 have not been run anywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 21:18:31 +07:00

20 lines
1.3 KiB
SQL

-- Reward configurations made by accepting a Budget Controller recommendation
-- (docs/rfc-enakgame.md §10, PRD §29–§31). Such a version scales the amounts of the
-- version an admin wrote last, its base, so a run of adjustments never compounds its
-- rounding and min/max multiplier are measured against what the admin set. A version
-- an admin writes has none of the three, and becomes the base of the next adjustments.
ALTER TABLE game_reward_configs
ADD COLUMN base_config_id UUID REFERENCES game_reward_configs(id),
-- Applied to every amount of the base, rounded down: 0.8100 pays 81%.
ADD COLUMN multiplier NUMERIC(6,4),
-- The global budget whose recommendation was accepted.
ADD COLUMN budget_id UUID REFERENCES game_budgets(id),
ADD CONSTRAINT chk_game_reward_configs_controller CHECK (
(base_config_id IS NULL AND multiplier IS NULL AND budget_id IS NULL)
OR (base_config_id IS NOT NULL AND multiplier > 0 AND budget_id IS NOT NULL AND effective_at IS NOT NULL));
-- The last adjustment of an organization, for the cooldown. effective_at is when the
-- Budget Controller made it, by the application clock.
CREATE INDEX idx_game_reward_configs_controller
ON game_reward_configs(organization_id, effective_at DESC) WHERE budget_id IS NOT NULL;