Files
apskel-pos-backend/internal/service/gamification_service.go
T
efrilmandClaude Opus 5.5 18e87398bb 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>
2026-10-07 21:31:56 +07:00

141 lines
5.7 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)
// 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
omsetTrackerProcessor *processor.OmsetTrackerProcessor
}
func NewGamificationService(
tierProcessor *processor.TierProcessor,
omsetTrackerProcessor *processor.OmsetTrackerProcessor,
) *GamificationServiceImpl {
return &GamificationServiceImpl{
tierProcessor: tierProcessor,
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
}
// 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
}