apskel-pos-backend/internal/repository/customer_points_repository.go

98 lines
2.5 KiB
Go
Raw Normal View History

2025-09-17 19:30:17 +07:00
package repository
import (
"context"
"fmt"
2025-09-18 02:01:50 +07:00
"apskel-pos-be/internal/entities"
2025-09-17 19:30:17 +07:00
"gorm.io/gorm"
)
2025-09-18 02:01:50 +07:00
type CustomerPointsRepository interface {
GetCustomerTotalPoints(ctx context.Context, customerID string) (int64, error)
GetCustomerTotalTokens(ctx context.Context, customerID string) (int64, error)
GetCustomerPointsHistory(ctx context.Context, customerID string, limit int) ([]entities.CustomerPoints, error)
GetCustomerTokensHistory(ctx context.Context, customerID string, limit int) ([]entities.CustomerTokens, error)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
type customerPointsRepository struct {
db *gorm.DB
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func NewCustomerPointsRepository(db *gorm.DB) CustomerPointsRepository {
return &customerPointsRepository{
db: db,
2025-09-17 19:30:17 +07:00
}
}
2025-09-18 02:01:50 +07:00
func (r *customerPointsRepository) GetCustomerTotalPoints(ctx context.Context, customerID string) (int64, error) {
var totalPoints int64
err := r.db.WithContext(ctx).
Model(&entities.CustomerPoints{}).
Where("customer_id = ?", customerID).
Select("COALESCE(SUM(balance), 0)").
Scan(&totalPoints).Error
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 02:01:50 +07:00
return 0, 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
return totalPoints, nil
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func (r *customerPointsRepository) GetCustomerTotalTokens(ctx context.Context, customerID string) (int64, error) {
var totalTokens int64
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
err := r.db.WithContext(ctx).
Model(&entities.CustomerTokens{}).
Where("customer_id = ?", customerID).
2025-09-18 12:42:07 +07:00
Select("COALESCE(SUM(balance), 0)").
2025-09-18 02:01:50 +07:00
Scan(&totalTokens).Error
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
if err != nil {
return 0, 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
return totalTokens, nil
}
func (r *customerPointsRepository) GetCustomerPointsHistory(ctx context.Context, customerID string, limit int) ([]entities.CustomerPoints, error) {
var pointsHistory []entities.CustomerPoints
query := r.db.WithContext(ctx).
Where("customer_id = ?", customerID).
Order("created_at DESC")
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
if limit > 0 {
query = query.Limit(limit)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
err := query.Find(&pointsHistory).Error
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
return pointsHistory, nil
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
func (r *customerPointsRepository) GetCustomerTokensHistory(ctx context.Context, customerID string, limit int) ([]entities.CustomerTokens, error) {
var tokensHistory []entities.CustomerTokens
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
query := r.db.WithContext(ctx).
2025-09-17 19:30:17 +07:00
Where("customer_id = ?", customerID).
2025-09-18 02:01:50 +07:00
Order("created_at DESC")
2025-09-17 19:30:17 +07:00
2025-09-18 02:01:50 +07:00
if limit > 0 {
query = query.Limit(limit)
2025-09-17 19:30:17 +07:00
}
2025-09-18 02:01:50 +07:00
err := query.Find(&tokensHistory).Error
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
return tokensHistory, nil
2025-09-17 19:30:17 +07:00
}