Files
apskel-pos-backend/internal/mappers/spin_game_mapper.go
efrilmandClaude Opus 5.5 a18bb072f5 feat(loyalty): pay every game with EnakCoin
Games now spend the wallet's EnakCoin instead of the per-type tokens
(docs/prd-point-coin.md F8, K1, PC-403).

GamePlayProcessor.PlayGame charges the game's metadata.coin_cost, 1 when
it is not set; a cost that is not a whole number of at least 1 refuses the
game. In one transaction it picks the prize, takes the EnakCoin with a
GAME_SPEND row pointing at the new game_plays.id (which locks the wallet,
so a customer's plays at the same time queue up), records the play and
takes the prize from stock. The play owns its transaction, so the spin
service no longer wraps it, and the admin play endpoint is now atomic too.

The game, game prize and game play repositories go through DBFromContext
so they join that transaction. DecreaseStock now reports a prize that ran
out (ErrGamePrizeOutOfStock) instead of silently updating nothing; that,
or any other stock failure, cancels the whole play, where it used to be
only printed. The manual AddTokens rollback is gone. Not enough EnakCoin,
an inactive game or a prize that ran out answer 400 on /customer/spin
instead of 500.

game_plays.token_used is renamed coins_used (migration 000095). What a
play costs is no longer the caller's choice, so PlayGameRequest loses
token_used. Responses carry coins_used and coins_remaining; token_used and
tokens_remaining stay as deprecated copies until the apps move over, and
sort_by=token_used still sorts by coins_used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 12:20:42 +07:00

83 lines
2.4 KiB
Go

package mappers
import (
"apskel-pos-be/internal/contract"
"apskel-pos-be/internal/models"
)
// SpinGameRequestContractToModel converts spin game request from contract to model
func SpinGameRequestContractToModel(req *contract.SpinGameRequest) *models.SpinGameRequest {
if req == nil {
return nil
}
return &models.SpinGameRequest{
SpinID: req.SpinID,
}
}
// SpinGameResponseModelToContract converts spin game response from model to contract
func SpinGameResponseModelToContract(resp *models.SpinGameResponse) *contract.SpinGameResponse {
if resp == nil {
return nil
}
return &contract.SpinGameResponse{
Status: resp.Status,
Message: resp.Message,
Data: SpinGameResponseDataModelToContract(resp.Data),
}
}
// SpinGameResponseDataModelToContract converts spin game response data from model to contract
func SpinGameResponseDataModelToContract(data *models.SpinGameResponseData) *contract.SpinGameResponseData {
if data == nil {
return nil
}
return &contract.SpinGameResponseData{
GamePlay: GamePlayResponseModelToContract(&data.GamePlay),
PrizeWon: CustomerGamePrizeResponseModelToContract(data.PrizeWon),
CoinsRemaining: data.CoinsRemaining,
TokensRemaining: data.CoinsRemaining,
}
}
// GamePlayResponseModelToContract converts game play response from model to contract
func GamePlayResponseModelToContract(resp *models.GamePlayResponse) contract.GamePlayResponse {
if resp == nil {
return contract.GamePlayResponse{}
}
return contract.GamePlayResponse{
ID: resp.ID,
GameID: resp.GameID,
CustomerID: resp.CustomerID,
PrizeID: resp.PrizeID,
CoinsUsed: resp.CoinsUsed,
TokenUsed: resp.CoinsUsed,
RandomSeed: resp.RandomSeed,
CreatedAt: resp.CreatedAt,
Game: nil, // Optional field - can be populated separately if needed
Customer: nil, // Optional field - can be populated separately if needed
Prize: nil, // Optional field - can be populated separately if needed
}
}
// CustomerGamePrizeResponseModelToContract converts customer game prize response from model to contract
func CustomerGamePrizeResponseModelToContract(resp *models.CustomerGamePrizeResponse) *contract.CustomerGamePrizeResponse {
if resp == nil {
return nil
}
return &contract.CustomerGamePrizeResponse{
ID: resp.ID,
GameID: resp.GameID,
Name: resp.Name,
Image: resp.Image,
Metadata: resp.Metadata,
CreatedAt: resp.CreatedAt,
UpdatedAt: resp.UpdatedAt,
}
}