EnakGame phase 10 of docs/tasks-enakgame.md (EG-1001 to EG-1003). Spin (EG-1001) - PROBABILITY entries take an optional label (a wheel segment). The customer game list shows a PROBABILITY game's prizes (entry, label, amount, never weights), and completing returns the drawn prize, so the client can draw the wheel and stop it on the server's draw. - docs/enakgame-spin.md: the admin steps to set up spin per organization (no seeder) and the customer app flow. An HTTP test plays it end to end. Old game flow removed (EG-1002) - Routes POST /customer/spin, GET /customer/games, GET /customer/ferris-wheel, and admin /marketing/games, /marketing/game-prizes, /marketing/rewards, with their handlers, services, processors, repositories, validators, models, contracts, mappers and tests (GamePlayProcessor, SpinGameService, rewards, ...). This also closes RFC ยง15 findings 1 and 2 (double charge, spinning another org's game). - Tables games, game_prizes, game_plays and rewards stay for ledger history. entities.StringSlice moves to its own file; the omset tracker (unrouted) keeps game_id but no longer embeds the old game response. games.is_active dropped (EG-1003) - Migration 000115; nothing reads metadata.coin_cost any more. The EnakPoint integration docs now point at /customer/enakgame. The Postgres tests were not run: no test database here. Migration 000115 has not been run anywhere. The customer app must stop calling the removed endpoints before this is deployed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
7.0 KiB
Go
166 lines
7.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/contract"
|
|
"apskel-pos-be/internal/logger"
|
|
"apskel-pos-be/internal/models"
|
|
"apskel-pos-be/internal/processor"
|
|
"apskel-pos-be/internal/repository"
|
|
"apskel-pos-be/internal/service"
|
|
"apskel-pos-be/internal/util"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CustomerPointsHandler struct {
|
|
customerPointsService service.CustomerPointsService
|
|
}
|
|
|
|
func NewCustomerPointsHandler(customerPointsService service.CustomerPointsService) *CustomerPointsHandler {
|
|
return &CustomerPointsHandler{
|
|
customerPointsService: customerPointsService,
|
|
}
|
|
}
|
|
|
|
func (h *CustomerPointsHandler) GetCustomerPoints(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
// Get customer ID from context (set by middleware)
|
|
customerID, exists := c.Get("customer_id")
|
|
if !exists {
|
|
logger.FromContext(ctx).Error("Customer ID not found in context")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Customer ID not found"),
|
|
}), "CustomerPointsHandler::GetCustomerPoints")
|
|
return
|
|
}
|
|
|
|
customerIDStr, ok := customerID.(string)
|
|
if !ok {
|
|
logger.FromContext(ctx).Error("Invalid customer ID type in context")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Invalid customer ID"),
|
|
}), "CustomerPointsHandler::GetCustomerPoints")
|
|
return
|
|
}
|
|
|
|
response, err := h.customerPointsService.GetCustomerPoints(ctx, customerIDStr)
|
|
if err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("CustomerPointsHandler::GetCustomerPoints -> service call failed")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(walletErrorCode(err), constants.RequestEntity, err.Error()),
|
|
}), "CustomerPointsHandler::GetCustomerPoints")
|
|
return
|
|
}
|
|
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "CustomerPointsHandler::GetCustomerPoints")
|
|
}
|
|
|
|
func (h *CustomerPointsHandler) GetCustomerWallet(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
// Get customer ID from context (set by middleware)
|
|
customerID, exists := c.Get("customer_id")
|
|
if !exists {
|
|
logger.FromContext(ctx).Error("Customer ID not found in context")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Customer ID not found"),
|
|
}), "CustomerPointsHandler::GetCustomerWallet")
|
|
return
|
|
}
|
|
|
|
customerIDStr, ok := customerID.(string)
|
|
if !ok {
|
|
logger.FromContext(ctx).Error("Invalid customer ID type in context")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Invalid customer ID"),
|
|
}), "CustomerPointsHandler::GetCustomerWallet")
|
|
return
|
|
}
|
|
|
|
response, err := h.customerPointsService.GetCustomerWallet(ctx, customerIDStr)
|
|
if err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("CustomerPointsHandler::GetCustomerWallet -> service call failed")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(walletErrorCode(err), constants.RequestEntity, err.Error()),
|
|
}), "CustomerPointsHandler::GetCustomerWallet")
|
|
return
|
|
}
|
|
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "CustomerPointsHandler::GetCustomerWallet")
|
|
}
|
|
|
|
// GetCustomerWalletTransactions is GET /customer/wallet/transactions: the ledger, newest
|
|
// first, filterable by currency, type and date (docs/prd-point-coin.md F6).
|
|
func (h *CustomerPointsHandler) GetCustomerWalletTransactions(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
customerID, ok := c.Get("customer_id")
|
|
customerIDStr, isString := customerID.(string)
|
|
if !ok || !isString {
|
|
logger.FromContext(ctx).Error("Customer ID not found in context")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Customer ID not found"),
|
|
}), "CustomerPointsHandler::GetCustomerWalletTransactions")
|
|
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()),
|
|
}), "CustomerPointsHandler::GetCustomerWalletTransactions")
|
|
return
|
|
}
|
|
|
|
response, err := h.customerPointsService.GetCustomerWalletTransactions(ctx, customerIDStr, query)
|
|
if err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("CustomerPointsHandler::GetCustomerWalletTransactions -> service call failed")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(walletErrorCode(err), constants.RequestEntity, err.Error()),
|
|
}), "CustomerPointsHandler::GetCustomerWalletTransactions")
|
|
return
|
|
}
|
|
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "CustomerPointsHandler::GetCustomerWalletTransactions")
|
|
}
|
|
|
|
// walletErrorCode tells a bad request apart from a server failure on the wallet
|
|
// endpoints.
|
|
func walletErrorCode(err error) string {
|
|
switch {
|
|
case errors.Is(err, processor.ErrInvalidWalletQuery):
|
|
return constants.ValidationErrorCode
|
|
case errors.Is(err, repository.ErrWalletNotFound):
|
|
return constants.NotFoundErrorCode
|
|
default:
|
|
return constants.InternalServerErrorCode
|
|
}
|
|
}
|
|
|
|
// GetCustomerWalletExpiring is GET /customer/wallet/expiring: what will expire, per
|
|
// currency and day (docs/prd-point-coin.md F6).
|
|
func (h *CustomerPointsHandler) GetCustomerWalletExpiring(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
customerID, ok := c.Get("customer_id")
|
|
customerIDStr, isString := customerID.(string)
|
|
if !ok || !isString {
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(constants.ValidationErrorCode, constants.AuthHandlerEntity, "Customer ID not found"),
|
|
}), "CustomerPointsHandler::GetCustomerWalletExpiring")
|
|
return
|
|
}
|
|
response, err := h.customerPointsService.GetCustomerWalletExpiring(ctx, customerIDStr)
|
|
if err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("CustomerPointsHandler::GetCustomerWalletExpiring -> service call failed")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
|
contract.NewResponseError(walletErrorCode(err), constants.RequestEntity, err.Error()),
|
|
}), "CustomerPointsHandler::GetCustomerWalletExpiring")
|
|
return
|
|
}
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "CustomerPointsHandler::GetCustomerWalletExpiring")
|
|
}
|