apskel-pos-backend/internal/processor/customer_points_processor.go

223 lines
7.1 KiB
Go
Raw Normal View History

2025-09-17 19:30:17 +07:00
package processor
import (
"context"
"fmt"
2025-09-18 02:01:50 +07:00
"time"
"apskel-pos-be/internal/models"
"apskel-pos-be/internal/repository"
2025-09-17 19:30:17 +07:00
"github.com/google/uuid"
)
type CustomerPointsProcessor struct {
2025-09-18 02:01:50 +07:00
customerPointsRepo repository.CustomerPointsRepository
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func NewCustomerPointsProcessor(customerPointsRepo repository.CustomerPointsRepository) *CustomerPointsProcessor {
2025-09-17 19:30:17 +07:00
return &CustomerPointsProcessor{
customerPointsRepo: customerPointsRepo,
}
}
2025-09-18 02:01:50 +07:00
// Existing gamification methods - placeholder implementations
2025-09-17 19:30:17 +07:00
func (p *CustomerPointsProcessor) CreateCustomerPoints(ctx context.Context, req *models.CreateCustomerPointsRequest) (*models.CustomerPointsResponse, error) {
2025-09-18 02:01:50 +07:00
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
2025-09-17 19:30:17 +07:00
}
func (p *CustomerPointsProcessor) GetCustomerPoints(ctx context.Context, id uuid.UUID) (*models.CustomerPointsResponse, error) {
2025-09-18 02:01:50 +07:00
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
2025-09-17 19:30:17 +07:00
}
func (p *CustomerPointsProcessor) GetCustomerPointsByCustomerID(ctx context.Context, customerID uuid.UUID) (*models.CustomerPointsResponse, error) {
2025-09-18 02:01:50 +07:00
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func (p *CustomerPointsProcessor) ListCustomerPoints(ctx context.Context, query *models.ListCustomerPointsQuery) (*models.PaginatedCustomerPointsResponse, error) {
// Return empty paginated response for now
return &models.PaginatedCustomerPointsResponse{
Data: []models.CustomerPointsResponse{},
TotalCount: 0,
Page: 1,
Limit: 10,
TotalPages: 0,
2025-09-17 19:30:17 +07:00
}, nil
}
func (p *CustomerPointsProcessor) UpdateCustomerPoints(ctx context.Context, id uuid.UUID, req *models.UpdateCustomerPointsRequest) (*models.CustomerPointsResponse, error) {
2025-09-18 02:01:50 +07:00
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
func (p *CustomerPointsProcessor) DeleteCustomerPoints(ctx context.Context, id uuid.UUID) error {
// TODO: Implement this method
return fmt.Errorf("not implemented")
}
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
func (p *CustomerPointsProcessor) AddPoints(ctx context.Context, customerID uuid.UUID, points int64) (*models.CustomerPointsResponse, error) {
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
func (p *CustomerPointsProcessor) DeductPoints(ctx context.Context, customerID uuid.UUID, points int64) (*models.CustomerPointsResponse, error) {
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func (p *CustomerPointsProcessor) GetCustomerTotalPointsAPI(ctx context.Context, customerID string) (*models.GetCustomerPointsResponse, error) {
// Get total points
totalPoints, err := p.customerPointsRepo.GetCustomerTotalPoints(ctx, customerID)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer total points: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Get points history (last 10 records)
pointsHistory, err := p.customerPointsRepo.GetCustomerPointsHistory(ctx, customerID, 10)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer points history: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Convert to response format
var historyItems []models.PointsHistoryItem
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
for _, point := range pointsHistory {
historyItems = append(historyItems, models.PointsHistoryItem{
ID: point.ID.String(),
Points: point.Balance,
Type: "BALANCE",
Description: "Points balance",
CreatedAt: point.CreatedAt,
})
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
var lastUpdated time.Time
if len(pointsHistory) > 0 {
lastUpdated = pointsHistory[0].CreatedAt
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
return &models.GetCustomerPointsResponse{
Status: "SUCCESS",
Message: "Customer points retrieved successfully.",
Data: &models.GetCustomerPointsResponseData{
TotalPoints: totalPoints,
PointsHistory: historyItems,
LastUpdated: lastUpdated,
},
}, nil
}
func (p *CustomerPointsProcessor) GetCustomerTotalTokensAPI(ctx context.Context, customerID string) (*models.GetCustomerTokensResponse, error) {
// Get total tokens
totalTokens, err := p.customerPointsRepo.GetCustomerTotalTokens(ctx, customerID)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer total tokens: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Get tokens history (last 10 records)
tokensHistory, err := p.customerPointsRepo.GetCustomerTokensHistory(ctx, customerID, 10)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer tokens history: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Convert to response format
var historyItems []models.TokensHistoryItem
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
for _, token := range tokensHistory {
historyItems = append(historyItems, models.TokensHistoryItem{
ID: token.ID.String(),
Tokens: token.Balance,
Type: string(token.TokenType),
Description: "Tokens balance",
CreatedAt: token.CreatedAt,
})
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
var lastUpdated time.Time
if len(tokensHistory) > 0 {
lastUpdated = tokensHistory[0].CreatedAt
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
return &models.GetCustomerTokensResponse{
Status: "SUCCESS",
Message: "Customer tokens retrieved successfully.",
Data: &models.GetCustomerTokensResponseData{
TotalTokens: totalTokens,
TokensHistory: historyItems,
LastUpdated: lastUpdated,
},
}, nil
}
func (p *CustomerPointsProcessor) GetCustomerWalletAPI(ctx context.Context, customerID string) (*models.GetCustomerWalletResponse, error) {
// Get total points
totalPoints, err := p.customerPointsRepo.GetCustomerTotalPoints(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to get customer total points: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Get total tokens
totalTokens, err := p.customerPointsRepo.GetCustomerTotalTokens(ctx, customerID)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer total tokens: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Get points history (last 5 records)
pointsHistory, err := p.customerPointsRepo.GetCustomerPointsHistory(ctx, customerID, 5)
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return nil, fmt.Errorf("failed to get customer points history: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
// Get tokens history (last 5 records)
tokensHistory, err := p.customerPointsRepo.GetCustomerTokensHistory(ctx, customerID, 5)
if err != nil {
return nil, fmt.Errorf("failed to get customer tokens history: %w", err)
}
// Convert to response format
var pointsHistoryItems []models.PointsHistoryItem
var tokensHistoryItems []models.TokensHistoryItem
var lastUpdated time.Time
for _, point := range pointsHistory {
pointsHistoryItems = append(pointsHistoryItems, models.PointsHistoryItem{
ID: point.ID.String(),
Points: point.Balance,
Type: "BALANCE",
Description: "Points balance",
CreatedAt: point.CreatedAt,
})
if point.CreatedAt.After(lastUpdated) {
lastUpdated = point.CreatedAt
}
}
for _, token := range tokensHistory {
tokensHistoryItems = append(tokensHistoryItems, models.TokensHistoryItem{
ID: token.ID.String(),
Tokens: token.Balance,
Type: string(token.TokenType),
Description: "Tokens balance",
CreatedAt: token.CreatedAt,
})
if token.CreatedAt.After(lastUpdated) {
lastUpdated = token.CreatedAt
}
}
return &models.GetCustomerWalletResponse{
Status: "SUCCESS",
Message: "Customer wallet retrieved successfully.",
Data: &models.GetCustomerWalletResponseData{
TotalPoints: totalPoints,
TotalTokens: totalTokens,
PointsHistory: pointsHistoryItems,
TokensHistory: tokensHistoryItems,
LastUpdated: lastUpdated,
},
}, nil
2025-09-17 19:30:17 +07:00
}