feat(loyalty): typed loyalty settings with change history

Adds LoyaltySettingsProcessor (docs/prd-point-coin.md F1, F2, F12, PC-109).

Reading returns typed settings for an outlet (earning per currency, paying
with EnakPoint) and for an organization (point value, exchange rate,
transfers, and the expiry settings awaiting note N4). A key that was never
set takes the PRD default. A stored value that is unusable, such as an
earn_per_amount of 0 that would divide by zero, also falls back to the
default and is logged, so a bad row never reaches a calculation.

Writing takes the whole settings struct, validates every rule in the PRD
before touching the database, and stores and records in
loyalty_setting_changes only the keys whose effective value changes: old
value (NULL while it was on its default), new value, and who changed it.
Clearing a limit deletes the stored value. Each save runs in one
transaction under an advisory lock per outlet or organization, so two saves
at once cannot both compute their change from the same old value. The
outlet must belong to the caller's organization.

Every key is described once (key, default, valid range, bound field), and
reading, validating and diffing all use that description.

GET /customer/wallet now reads the point value through this processor; the
minimal organization settings repository from PC-106 is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
efrilm
2026-09-30 10:23:08 +07:00
co-authored by Claude Opus 5.5
parent 040780cd2d
commit 39e47ff0e6
15 changed files with 1250 additions and 80 deletions
+9 -21
View File
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
@@ -41,11 +40,17 @@ var walletDisplayLocation = func() *time.Location {
// history and upcoming expiry (docs/prd-point-coin.md F6). It only reads.
type WalletQueryProcessor struct {
repo repository.WalletQueryRepository
settings repository.OrganizationSettingRepository
settings pointValueReader
now func() time.Time
}
func NewWalletQueryProcessor(repo repository.WalletQueryRepository, settings repository.OrganizationSettingRepository) *WalletQueryProcessor {
// pointValueReader gives the rupiah value of one EnakPoint in an organization.
// LoyaltySettingsProcessor is one.
type pointValueReader interface {
PointValue(ctx context.Context, organizationID uuid.UUID) (int64, error)
}
func NewWalletQueryProcessor(repo repository.WalletQueryRepository, settings pointValueReader) *WalletQueryProcessor {
return &WalletQueryProcessor{repo: repo, settings: settings, now: time.Now}
}
@@ -61,7 +66,7 @@ func (p *WalletQueryProcessor) Summary(ctx context.Context, customerID uuid.UUID
if err != nil {
return nil, err
}
pointValue, err := p.pointValue(ctx, organizationID)
pointValue, err := p.settings.PointValue(ctx, organizationID)
if err != nil {
return nil, err
}
@@ -192,23 +197,6 @@ func (p *WalletQueryProcessor) recent(ctx context.Context, customerID uuid.UUID,
return p.toTransactions(ctx, rows)
}
// pointValue reads the organization's rupiah value of one EnakPoint. A missing or
// unusable value falls back to the default rather than failing the whole screen.
func (p *WalletQueryProcessor) pointValue(ctx context.Context, organizationID uuid.UUID) (int64, error) {
raw, ok, err := p.settings.Get(ctx, organizationID, constants.LoyaltyPointValueKey)
if err != nil {
return 0, err
}
if !ok {
return constants.LoyaltyPointValueDefault, nil
}
value, err := strconv.ParseInt(strings.TrimSpace(raw), 10, 64)
if err != nil || value < 1 {
return constants.LoyaltyPointValueDefault, nil
}
return value, nil
}
func (p *WalletQueryProcessor) toTransactions(ctx context.Context, rows []entities.WalletTransaction) ([]models.CustomerWalletTransaction, error) {
return buildWalletTransactions(ctx, p.repo, rows)
}