refactor(loyalty): remove the dead customer points and tokens code

First part of PC-601 (docs/prd-point-coin.md §10.7): the code that has
had no way in since balances moved to the wallet.

- The /marketing/customer-points and /marketing/customer-tokens routes
  were commented out; their 16 handler methods, the GamificationService
  methods behind them, and the validators, transformers, mappers and
  contract/model types only they used are gone.
- CustomerPointsProcessor loses its "not implemented" stubs; it keeps the
  customer app's balance, wallet and games endpoints.
- CustomerTokensProcessor and the customer points and tokens repositories,
  wired but no longer called by anything, are gone.

What stays until its preconditions are met: the customer_points and
customer_tokens tables and their entities, which cmd/wallet-migrate still
reads, and the /customer/points, /customer/tokens aliases and the
token_used / tokens_remaining fields, until the apps no longer use them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
efrilm
2026-09-30 14:43:15 +07:00
co-authored by Claude Opus 5.5
parent 550122f29c
commit d1e543a79f
16 changed files with 11 additions and 1731 deletions
@@ -1,97 +0,0 @@
package repository
import (
"context"
"fmt"
"apskel-pos-be/internal/entities"
"gorm.io/gorm"
)
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)
}
type customerPointsRepository struct {
db *gorm.DB
}
func NewCustomerPointsRepository(db *gorm.DB) CustomerPointsRepository {
return &customerPointsRepository{
db: db,
}
}
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
if err != nil {
return 0, fmt.Errorf("failed to get customer total points: %w", err)
}
return totalPoints, nil
}
func (r *customerPointsRepository) GetCustomerTotalTokens(ctx context.Context, customerID string) (int64, error) {
var totalTokens int64
err := r.db.WithContext(ctx).
Model(&entities.CustomerTokens{}).
Where("customer_id = ?", customerID).
Select("COALESCE(SUM(balance), 0)").
Scan(&totalTokens).Error
if err != nil {
return 0, fmt.Errorf("failed to get customer total tokens: %w", err)
}
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")
if limit > 0 {
query = query.Limit(limit)
}
err := query.Find(&pointsHistory).Error
if err != nil {
return nil, fmt.Errorf("failed to get customer points history: %w", err)
}
return pointsHistory, nil
}
func (r *customerPointsRepository) GetCustomerTokensHistory(ctx context.Context, customerID string, limit int) ([]entities.CustomerTokens, error) {
var tokensHistory []entities.CustomerTokens
query := r.db.WithContext(ctx).
Where("customer_id = ?", customerID).
Order("created_at DESC")
if limit > 0 {
query = query.Limit(limit)
}
err := query.Find(&tokensHistory).Error
if err != nil {
return nil, fmt.Errorf("failed to get customer tokens history: %w", err)
}
return tokensHistory, nil
}
@@ -1,131 +0,0 @@
package repository
import (
"apskel-pos-be/internal/entities"
"context"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
)
type CustomerTokensRepository struct {
db *gorm.DB
}
func NewCustomerTokensRepository(db *gorm.DB) *CustomerTokensRepository {
return &CustomerTokensRepository{db: db}
}
func (r *CustomerTokensRepository) Create(ctx context.Context, customerTokens *entities.CustomerTokens) error {
return r.db.WithContext(ctx).Create(customerTokens).Error
}
func (r *CustomerTokensRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.CustomerTokens, error) {
var customerTokens entities.CustomerTokens
err := r.db.WithContext(ctx).Preload("Customer").Where("id = ?", id).First(&customerTokens).Error
if err != nil {
return nil, err
}
return &customerTokens, nil
}
func (r *CustomerTokensRepository) GetByCustomerIDAndType(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType) (*entities.CustomerTokens, error) {
var customerTokens entities.CustomerTokens
err := r.db.WithContext(ctx).Preload("Customer").Where("customer_id = ? AND token_type = ?", customerID, tokenType).First(&customerTokens).Error
if err != nil {
return nil, err
}
return &customerTokens, nil
}
func (r *CustomerTokensRepository) GetByCustomerID(ctx context.Context, customerID uuid.UUID) ([]entities.CustomerTokens, error) {
var customerTokens []entities.CustomerTokens
err := r.db.WithContext(ctx).Preload("Customer").Where("customer_id = ?", customerID).Find(&customerTokens).Error
if err != nil {
return nil, err
}
return customerTokens, nil
}
func (r *CustomerTokensRepository) List(ctx context.Context, offset, limit int, search, tokenType string, sortBy, sortOrder string) ([]entities.CustomerTokens, int64, error) {
var customerTokens []entities.CustomerTokens
var total int64
query := r.db.WithContext(ctx).Preload("Customer")
if search != "" {
searchTerm := "%" + search + "%"
query = query.Joins("JOIN customers ON customer_tokens.customer_id = customers.id").
Where("customers.name ILIKE ? OR customers.email ILIKE ?", searchTerm, searchTerm)
}
if tokenType != "" {
query = query.Where("token_type = ?", tokenType)
}
if err := query.Model(&entities.CustomerTokens{}).Count(&total).Error; err != nil {
return nil, 0, err
}
if sortBy != "" {
if sortOrder == "" {
sortOrder = "asc"
}
query = query.Order(fmt.Sprintf("customer_tokens.%s %s", sortBy, sortOrder))
} else {
query = query.Order("customer_tokens.created_at DESC")
}
err := query.Offset(offset).Limit(limit).Find(&customerTokens).Error
if err != nil {
return nil, 0, err
}
return customerTokens, total, nil
}
func (r *CustomerTokensRepository) Update(ctx context.Context, customerTokens *entities.CustomerTokens) error {
return r.db.WithContext(ctx).Save(customerTokens).Error
}
func (r *CustomerTokensRepository) Delete(ctx context.Context, id uuid.UUID) error {
return r.db.WithContext(ctx).Delete(&entities.CustomerTokens{}, id).Error
}
func (r *CustomerTokensRepository) AddTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType, tokens int64) error {
return r.db.WithContext(ctx).Model(&entities.CustomerTokens{}).
Where("customer_id = ? AND token_type = ?", customerID, tokenType).
Update("balance", gorm.Expr("balance + ?", tokens)).Error
}
func (r *CustomerTokensRepository) DeductTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType, tokens int64) error {
return r.db.WithContext(ctx).Model(&entities.CustomerTokens{}).
Where("customer_id = ? AND token_type = ? AND balance >= ?", customerID, tokenType, tokens).
Update("balance", gorm.Expr("balance - ?", tokens)).Error
}
func (r *CustomerTokensRepository) EnsureCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType) (*entities.CustomerTokens, error) {
customerTokens, err := r.GetByCustomerIDAndType(ctx, customerID, tokenType)
if err == nil {
return customerTokens, nil
}
if err != gorm.ErrRecordNotFound {
return nil, err
}
// Create new customer tokens record
newCustomerTokens := &entities.CustomerTokens{
CustomerID: customerID,
TokenType: tokenType,
Balance: 0,
}
err = r.Create(ctx, newCustomerTokens)
if err != nil {
return nil, err
}
return newCustomerTokens, nil
}