apskel-pos-backend/internal/service/gamification_service.go

467 lines
21 KiB
Go
Raw Permalink Normal View History

2025-09-17 19:30:17 +07:00
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 {
// Customer Points
CreateCustomerPoints(ctx context.Context, req *contract.CreateCustomerPointsRequest) (*contract.CustomerPointsResponse, error)
GetCustomerPoints(ctx context.Context, id uuid.UUID) (*contract.CustomerPointsResponse, error)
GetCustomerPointsByCustomerID(ctx context.Context, customerID uuid.UUID) (*contract.CustomerPointsResponse, error)
ListCustomerPoints(ctx context.Context, query *contract.ListCustomerPointsRequest) (*contract.PaginatedCustomerPointsResponse, error)
UpdateCustomerPoints(ctx context.Context, id uuid.UUID, req *contract.UpdateCustomerPointsRequest) (*contract.CustomerPointsResponse, error)
DeleteCustomerPoints(ctx context.Context, id uuid.UUID) error
AddCustomerPoints(ctx context.Context, customerID uuid.UUID, req *contract.AddCustomerPointsRequest) (*contract.CustomerPointsResponse, error)
DeductCustomerPoints(ctx context.Context, customerID uuid.UUID, req *contract.DeductCustomerPointsRequest) (*contract.CustomerPointsResponse, error)
// Customer Tokens
CreateCustomerTokens(ctx context.Context, req *contract.CreateCustomerTokensRequest) (*contract.CustomerTokensResponse, error)
GetCustomerTokens(ctx context.Context, id uuid.UUID) (*contract.CustomerTokensResponse, error)
GetCustomerTokensByCustomerIDAndType(ctx context.Context, customerID uuid.UUID, tokenType string) (*contract.CustomerTokensResponse, error)
ListCustomerTokens(ctx context.Context, query *contract.ListCustomerTokensRequest) (*contract.PaginatedCustomerTokensResponse, error)
UpdateCustomerTokens(ctx context.Context, id uuid.UUID, req *contract.UpdateCustomerTokensRequest) (*contract.CustomerTokensResponse, error)
DeleteCustomerTokens(ctx context.Context, id uuid.UUID) error
AddCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType string, req *contract.AddCustomerTokensRequest) (*contract.CustomerTokensResponse, error)
DeductCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType string, req *contract.DeductCustomerTokensRequest) (*contract.CustomerTokensResponse, error)
// 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 {
customerPointsProcessor *processor.CustomerPointsProcessor
customerTokensProcessor *processor.CustomerTokensProcessor
tierProcessor *processor.TierProcessor
gameProcessor *processor.GameProcessor
gamePrizeProcessor *processor.GamePrizeProcessor
gamePlayProcessor *processor.GamePlayProcessor
omsetTrackerProcessor *processor.OmsetTrackerProcessor
}
func NewGamificationService(
customerPointsProcessor *processor.CustomerPointsProcessor,
customerTokensProcessor *processor.CustomerTokensProcessor,
tierProcessor *processor.TierProcessor,
gameProcessor *processor.GameProcessor,
gamePrizeProcessor *processor.GamePrizeProcessor,
gamePlayProcessor *processor.GamePlayProcessor,
omsetTrackerProcessor *processor.OmsetTrackerProcessor,
) *GamificationServiceImpl {
return &GamificationServiceImpl{
customerPointsProcessor: customerPointsProcessor,
customerTokensProcessor: customerTokensProcessor,
tierProcessor: tierProcessor,
gameProcessor: gameProcessor,
gamePrizeProcessor: gamePrizeProcessor,
gamePlayProcessor: gamePlayProcessor,
omsetTrackerProcessor: omsetTrackerProcessor,
}
}
// Customer Points Service Methods
func (s *GamificationServiceImpl) CreateCustomerPoints(ctx context.Context, req *contract.CreateCustomerPointsRequest) (*contract.CustomerPointsResponse, error) {
modelReq := transformer.CreateCustomerPointsRequestToModel(req)
response, err := s.customerPointsProcessor.CreateCustomerPoints(ctx, modelReq)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetCustomerPoints(ctx context.Context, id uuid.UUID) (*contract.CustomerPointsResponse, error) {
response, err := s.customerPointsProcessor.GetCustomerPoints(ctx, id)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetCustomerPointsByCustomerID(ctx context.Context, customerID uuid.UUID) (*contract.CustomerPointsResponse, error) {
response, err := s.customerPointsProcessor.GetCustomerPointsByCustomerID(ctx, customerID)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
func (s *GamificationServiceImpl) ListCustomerPoints(ctx context.Context, query *contract.ListCustomerPointsRequest) (*contract.PaginatedCustomerPointsResponse, error) {
modelQuery := transformer.ListCustomerPointsRequestToModel(query)
response, err := s.customerPointsProcessor.ListCustomerPoints(ctx, modelQuery)
if err != nil {
return nil, err
}
return transformer.PaginatedCustomerPointsResponseToContract(response), nil
}
func (s *GamificationServiceImpl) UpdateCustomerPoints(ctx context.Context, id uuid.UUID, req *contract.UpdateCustomerPointsRequest) (*contract.CustomerPointsResponse, error) {
modelReq := transformer.UpdateCustomerPointsRequestToModel(req)
response, err := s.customerPointsProcessor.UpdateCustomerPoints(ctx, id, modelReq)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeleteCustomerPoints(ctx context.Context, id uuid.UUID) error {
return s.customerPointsProcessor.DeleteCustomerPoints(ctx, id)
}
func (s *GamificationServiceImpl) AddCustomerPoints(ctx context.Context, customerID uuid.UUID, req *contract.AddCustomerPointsRequest) (*contract.CustomerPointsResponse, error) {
response, err := s.customerPointsProcessor.AddPoints(ctx, customerID, req.Points)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeductCustomerPoints(ctx context.Context, customerID uuid.UUID, req *contract.DeductCustomerPointsRequest) (*contract.CustomerPointsResponse, error) {
response, err := s.customerPointsProcessor.DeductPoints(ctx, customerID, req.Points)
if err != nil {
return nil, err
}
return transformer.CustomerPointsModelToResponse(response), nil
}
// Customer Tokens Service Methods
func (s *GamificationServiceImpl) CreateCustomerTokens(ctx context.Context, req *contract.CreateCustomerTokensRequest) (*contract.CustomerTokensResponse, error) {
modelReq := transformer.CreateCustomerTokensRequestToModel(req)
response, err := s.customerTokensProcessor.CreateCustomerTokens(ctx, modelReq)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetCustomerTokens(ctx context.Context, id uuid.UUID) (*contract.CustomerTokensResponse, error) {
response, err := s.customerTokensProcessor.GetCustomerTokens(ctx, id)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
func (s *GamificationServiceImpl) GetCustomerTokensByCustomerIDAndType(ctx context.Context, customerID uuid.UUID, tokenType string) (*contract.CustomerTokensResponse, error) {
response, err := s.customerTokensProcessor.GetCustomerTokensByCustomerIDAndType(ctx, customerID, tokenType)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
func (s *GamificationServiceImpl) ListCustomerTokens(ctx context.Context, query *contract.ListCustomerTokensRequest) (*contract.PaginatedCustomerTokensResponse, error) {
modelQuery := transformer.ListCustomerTokensRequestToModel(query)
response, err := s.customerTokensProcessor.ListCustomerTokens(ctx, modelQuery)
if err != nil {
return nil, err
}
return transformer.PaginatedCustomerTokensResponseToContract(response), nil
}
func (s *GamificationServiceImpl) UpdateCustomerTokens(ctx context.Context, id uuid.UUID, req *contract.UpdateCustomerTokensRequest) (*contract.CustomerTokensResponse, error) {
modelReq := transformer.UpdateCustomerTokensRequestToModel(req)
response, err := s.customerTokensProcessor.UpdateCustomerTokens(ctx, id, modelReq)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeleteCustomerTokens(ctx context.Context, id uuid.UUID) error {
return s.customerTokensProcessor.DeleteCustomerTokens(ctx, id)
}
func (s *GamificationServiceImpl) AddCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType string, req *contract.AddCustomerTokensRequest) (*contract.CustomerTokensResponse, error) {
response, err := s.customerTokensProcessor.AddTokens(ctx, customerID, tokenType, req.Tokens)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
func (s *GamificationServiceImpl) DeductCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType string, req *contract.DeductCustomerTokensRequest) (*contract.CustomerTokensResponse, error) {
response, err := s.customerTokensProcessor.DeductTokens(ctx, customerID, tokenType, req.Tokens)
if err != nil {
return nil, err
}
return transformer.CustomerTokensModelToResponse(response), nil
}
// 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
}