feat(wallet): customer wallet and manual adjustments in the dashboard
Adds the dashboard side of a customer's wallet (docs/prd-point-coin.md F7, PC-107), under /marketing for admins and managers: - GET /marketing/customers/:id/wallet returns the customer, the ledger and spendable balances, every lot that still holds something (flagged when expired), and a page of history. Unlike the customer's own view, each row carries the real names behind it: the transfer counterparty, the admin or cashier, and the outlet, plus the reason and metadata. - POST /marketing/customers/:id/wallet/adjust takes a signed amount and a required reason. It writes an ADJUSTMENT pointing at the admin through the wallet engine, refuses to take more than the customer can spend, and accepts an optional idempotency key so a retried request adjusts once. Reasons describing a cash-out are refused (K7). The customer must belong to the caller's organization; otherwise both endpoints answer 404. Positive adjustments create non-expiring lots until the expiry model is decided (F12, note N4). The mapping from ledger rows to what the apps show is now shared between the customer and dashboard views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e5db0325cc
commit
a6d5a8b056
@@ -0,0 +1,69 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"apskel-pos-be/internal/appcontext"
|
||||
"apskel-pos-be/internal/constants"
|
||||
"apskel-pos-be/internal/contract"
|
||||
"apskel-pos-be/internal/models"
|
||||
"apskel-pos-be/internal/processor"
|
||||
"apskel-pos-be/internal/repository"
|
||||
)
|
||||
|
||||
// WalletAdminService is the dashboard's access to a customer's wallet
|
||||
// (docs/prd-point-coin.md F7). Every call is scoped to the caller's organization.
|
||||
type WalletAdminService interface {
|
||||
GetCustomerWallet(ctx context.Context, apctx *appcontext.ContextInfo, customerID uuid.UUID, query models.ListCustomerWalletTransactionsQuery) *contract.Response
|
||||
AdjustCustomerWallet(ctx context.Context, apctx *appcontext.ContextInfo, customerID uuid.UUID, req *contract.AdjustCustomerWalletRequest) *contract.Response
|
||||
}
|
||||
|
||||
type WalletAdminServiceImpl struct {
|
||||
walletAdminProcessor *processor.WalletAdminProcessor
|
||||
}
|
||||
|
||||
func NewWalletAdminService(walletAdminProcessor *processor.WalletAdminProcessor) *WalletAdminServiceImpl {
|
||||
return &WalletAdminServiceImpl{walletAdminProcessor: walletAdminProcessor}
|
||||
}
|
||||
|
||||
func (s *WalletAdminServiceImpl) GetCustomerWallet(ctx context.Context, apctx *appcontext.ContextInfo, customerID uuid.UUID, query models.ListCustomerWalletTransactionsQuery) *contract.Response {
|
||||
wallet, err := s.walletAdminProcessor.GetCustomerWallet(ctx, apctx.OrganizationID, customerID, query)
|
||||
if err != nil {
|
||||
return walletErrorResponse(err)
|
||||
}
|
||||
return contract.BuildSuccessResponse(wallet)
|
||||
}
|
||||
|
||||
func (s *WalletAdminServiceImpl) AdjustCustomerWallet(ctx context.Context, apctx *appcontext.ContextInfo, customerID uuid.UUID, req *contract.AdjustCustomerWalletRequest) *contract.Response {
|
||||
result, err := s.walletAdminProcessor.Adjust(ctx, apctx.OrganizationID, apctx.UserID, customerID, models.WalletAdjustment{
|
||||
Currency: req.Currency,
|
||||
Amount: req.Amount,
|
||||
Reason: req.Reason,
|
||||
IdempotencyKey: req.IdempotencyKey,
|
||||
})
|
||||
if err != nil {
|
||||
return walletErrorResponse(err)
|
||||
}
|
||||
return contract.BuildSuccessResponse(result)
|
||||
}
|
||||
|
||||
// walletErrorResponse tells the dashboard's mistakes apart from server failures.
|
||||
func walletErrorResponse(err error) *contract.Response {
|
||||
code, message := constants.InternalServerErrorCode, err.Error()
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrWalletNotFound):
|
||||
code, message = constants.NotFoundErrorCode, "customer not found"
|
||||
case errors.Is(err, repository.ErrWalletInsufficientBalance):
|
||||
code, message = constants.ValidationErrorCode, "the adjustment would take more than the customer can spend"
|
||||
case errors.Is(err, processor.ErrInvalidWalletQuery),
|
||||
errors.Is(err, processor.ErrWalletInvalidEntry),
|
||||
errors.Is(err, processor.ErrWalletIdempotencyConflict):
|
||||
code = constants.ValidationErrorCode
|
||||
}
|
||||
return contract.BuildErrorResponse([]*contract.ResponseError{
|
||||
contract.NewResponseError(code, constants.WalletServiceEntity, message),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user