This commit is contained in:
Aditya Siregar
2025-09-18 02:01:50 +07:00
parent 259b8a11b5
commit f64fec1fe2
10 changed files with 743 additions and 247 deletions
+166 -140
View File
@@ -1,196 +1,222 @@
package processor
import (
"apskel-pos-be/internal/mappers"
"context"
"fmt"
"time"
"apskel-pos-be/internal/models"
"apskel-pos-be/internal/repository"
"context"
"errors"
"fmt"
"github.com/google/uuid"
)
type CustomerPointsProcessor struct {
customerPointsRepo *repository.CustomerPointsRepository
customerPointsRepo repository.CustomerPointsRepository
}
func NewCustomerPointsProcessor(customerPointsRepo *repository.CustomerPointsRepository) *CustomerPointsProcessor {
func NewCustomerPointsProcessor(customerPointsRepo repository.CustomerPointsRepository) *CustomerPointsProcessor {
return &CustomerPointsProcessor{
customerPointsRepo: customerPointsRepo,
}
}
// CreateCustomerPoints creates a new customer points record
// Existing gamification methods - placeholder implementations
func (p *CustomerPointsProcessor) CreateCustomerPoints(ctx context.Context, req *models.CreateCustomerPointsRequest) (*models.CustomerPointsResponse, error) {
// Convert request to entity
customerPoints := mappers.ToCustomerPointsEntity(req)
// Create customer points
err := p.customerPointsRepo.Create(ctx, customerPoints)
if err != nil {
return nil, fmt.Errorf("failed to create customer points: %w", err)
}
return mappers.ToCustomerPointsResponse(customerPoints), nil
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
// GetCustomerPoints retrieves customer points by ID
func (p *CustomerPointsProcessor) GetCustomerPoints(ctx context.Context, id uuid.UUID) (*models.CustomerPointsResponse, error) {
customerPoints, err := p.customerPointsRepo.GetByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("customer points not found: %w", err)
}
return mappers.ToCustomerPointsResponse(customerPoints), nil
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
// GetCustomerPointsByCustomerID retrieves customer points by customer ID
func (p *CustomerPointsProcessor) GetCustomerPointsByCustomerID(ctx context.Context, customerID uuid.UUID) (*models.CustomerPointsResponse, error) {
customerPoints, err := p.customerPointsRepo.EnsureCustomerPoints(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to get customer points: %w", err)
}
return mappers.ToCustomerPointsResponse(customerPoints), nil
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
// ListCustomerPoints retrieves customer points with pagination and filtering
func (p *CustomerPointsProcessor) ListCustomerPoints(ctx context.Context, query *models.ListCustomerPointsQuery) (*models.PaginatedResponse[models.CustomerPointsResponse], error) {
// Set default values
if query.Page <= 0 {
query.Page = 1
}
if query.Limit <= 0 {
query.Limit = 10
}
if query.Limit > 100 {
query.Limit = 100
}
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,
}, nil
}
offset := (query.Page - 1) * query.Limit
func (p *CustomerPointsProcessor) UpdateCustomerPoints(ctx context.Context, id uuid.UUID, req *models.UpdateCustomerPointsRequest) (*models.CustomerPointsResponse, error) {
// TODO: Implement this method
return nil, fmt.Errorf("not implemented")
}
// Get customer points from repository
customerPoints, total, err := p.customerPointsRepo.List(
ctx,
offset,
query.Limit,
query.Search,
query.SortBy,
query.SortOrder,
)
func (p *CustomerPointsProcessor) DeleteCustomerPoints(ctx context.Context, id uuid.UUID) error {
// TODO: Implement this method
return fmt.Errorf("not implemented")
}
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")
}
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")
}
func (p *CustomerPointsProcessor) GetCustomerTotalPointsAPI(ctx context.Context, customerID string) (*models.GetCustomerPointsResponse, error) {
// Get total points
totalPoints, err := p.customerPointsRepo.GetCustomerTotalPoints(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to list customer points: %w", err)
return nil, fmt.Errorf("failed to get customer total points: %w", err)
}
// Convert to responses
responses := mappers.ToCustomerPointsResponses(customerPoints)
// Get points history (last 10 records)
pointsHistory, err := p.customerPointsRepo.GetCustomerPointsHistory(ctx, customerID, 10)
if err != nil {
return nil, fmt.Errorf("failed to get customer points history: %w", err)
}
// Calculate pagination info
totalPages := int((total + int64(query.Limit) - 1) / int64(query.Limit))
// Convert to response format
var historyItems []models.PointsHistoryItem
return &models.PaginatedResponse[models.CustomerPointsResponse]{
Data: responses,
Pagination: models.Pagination{
Page: query.Page,
Limit: query.Limit,
Total: total,
TotalPages: totalPages,
for _, point := range pointsHistory {
historyItems = append(historyItems, models.PointsHistoryItem{
ID: point.ID.String(),
Points: point.Balance,
Type: "BALANCE",
Description: "Points balance",
CreatedAt: point.CreatedAt,
})
}
var lastUpdated time.Time
if len(pointsHistory) > 0 {
lastUpdated = pointsHistory[0].CreatedAt
}
return &models.GetCustomerPointsResponse{
Status: "SUCCESS",
Message: "Customer points retrieved successfully.",
Data: &models.GetCustomerPointsResponseData{
TotalPoints: totalPoints,
PointsHistory: historyItems,
LastUpdated: lastUpdated,
},
}, nil
}
// UpdateCustomerPoints updates an existing customer points record
func (p *CustomerPointsProcessor) UpdateCustomerPoints(ctx context.Context, id uuid.UUID, req *models.UpdateCustomerPointsRequest) (*models.CustomerPointsResponse, error) {
// Get existing customer points
customerPoints, err := p.customerPointsRepo.GetByID(ctx, id)
func (p *CustomerPointsProcessor) GetCustomerTotalTokensAPI(ctx context.Context, customerID string) (*models.GetCustomerTokensResponse, error) {
// Get total tokens
totalTokens, err := p.customerPointsRepo.GetCustomerTotalTokens(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("customer points not found: %w", err)
return nil, fmt.Errorf("failed to get customer total tokens: %w", err)
}
// Update customer points fields
mappers.UpdateCustomerPointsEntity(customerPoints, req)
// Save updated customer points
err = p.customerPointsRepo.Update(ctx, customerPoints)
// Get tokens history (last 10 records)
tokensHistory, err := p.customerPointsRepo.GetCustomerTokensHistory(ctx, customerID, 10)
if err != nil {
return nil, fmt.Errorf("failed to update customer points: %w", err)
return nil, fmt.Errorf("failed to get customer tokens history: %w", err)
}
return mappers.ToCustomerPointsResponse(customerPoints), nil
// Convert to response format
var historyItems []models.TokensHistoryItem
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,
})
}
var lastUpdated time.Time
if len(tokensHistory) > 0 {
lastUpdated = tokensHistory[0].CreatedAt
}
return &models.GetCustomerTokensResponse{
Status: "SUCCESS",
Message: "Customer tokens retrieved successfully.",
Data: &models.GetCustomerTokensResponseData{
TotalTokens: totalTokens,
TokensHistory: historyItems,
LastUpdated: lastUpdated,
},
}, nil
}
// DeleteCustomerPoints deletes a customer points record
func (p *CustomerPointsProcessor) DeleteCustomerPoints(ctx context.Context, id uuid.UUID) error {
// Get existing customer points
_, err := p.customerPointsRepo.GetByID(ctx, id)
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 fmt.Errorf("customer points not found: %w", err)
return nil, fmt.Errorf("failed to get customer total points: %w", err)
}
// Delete customer points
err = p.customerPointsRepo.Delete(ctx, id)
// Get total tokens
totalTokens, err := p.customerPointsRepo.GetCustomerTotalTokens(ctx, customerID)
if err != nil {
return fmt.Errorf("failed to delete customer points: %w", err)
return nil, fmt.Errorf("failed to get customer total tokens: %w", err)
}
return nil
}
// AddPoints adds points to a customer's balance
func (p *CustomerPointsProcessor) AddPoints(ctx context.Context, customerID uuid.UUID, points int64) (*models.CustomerPointsResponse, error) {
if points <= 0 {
return nil, errors.New("points must be greater than 0")
}
// Ensure customer points record exists
_, err := p.customerPointsRepo.EnsureCustomerPoints(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to ensure customer points: %w", err)
}
// Add points
err = p.customerPointsRepo.AddPoints(ctx, customerID, points)
if err != nil {
return nil, fmt.Errorf("failed to add points: %w", err)
}
// Get updated customer points
customerPoints, err := p.customerPointsRepo.GetByCustomerID(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to get updated customer points: %w", err)
}
return mappers.ToCustomerPointsResponse(customerPoints), nil
}
// DeductPoints deducts points from a customer's balance
func (p *CustomerPointsProcessor) DeductPoints(ctx context.Context, customerID uuid.UUID, points int64) (*models.CustomerPointsResponse, error) {
if points <= 0 {
return nil, errors.New("points must be greater than 0")
}
// Get current customer points
customerPoints, err := p.customerPointsRepo.GetByCustomerID(ctx, customerID)
// Get points history (last 5 records)
pointsHistory, err := p.customerPointsRepo.GetCustomerPointsHistory(ctx, customerID, 5)
if err != nil {
return nil, fmt.Errorf("customer points not found: %w", err)
}
if customerPoints.Balance < points {
return nil, errors.New("insufficient points balance")
return nil, fmt.Errorf("failed to get customer points history: %w", err)
}
// Deduct points
err = p.customerPointsRepo.DeductPoints(ctx, customerID, points)
// Get tokens history (last 5 records)
tokensHistory, err := p.customerPointsRepo.GetCustomerTokensHistory(ctx, customerID, 5)
if err != nil {
return nil, fmt.Errorf("failed to deduct points: %w", err)
}
// Get updated customer points
updatedCustomerPoints, err := p.customerPointsRepo.GetByCustomerID(ctx, customerID)
if err != nil {
return nil, fmt.Errorf("failed to get updated customer points: %w", err)
}
return mappers.ToCustomerPointsResponse(updatedCustomerPoints), 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
}