Files
apskel-pos-backend/internal/service/gamification_service.go
T
efrilmandClaude Opus 5.5 d1e543a79f refactor(loyalty): remove the dead customer points and tokens code
First part of PC-601 (docs/prd-point-coin.md ยง10.7): the code that has
had no way in since balances moved to the wallet.

- The /marketing/customer-points and /marketing/customer-tokens routes
  were commented out; their 16 handler methods, the GamificationService
  methods behind them, and the validators, transformers, mappers and
  contract/model types only they used are gone.
- CustomerPointsProcessor loses its "not implemented" stubs; it keeps the
  customer app's balance, wallet and games endpoints.
- CustomerTokensProcessor and the customer points and tokens repositories,
  wired but no longer called by anything, are gone.

What stays until its preconditions are met: the customer_points and
customer_tokens tables and their entities, which cmd/wallet-migrate still
reads, and the /customer/points, /customer/tokens aliases and the
token_used / tokens_remaining fields, until the apps no longer use them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 14:43:15 +07:00

313 lines
13 KiB
Go

package service
import (
"apskel-pos-be/internal/contract"
"apskel-pos-be/internal/processor"
"apskel-pos-be/internal/transformer"
"context"
"time"
"github.com/google/uuid"
)
type GamificationService interface {
// Tiers
CreateTier(ctx context.Context, req *contract.CreateTierRequest) (*contract.TierResponse, error)
GetTier(ctx context.Context, id uuid.UUID) (*contract.TierResponse, error)
ListTiers(ctx context.Context, query *contract.ListTiersRequest) (*contract.PaginatedTiersResponse, error)
UpdateTier(ctx context.Context, id uuid.UUID, req *contract.UpdateTierRequest) (*contract.TierResponse, error)
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)
ListOmsetTrackers(ctx context.Context, query *contract.ListOmsetTrackerRequest) (*contract.PaginatedOmsetTrackerResponse, error)
UpdateOmsetTracker(ctx context.Context, id uuid.UUID, req *contract.UpdateOmsetTrackerRequest) (*contract.OmsetTrackerResponse, error)
DeleteOmsetTracker(ctx context.Context, id uuid.UUID) error
AddOmset(ctx context.Context, periodType string, periodStart, periodEnd time.Time, amount int64, gameID *uuid.UUID) (*contract.OmsetTrackerResponse, error)
}
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,
}
}
// Tier Service Methods
func (s *GamificationServiceImpl) CreateTier(ctx context.Context, req *contract.CreateTierRequest) (*contract.TierResponse, error) {
modelReq := transformer.CreateTierRequestToModel(req)
response, err := s.tierProcessor.CreateTier(ctx, modelReq)
if err != nil {
return nil, err
}
return transformer.TierModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetTier(ctx context.Context, id uuid.UUID) (*contract.TierResponse, error) {
response, err := s.tierProcessor.GetTier(ctx, id)
if err != nil {
return nil, err
}
return transformer.TierModelToResponse(response), nil
}
func (s *GamificationServiceImpl) ListTiers(ctx context.Context, query *contract.ListTiersRequest) (*contract.PaginatedTiersResponse, error) {
modelQuery := transformer.ListTiersRequestToModel(query)
response, err := s.tierProcessor.ListTiers(ctx, modelQuery)
if err != nil {
return nil, err
}
return transformer.PaginatedTiersResponseToContract(response), nil
}
func (s *GamificationServiceImpl) UpdateTier(ctx context.Context, id uuid.UUID, req *contract.UpdateTierRequest) (*contract.TierResponse, error) {
modelReq := transformer.UpdateTierRequestToModel(req)
response, err := s.tierProcessor.UpdateTier(ctx, id, modelReq)
if err != nil {
return nil, err
}
return transformer.TierModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeleteTier(ctx context.Context, id uuid.UUID) error {
return s.tierProcessor.DeleteTier(ctx, id)
}
func (s *GamificationServiceImpl) GetTierByPoints(ctx context.Context, points int64) (*contract.TierResponse, error) {
response, err := s.tierProcessor.GetTierByPoints(ctx, points)
if err != nil {
return nil, err
}
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)
response, err := s.omsetTrackerProcessor.CreateOmsetTracker(ctx, modelReq)
if err != nil {
return nil, err
}
return transformer.OmsetTrackerModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetOmsetTracker(ctx context.Context, id uuid.UUID) (*contract.OmsetTrackerResponse, error) {
response, err := s.omsetTrackerProcessor.GetOmsetTracker(ctx, id)
if err != nil {
return nil, err
}
return transformer.OmsetTrackerModelToResponse(response), nil
}
func (s *GamificationServiceImpl) ListOmsetTrackers(ctx context.Context, query *contract.ListOmsetTrackerRequest) (*contract.PaginatedOmsetTrackerResponse, error) {
modelQuery := transformer.ListOmsetTrackerRequestToModel(query)
response, err := s.omsetTrackerProcessor.ListOmsetTrackers(ctx, modelQuery)
if err != nil {
return nil, err
}
return transformer.PaginatedOmsetTrackerResponseToContract(response), nil
}
func (s *GamificationServiceImpl) UpdateOmsetTracker(ctx context.Context, id uuid.UUID, req *contract.UpdateOmsetTrackerRequest) (*contract.OmsetTrackerResponse, error) {
modelReq := transformer.UpdateOmsetTrackerRequestToModel(req)
response, err := s.omsetTrackerProcessor.UpdateOmsetTracker(ctx, id, modelReq)
if err != nil {
return nil, err
}
return transformer.OmsetTrackerModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeleteOmsetTracker(ctx context.Context, id uuid.UUID) error {
return s.omsetTrackerProcessor.DeleteOmsetTracker(ctx, id)
}
func (s *GamificationServiceImpl) AddOmset(ctx context.Context, periodType string, periodStart, periodEnd time.Time, amount int64, gameID *uuid.UUID) (*contract.OmsetTrackerResponse, error) {
response, err := s.omsetTrackerProcessor.AddOmset(ctx, periodType, periodStart, periodEnd, amount, gameID)
if err != nil {
return nil, err
}
return transformer.OmsetTrackerModelToResponse(response), nil
}