feat(enakgame): spin as an EnakGame game; remove the old game flow
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
296708244e
commit
18e87398bb
@@ -13,8 +13,6 @@ type CustomerPointsService interface {
|
||||
GetCustomerWallet(ctx context.Context, customerID string) (*models.GetCustomerWalletResponse, error)
|
||||
GetCustomerWalletTransactions(ctx context.Context, customerID string, query models.ListCustomerWalletTransactionsQuery) (*models.PaginatedResponse[models.CustomerWalletTransaction], error)
|
||||
GetCustomerWalletExpiring(ctx context.Context, customerID string) (*models.CustomerWalletExpiringList, error)
|
||||
GetCustomerGames(ctx context.Context) (*models.GetCustomerGamesResponse, error)
|
||||
GetFerrisWheelGame(ctx context.Context) (*models.GetFerrisWheelGameResponse, error)
|
||||
}
|
||||
|
||||
type customerPointsService struct {
|
||||
@@ -53,24 +51,6 @@ func (s *customerPointsService) GetCustomerWallet(ctx context.Context, customerI
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *customerPointsService) GetCustomerGames(ctx context.Context) (*models.GetCustomerGamesResponse, error) {
|
||||
response, err := s.customerPointsProcessor.GetCustomerGamesAPI(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get customer games: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *customerPointsService) GetFerrisWheelGame(ctx context.Context) (*models.GetFerrisWheelGameResponse, error) {
|
||||
response, err := s.customerPointsProcessor.GetFerrisWheelGameAPI(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get Ferris Wheel game: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *customerPointsService) GetCustomerWalletTransactions(ctx context.Context, customerID string, query models.ListCustomerWalletTransactionsQuery) (*models.PaginatedResponse[models.CustomerWalletTransaction], error) {
|
||||
if customerID == "" {
|
||||
return nil, fmt.Errorf("customer ID is required")
|
||||
|
||||
@@ -19,29 +19,6 @@ type GamificationService interface {
|
||||
DeleteTier(ctx context.Context, id uuid.UUID) error
|
||||
GetTierByPoints(ctx context.Context, points int64) (*contract.TierResponse, error)
|
||||
|
||||
// Games
|
||||
CreateGame(ctx context.Context, req *contract.CreateGameRequest) (*contract.GameResponse, error)
|
||||
GetGame(ctx context.Context, id uuid.UUID) (*contract.GameResponse, error)
|
||||
ListGames(ctx context.Context, query *contract.ListGamesRequest) (*contract.PaginatedGamesResponse, error)
|
||||
UpdateGame(ctx context.Context, id uuid.UUID, req *contract.UpdateGameRequest) (*contract.GameResponse, error)
|
||||
DeleteGame(ctx context.Context, id uuid.UUID) error
|
||||
GetActiveGames(ctx context.Context) ([]contract.GameResponse, error)
|
||||
|
||||
// Game Prizes
|
||||
CreateGamePrize(ctx context.Context, req *contract.CreateGamePrizeRequest) (*contract.GamePrizeResponse, error)
|
||||
GetGamePrize(ctx context.Context, id uuid.UUID) (*contract.GamePrizeResponse, error)
|
||||
GetGamePrizesByGameID(ctx context.Context, gameID uuid.UUID) ([]contract.GamePrizeResponse, error)
|
||||
ListGamePrizes(ctx context.Context, query *contract.ListGamePrizesRequest) (*contract.PaginatedGamePrizesResponse, error)
|
||||
UpdateGamePrize(ctx context.Context, id uuid.UUID, req *contract.UpdateGamePrizeRequest) (*contract.GamePrizeResponse, error)
|
||||
DeleteGamePrize(ctx context.Context, id uuid.UUID) error
|
||||
GetAvailablePrizes(ctx context.Context, gameID uuid.UUID) ([]contract.GamePrizeResponse, error)
|
||||
|
||||
// Game Plays
|
||||
CreateGamePlay(ctx context.Context, req *contract.CreateGamePlayRequest) (*contract.GamePlayResponse, error)
|
||||
GetGamePlay(ctx context.Context, id uuid.UUID) (*contract.GamePlayResponse, error)
|
||||
ListGamePlays(ctx context.Context, query *contract.ListGamePlaysRequest) (*contract.PaginatedGamePlaysResponse, error)
|
||||
PlayGame(ctx context.Context, req *contract.PlayGameRequest) (*contract.PlayGameResponse, error)
|
||||
|
||||
// Omset Tracker
|
||||
CreateOmsetTracker(ctx context.Context, req *contract.CreateOmsetTrackerRequest) (*contract.OmsetTrackerResponse, error)
|
||||
GetOmsetTracker(ctx context.Context, id uuid.UUID) (*contract.OmsetTrackerResponse, error)
|
||||
@@ -53,24 +30,15 @@ type GamificationService interface {
|
||||
|
||||
type GamificationServiceImpl struct {
|
||||
tierProcessor *processor.TierProcessor
|
||||
gameProcessor *processor.GameProcessor
|
||||
gamePrizeProcessor *processor.GamePrizeProcessor
|
||||
gamePlayProcessor *processor.GamePlayProcessor
|
||||
omsetTrackerProcessor *processor.OmsetTrackerProcessor
|
||||
}
|
||||
|
||||
func NewGamificationService(
|
||||
tierProcessor *processor.TierProcessor,
|
||||
gameProcessor *processor.GameProcessor,
|
||||
gamePrizeProcessor *processor.GamePrizeProcessor,
|
||||
gamePlayProcessor *processor.GamePlayProcessor,
|
||||
omsetTrackerProcessor *processor.OmsetTrackerProcessor,
|
||||
) *GamificationServiceImpl {
|
||||
return &GamificationServiceImpl{
|
||||
tierProcessor: tierProcessor,
|
||||
gameProcessor: gameProcessor,
|
||||
gamePrizeProcessor: gamePrizeProcessor,
|
||||
gamePlayProcessor: gamePlayProcessor,
|
||||
omsetTrackerProcessor: omsetTrackerProcessor,
|
||||
}
|
||||
}
|
||||
@@ -123,146 +91,6 @@ func (s *GamificationServiceImpl) GetTierByPoints(ctx context.Context, points in
|
||||
return transformer.TierModelToResponse(response), nil
|
||||
}
|
||||
|
||||
// Game Service Methods
|
||||
func (s *GamificationServiceImpl) CreateGame(ctx context.Context, req *contract.CreateGameRequest) (*contract.GameResponse, error) {
|
||||
modelReq := transformer.CreateGameRequestToModel(req)
|
||||
response, err := s.gameProcessor.CreateGame(ctx, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GameModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetGame(ctx context.Context, id uuid.UUID) (*contract.GameResponse, error) {
|
||||
response, err := s.gameProcessor.GetGame(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GameModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) ListGames(ctx context.Context, query *contract.ListGamesRequest) (*contract.PaginatedGamesResponse, error) {
|
||||
modelQuery := transformer.ListGamesRequestToModel(query)
|
||||
response, err := s.gameProcessor.ListGames(ctx, modelQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.PaginatedGamesResponseToContract(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) UpdateGame(ctx context.Context, id uuid.UUID, req *contract.UpdateGameRequest) (*contract.GameResponse, error) {
|
||||
modelReq := transformer.UpdateGameRequestToModel(req)
|
||||
response, err := s.gameProcessor.UpdateGame(ctx, id, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GameModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) DeleteGame(ctx context.Context, id uuid.UUID) error {
|
||||
return s.gameProcessor.DeleteGame(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetActiveGames(ctx context.Context) ([]contract.GameResponse, error) {
|
||||
response, err := s.gameProcessor.GetActiveGames(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GameModelsToResponses(response), nil
|
||||
}
|
||||
|
||||
// Game Prize Service Methods
|
||||
func (s *GamificationServiceImpl) CreateGamePrize(ctx context.Context, req *contract.CreateGamePrizeRequest) (*contract.GamePrizeResponse, error) {
|
||||
modelReq := transformer.CreateGamePrizeRequestToModel(req)
|
||||
response, err := s.gamePrizeProcessor.CreateGamePrize(ctx, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePrizeModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetGamePrize(ctx context.Context, id uuid.UUID) (*contract.GamePrizeResponse, error) {
|
||||
response, err := s.gamePrizeProcessor.GetGamePrize(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePrizeModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetGamePrizesByGameID(ctx context.Context, gameID uuid.UUID) ([]contract.GamePrizeResponse, error) {
|
||||
response, err := s.gamePrizeProcessor.GetGamePrizesByGameID(ctx, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePrizeModelsToResponses(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) ListGamePrizes(ctx context.Context, query *contract.ListGamePrizesRequest) (*contract.PaginatedGamePrizesResponse, error) {
|
||||
modelQuery := transformer.ListGamePrizesRequestToModel(query)
|
||||
response, err := s.gamePrizeProcessor.ListGamePrizes(ctx, modelQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.PaginatedGamePrizesResponseToContract(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) UpdateGamePrize(ctx context.Context, id uuid.UUID, req *contract.UpdateGamePrizeRequest) (*contract.GamePrizeResponse, error) {
|
||||
modelReq := transformer.UpdateGamePrizeRequestToModel(req)
|
||||
response, err := s.gamePrizeProcessor.UpdateGamePrize(ctx, id, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePrizeModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) DeleteGamePrize(ctx context.Context, id uuid.UUID) error {
|
||||
return s.gamePrizeProcessor.DeleteGamePrize(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetAvailablePrizes(ctx context.Context, gameID uuid.UUID) ([]contract.GamePrizeResponse, error) {
|
||||
response, err := s.gamePrizeProcessor.GetAvailablePrizes(ctx, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePrizeModelsToResponses(response), nil
|
||||
}
|
||||
|
||||
// Game Play Service Methods
|
||||
func (s *GamificationServiceImpl) CreateGamePlay(ctx context.Context, req *contract.CreateGamePlayRequest) (*contract.GamePlayResponse, error) {
|
||||
modelReq := transformer.CreateGamePlayRequestToModel(req)
|
||||
response, err := s.gamePlayProcessor.CreateGamePlay(ctx, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePlayModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) GetGamePlay(ctx context.Context, id uuid.UUID) (*contract.GamePlayResponse, error) {
|
||||
response, err := s.gamePlayProcessor.GetGamePlay(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.GamePlayModelToResponse(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) ListGamePlays(ctx context.Context, query *contract.ListGamePlaysRequest) (*contract.PaginatedGamePlaysResponse, error) {
|
||||
modelQuery := transformer.ListGamePlaysRequestToModel(query)
|
||||
response, err := s.gamePlayProcessor.ListGamePlays(ctx, modelQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.PaginatedGamePlaysResponseToContract(response), nil
|
||||
}
|
||||
|
||||
func (s *GamificationServiceImpl) PlayGame(ctx context.Context, req *contract.PlayGameRequest) (*contract.PlayGameResponse, error) {
|
||||
modelReq := transformer.PlayGameRequestToModel(req)
|
||||
response, err := s.gamePlayProcessor.PlayGame(ctx, modelReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformer.PlayGameModelToResponse(response), nil
|
||||
}
|
||||
|
||||
// Omset Tracker Service Methods
|
||||
func (s *GamificationServiceImpl) CreateOmsetTracker(ctx context.Context, req *contract.CreateOmsetTrackerRequest) (*contract.OmsetTrackerResponse, error) {
|
||||
modelReq := transformer.CreateOmsetTrackerRequestToModel(req)
|
||||
|
||||
@@ -1,198 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"apskel-pos-be/internal/contract"
|
||||
"apskel-pos-be/internal/models"
|
||||
"apskel-pos-be/internal/processor"
|
||||
)
|
||||
|
||||
type RewardService interface {
|
||||
CreateReward(ctx context.Context, req *contract.CreateRewardRequest) (*models.RewardResponse, error)
|
||||
GetReward(ctx context.Context, id string) (*models.RewardResponse, error)
|
||||
ListRewards(ctx context.Context, req *contract.ListRewardsRequest) (*models.ListRewardsResponse, error)
|
||||
UpdateReward(ctx context.Context, req *contract.UpdateRewardRequest) (*models.RewardResponse, error)
|
||||
DeleteReward(ctx context.Context, id string) error
|
||||
UpdateRewardStock(ctx context.Context, id string, newStock int) error
|
||||
GetRewardsByType(ctx context.Context, rewardType string) ([]models.RewardResponse, error)
|
||||
}
|
||||
|
||||
type rewardService struct {
|
||||
rewardProcessor processor.RewardProcessor
|
||||
}
|
||||
|
||||
func NewRewardService(rewardProcessor processor.RewardProcessor) RewardService {
|
||||
return &rewardService{
|
||||
rewardProcessor: rewardProcessor,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *rewardService) CreateReward(ctx context.Context, req *contract.CreateRewardRequest) (*models.RewardResponse, error) {
|
||||
// Validate reward type
|
||||
if err := s.validateRewardType(req.RewardType); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
// Validate cost points
|
||||
if req.CostPoints <= 0 {
|
||||
return nil, fmt.Errorf("cost points must be greater than 0")
|
||||
}
|
||||
|
||||
// Validate stock for physical rewards
|
||||
if req.RewardType == "PHYSICAL" && req.Stock != nil && *req.Stock <= 0 {
|
||||
return nil, fmt.Errorf("physical rewards must have positive stock or unlimited stock")
|
||||
}
|
||||
|
||||
// Validate max per customer
|
||||
if req.MaxPerCustomer <= 0 {
|
||||
return nil, fmt.Errorf("max per customer must be greater than 0")
|
||||
}
|
||||
|
||||
// Create reward
|
||||
response, err := s.rewardProcessor.CreateReward(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create reward: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *rewardService) GetReward(ctx context.Context, id string) (*models.RewardResponse, error) {
|
||||
if id == "" {
|
||||
return nil, fmt.Errorf("reward ID is required")
|
||||
}
|
||||
|
||||
response, err := s.rewardProcessor.GetReward(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get reward: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *rewardService) ListRewards(ctx context.Context, req *contract.ListRewardsRequest) (*models.ListRewardsResponse, error) {
|
||||
// Set default pagination
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.Limit <= 0 {
|
||||
req.Limit = 10
|
||||
}
|
||||
if req.Limit > 100 {
|
||||
req.Limit = 100
|
||||
}
|
||||
|
||||
// Validate reward type filter if provided
|
||||
if req.RewardType != "" {
|
||||
if err := s.validateRewardType(req.RewardType); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate points range if provided
|
||||
if req.MinPoints != nil && req.MaxPoints != nil {
|
||||
if *req.MinPoints > *req.MaxPoints {
|
||||
return nil, fmt.Errorf("min points cannot be greater than max points")
|
||||
}
|
||||
}
|
||||
|
||||
response, err := s.rewardProcessor.ListRewards(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list rewards: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *rewardService) UpdateReward(ctx context.Context, req *contract.UpdateRewardRequest) (*models.RewardResponse, error) {
|
||||
if req.ID.String() == "" {
|
||||
return nil, fmt.Errorf("reward ID is required")
|
||||
}
|
||||
|
||||
// Validate reward type
|
||||
if err := s.validateRewardType(req.RewardType); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
// Validate cost points
|
||||
if req.CostPoints <= 0 {
|
||||
return nil, fmt.Errorf("cost points must be greater than 0")
|
||||
}
|
||||
|
||||
// Validate stock for physical rewards
|
||||
if req.RewardType == "PHYSICAL" && req.Stock != nil && *req.Stock <= 0 {
|
||||
return nil, fmt.Errorf("physical rewards must have positive stock or unlimited stock")
|
||||
}
|
||||
|
||||
// Validate max per customer
|
||||
if req.MaxPerCustomer <= 0 {
|
||||
return nil, fmt.Errorf("max per customer must be greater than 0")
|
||||
}
|
||||
|
||||
response, err := s.rewardProcessor.UpdateReward(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to update reward: %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *rewardService) DeleteReward(ctx context.Context, id string) error {
|
||||
if id == "" {
|
||||
return fmt.Errorf("reward ID is required")
|
||||
}
|
||||
|
||||
err := s.rewardProcessor.DeleteReward(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete reward: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *rewardService) UpdateRewardStock(ctx context.Context, id string, newStock int) error {
|
||||
if id == "" {
|
||||
return fmt.Errorf("reward ID is required")
|
||||
}
|
||||
|
||||
if newStock < 0 {
|
||||
return fmt.Errorf("stock cannot be negative")
|
||||
}
|
||||
|
||||
err := s.rewardProcessor.UpdateRewardStock(ctx, id, newStock)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update reward stock: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *rewardService) GetRewardsByType(ctx context.Context, rewardType string) ([]models.RewardResponse, error) {
|
||||
if rewardType == "" {
|
||||
return nil, fmt.Errorf("reward type is required")
|
||||
}
|
||||
|
||||
// Validate reward type
|
||||
if err := s.validateRewardType(rewardType); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
rewards, err := s.rewardProcessor.GetRewardsByType(ctx, rewardType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get rewards by type: %w", err)
|
||||
}
|
||||
|
||||
return rewards, nil
|
||||
}
|
||||
|
||||
func (s *rewardService) validateRewardType(rewardType string) error {
|
||||
validTypes := []string{"VOUCHER", "PHYSICAL", "DIGITAL", "BALANCE"}
|
||||
for _, validType := range validTypes {
|
||||
if rewardType == validType {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("invalid reward type: %s. Valid types are: VOUCHER, PHYSICAL, DIGITAL, BALANCE", rewardType)
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"apskel-pos-be/internal/models"
|
||||
"apskel-pos-be/internal/processor"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type SpinGameService interface {
|
||||
PlaySpinGame(ctx context.Context, customerID string, req *models.SpinGameRequest) (*models.SpinGameResponse, error)
|
||||
}
|
||||
|
||||
type spinGameService struct {
|
||||
gamePlayProcessor *processor.GamePlayProcessor
|
||||
}
|
||||
|
||||
func NewSpinGameService(gamePlayProcessor *processor.GamePlayProcessor) SpinGameService {
|
||||
return &spinGameService{
|
||||
gamePlayProcessor: gamePlayProcessor,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *spinGameService) PlaySpinGame(ctx context.Context, customerID string, req *models.SpinGameRequest) (*models.SpinGameResponse, error) {
|
||||
// Validate customer ID
|
||||
if customerID == "" {
|
||||
return nil, fmt.Errorf("customer ID is required")
|
||||
}
|
||||
|
||||
// Parse spin ID to UUID
|
||||
spinID, err := uuid.Parse(req.SpinID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid spin ID format: %w", err)
|
||||
}
|
||||
|
||||
// Parse customer ID to UUID
|
||||
customerUUID, err := uuid.Parse(customerID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid customer ID format: %w", err)
|
||||
}
|
||||
|
||||
// PlayGame runs its own transaction and charges the game's coin_cost in EnakCoin.
|
||||
playResponse, err := s.gamePlayProcessor.PlayGame(ctx, &models.PlayGameRequest{
|
||||
GameID: spinID,
|
||||
CustomerID: customerUUID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to play spin game: %w", err)
|
||||
}
|
||||
|
||||
// Convert prize to customer-facing format
|
||||
var customerPrize *models.CustomerGamePrizeResponse
|
||||
if playResponse.PrizeWon != nil {
|
||||
customerPrize = &models.CustomerGamePrizeResponse{
|
||||
ID: playResponse.PrizeWon.ID,
|
||||
GameID: playResponse.PrizeWon.GameID,
|
||||
Name: playResponse.PrizeWon.Name,
|
||||
Image: playResponse.PrizeWon.Image,
|
||||
Metadata: &playResponse.PrizeWon.Metadata,
|
||||
CreatedAt: playResponse.PrizeWon.CreatedAt,
|
||||
UpdatedAt: playResponse.PrizeWon.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to spin game response
|
||||
response := &models.SpinGameResponse{
|
||||
Status: "SUCCESS",
|
||||
Message: "Spin game completed successfully.",
|
||||
Data: &models.SpinGameResponseData{
|
||||
GamePlay: playResponse.GamePlay,
|
||||
PrizeWon: customerPrize,
|
||||
CoinsRemaining: playResponse.CoinsRemaining,
|
||||
},
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user