Adds GET /marketing/wallet-transactions/:id/trace (docs/prd-point-coin.md F7, §8.1, PC-404). From any ledger row of the organization, the trace lists the lots a debit took from, with how much it took from each, or the lots a credit created. Each lot is followed back through origin_lot_id, across transfers, exchanges and refunds, to the lot an EARN, ADJUSTMENT or MIGRATION first created. Every step shows the lot and the row that created it, with the real name of the customer it belongs to, so the example of §8 (A sends 120 to B, B pays 30) leads from B's payment to A's order #ORD-1. Lots are loaded a generation at a time, and a chain stops at 100 steps or at a lot it has already seen, which only bad data could cause. A row of another organization answers 404. The dashboard's wallet view now builds its lots with the same helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
4.1 KiB
Go
100 lines
4.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"apskel-pos-be/internal/appcontext"
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/contract"
|
|
"apskel-pos-be/internal/logger"
|
|
"apskel-pos-be/internal/models"
|
|
"apskel-pos-be/internal/service"
|
|
"apskel-pos-be/internal/util"
|
|
"apskel-pos-be/internal/validator"
|
|
)
|
|
|
|
// WalletAdminHandler serves /marketing/customers/:id/wallet (docs/prd-point-coin.md F7).
|
|
type WalletAdminHandler struct {
|
|
walletAdminService service.WalletAdminService
|
|
walletValidator validator.WalletValidator
|
|
}
|
|
|
|
func NewWalletAdminHandler(walletAdminService service.WalletAdminService, walletValidator validator.WalletValidator) *WalletAdminHandler {
|
|
return &WalletAdminHandler{walletAdminService: walletAdminService, walletValidator: walletValidator}
|
|
}
|
|
|
|
func (h *WalletAdminHandler) GetCustomerWallet(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
customerID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid customer ID"),
|
|
}), "WalletAdminHandler::GetCustomerWallet")
|
|
return
|
|
}
|
|
|
|
var query models.ListCustomerWalletTransactionsQuery
|
|
if err := c.ShouldBindQuery(&query); err != nil {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, err.Error()),
|
|
}), "WalletAdminHandler::GetCustomerWallet")
|
|
return
|
|
}
|
|
|
|
response := h.walletAdminService.GetCustomerWallet(ctx, contextInfo, customerID, query)
|
|
if response.HasErrors() {
|
|
logger.FromContext(ctx).WithError(response.GetErrors()[0]).Error("WalletAdminHandler::GetCustomerWallet -> service call failed")
|
|
}
|
|
util.HandleResponse(c.Writer, c.Request, response, "WalletAdminHandler::GetCustomerWallet")
|
|
}
|
|
|
|
func (h *WalletAdminHandler) AdjustCustomerWallet(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
customerID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid customer ID"),
|
|
}), "WalletAdminHandler::AdjustCustomerWallet")
|
|
return
|
|
}
|
|
|
|
var req contract.AdjustCustomerWalletRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, err.Error()),
|
|
}), "WalletAdminHandler::AdjustCustomerWallet")
|
|
return
|
|
}
|
|
if validationError, code := h.walletValidator.ValidateAdjustRequest(&req); validationError != nil {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(code, constants.RequestEntity, validationError.Error()),
|
|
}), "WalletAdminHandler::AdjustCustomerWallet")
|
|
return
|
|
}
|
|
|
|
response := h.walletAdminService.AdjustCustomerWallet(ctx, contextInfo, customerID, &req)
|
|
if response.HasErrors() {
|
|
logger.FromContext(ctx).WithError(response.GetErrors()[0]).Error("WalletAdminHandler::AdjustCustomerWallet -> service call failed")
|
|
}
|
|
util.HandleResponse(c.Writer, c.Request, response, "WalletAdminHandler::AdjustCustomerWallet")
|
|
}
|
|
|
|
// TraceTransaction is GET /marketing/wallet-transactions/:id/trace.
|
|
func (h *WalletAdminHandler) TraceTransaction(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
transactionID, ok := parseUUIDParam(c, "id", "WalletAdminHandler::TraceTransaction")
|
|
if !ok {
|
|
return
|
|
}
|
|
response := h.walletAdminService.TraceTransaction(ctx, appcontext.FromGinContext(ctx), transactionID)
|
|
if response.HasErrors() {
|
|
logger.FromContext(ctx).WithError(response.GetErrors()[0]).Error("WalletAdminHandler::TraceTransaction -> service call failed")
|
|
}
|
|
util.HandleResponse(c.Writer, c.Request, response, "WalletAdminHandler::TraceTransaction")
|
|
}
|