feat(loyalty): send EnakPoint and EnakCoin to another customer
Adds GET /customer/wallet/transfer/recipient?phone= and
POST /customer/wallet/transfer (docs/prd-point-coin.md F5, Q4, Q16,
PC-402).
The recipient is found by phone number and must be an active customer of
the same organization, not the walk-in customer and not the sender. A
number of another organization answers 404 like an unknown one, so the
check does not reveal who uses the app elsewhere. The recipient check
returns the name and number masked ("Bu*** Sa***", "08**-****-1234").
The organization's transfer settings apply: transfers turned off, the
minimum, the maximum per transaction and the daily limit per currency,
which starts over at midnight WIB. Everything the request alone can get
wrong is refused before the PIN, so it costs no attempt; the PIN then
refuses a transfer held for 24 hours after a PIN reset.
Both wallets are locked in customer_id order, so transfers in opposite
directions cannot deadlock, and the daily limit is summed under the lock.
TRANSFER_OUT takes from the sender's lots in K9 order and TRANSFER_IN
gives the recipient lots with exactly the same expiries, pointing back at
the sender's lots. The rows share a group, reference each other and name
the other customer; descriptions carry only the masked name.
The Idempotency-Key header is required. A retry is recognised under the
lock before the daily limit, so it replays instead of counting twice; the
same key towards another recipient is refused.
The recipient is told by WhatsApp after the commit, as PIN locks are:
NotificationService only reaches staff devices, there is no push channel
to customers yet. A failure to send is logged, never undoes the transfer.
Transfers must not be released before note N3 (legal) is closed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ab3425070b
commit
694d65b6d8
@@ -14,18 +14,42 @@ import (
|
||||
)
|
||||
|
||||
// CustomerWalletService moves balance on the customer's own request: exchanging
|
||||
// EnakCoin into EnakPoint (docs/prd-point-coin.md F4).
|
||||
// EnakCoin into EnakPoint and sending either to another customer
|
||||
// (docs/prd-point-coin.md F4, F5).
|
||||
type CustomerWalletService interface {
|
||||
PreviewExchange(ctx context.Context, customerID uuid.UUID, coins int64) *contract.Response
|
||||
Exchange(ctx context.Context, customerID uuid.UUID, req *contract.ExchangeCoinsRequest, idempotencyKey string, info models.CustomerPinRequestInfo) *contract.Response
|
||||
TransferRecipient(ctx context.Context, customerID uuid.UUID, phoneNumber string) *contract.Response
|
||||
Transfer(ctx context.Context, customerID uuid.UUID, req *contract.TransferWalletRequest, idempotencyKey string, info models.CustomerPinRequestInfo) *contract.Response
|
||||
}
|
||||
|
||||
type CustomerWalletServiceImpl struct {
|
||||
exchanges *processor.WalletExchangeProcessor
|
||||
transfers *processor.WalletTransferProcessor
|
||||
}
|
||||
|
||||
func NewCustomerWalletService(exchanges *processor.WalletExchangeProcessor) *CustomerWalletServiceImpl {
|
||||
return &CustomerWalletServiceImpl{exchanges: exchanges}
|
||||
func NewCustomerWalletService(exchanges *processor.WalletExchangeProcessor, transfers *processor.WalletTransferProcessor) *CustomerWalletServiceImpl {
|
||||
return &CustomerWalletServiceImpl{exchanges: exchanges, transfers: transfers}
|
||||
}
|
||||
|
||||
func (s *CustomerWalletServiceImpl) TransferRecipient(ctx context.Context, customerID uuid.UUID, phoneNumber string) *contract.Response {
|
||||
recipient, err := s.transfers.Recipient(ctx, customerID, phoneNumber)
|
||||
if err != nil {
|
||||
return walletMoveErrorResponse(err)
|
||||
}
|
||||
return contract.BuildSuccessResponse(recipient)
|
||||
}
|
||||
|
||||
func (s *CustomerWalletServiceImpl) Transfer(ctx context.Context, customerID uuid.UUID, req *contract.TransferWalletRequest, idempotencyKey string, info models.CustomerPinRequestInfo) *contract.Response {
|
||||
result, err := s.transfers.Transfer(ctx, customerID, models.WalletTransfer{
|
||||
Currency: req.Currency,
|
||||
Amount: req.Amount,
|
||||
RecipientPhone: req.RecipientPhone,
|
||||
}, req.Pin, idempotencyKey, info)
|
||||
if err != nil {
|
||||
return walletMoveErrorResponse(err)
|
||||
}
|
||||
return contract.BuildSuccessResponse(result)
|
||||
}
|
||||
|
||||
func (s *CustomerWalletServiceImpl) PreviewExchange(ctx context.Context, customerID uuid.UUID, coins int64) *contract.Response {
|
||||
@@ -53,7 +77,8 @@ func walletMoveErrorResponse(err error) *contract.Response {
|
||||
}
|
||||
code := constants.InternalServerErrorCode
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrWalletNotFound):
|
||||
case errors.Is(err, repository.ErrWalletNotFound),
|
||||
errors.Is(err, processor.ErrWalletRecipientNotFound):
|
||||
code = constants.NotFoundErrorCode
|
||||
case errors.Is(err, processor.ErrWalletMoveRejected),
|
||||
errors.Is(err, processor.ErrWalletIdempotencyConflict),
|
||||
|
||||
Reference in New Issue
Block a user