feat(loyalty): exchange EnakCoin into EnakPoint
Adds GET /customer/wallet/exchange/preview?coins= and POST /customer/wallet/exchange (docs/prd-point-coin.md F4, K3, PC-401). The customer exchanges a multiple of the organization's coin_amount and gets (coins / coin_amount) x point_amount EnakPoint, approved by their PIN (K8). A malformed amount is refused before the PIN is checked, so it costs no attempt. In one transaction the wallet is locked, EXCHANGE_OUT takes the EnakCoin in K9 order and EXCHANGE_IN adds the EnakPoint; the two rows share a group, point at each other and both freeze the rate in their metadata. The EnakPoint are split over the EnakCoin lots they came from, each part keeping its lot's expiry and pointing back at it, so exchanging cannot extend a balance's life. The split takes floor(coins so far x rate) per lot, which adds up exactly because the total is a multiple of coin_amount. EnakPoint have no validity of their own until the expiry model is decided (N4), so the EnakCoin lot is for now the only bound. The Idempotency-Key header (or X-Idempotency-Key) is required. A retry with the same key is recognised under the wallet lock and replayed with the ids and rate the first attempt froze, even if the rate has changed since; the same key for another amount is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0b52edf84e
commit
ab3425070b
@@ -0,0 +1,56 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// WalletMoveCustomer is a customer on either side of an exchange or a transfer.
|
||||
type WalletMoveCustomer struct {
|
||||
ID uuid.UUID
|
||||
OrganizationID uuid.UUID
|
||||
Name string
|
||||
PhoneNumber *string
|
||||
IsDefault bool
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
// WalletMoveRepository reads what exchanges and transfers check before moving a
|
||||
// balance (docs/prd-point-coin.md F4, F5). The balance itself only moves through
|
||||
// WalletProcessor.
|
||||
type WalletMoveRepository interface {
|
||||
// GetCustomer returns ErrWalletNotFound when the customer does not exist.
|
||||
GetCustomer(ctx context.Context, customerID uuid.UUID) (*WalletMoveCustomer, error)
|
||||
}
|
||||
|
||||
type walletMoveRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewWalletMoveRepository(db *gorm.DB) WalletMoveRepository {
|
||||
return &walletMoveRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *walletMoveRepository) GetCustomer(ctx context.Context, customerID uuid.UUID) (*WalletMoveCustomer, error) {
|
||||
return r.customer(ctx, "id = ?", customerID)
|
||||
}
|
||||
|
||||
func (r *walletMoveRepository) customer(ctx context.Context, where string, arg interface{}) (*WalletMoveCustomer, error) {
|
||||
var c WalletMoveCustomer
|
||||
err := DBFromContext(ctx, r.db).WithContext(ctx).
|
||||
Table("customers").
|
||||
Select("id, organization_id, name, phone_number, is_default, is_active").
|
||||
Where(where, arg).
|
||||
Take(&c).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrWalletNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get customer: %w", err)
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user