Files
apskel-pos-backend/internal/processor/payment_method_processor.go
efrilmandClaude Opus 5.5 2c9753fae7 feat(loyalty): remove paying with EnakPoint
EnakPoint can only be redeemed for vouchers now: it can no longer pay for
orders and is never cashed out (docs/enakgame-prd.md §3.2, EG-001,
EG-002). No order was ever paid with EnakPoint, so there is no data to
move.

Removed:
- POST /customer/wallet/payment-code, POST /customer/orders/:id/pay-with-points
  and GET /orders/:id/point-payment/preview, with their processors,
  repositories, services, handlers and tests.
- The point payment method type: paying, splitting and refunding with it,
  the outlet filter on the method list, and the system-method guard.
- points and payment_code on CreatePayment; points_used and point_value
  on payments; accepts_point_payment on the customer outlets.
- The outlet point_payment settings. A PUT that still sends them is
  rejected as an unknown field.
- The EnakPoint split in the payment method analytics.
- PAYMENT and PAYMENT_REFUND from the wallet type rules. Tests that used
  them as a generic EnakPoint debit use REWARD_REDEEM.
- The EnakPoint-paid part from the earning basis, which is
  subtotal − discount again.

Migration 000102 drops the trigger, the point methods and their index,
the payments columns, and the outlet settings, and restores the method
type CHECK without point. payments.payment_method_id is ON DELETE
RESTRICT, so it fails rather than lose a payment made with EnakPoint.

The integration docs list the removed endpoints and fields, and the
EnakPoint & EnakCoin PRD and tasks note what is superseded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 13:48:29 +07:00

182 lines
6.2 KiB
Go

package processor
import (
"context"
"fmt"
"apskel-pos-be/internal/mappers"
"apskel-pos-be/internal/models"
"apskel-pos-be/internal/repository"
"github.com/google/uuid"
)
type PaymentMethodProcessor interface {
CreatePaymentMethod(ctx context.Context, req *models.CreatePaymentMethodRequest) (*models.PaymentMethodResponse, error)
GetPaymentMethodByID(ctx context.Context, id uuid.UUID) (*models.PaymentMethodResponse, error)
ListPaymentMethods(ctx context.Context, req *models.ListPaymentMethodsRequest) (*models.ListPaymentMethodsResponse, error)
UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *models.UpdatePaymentMethodRequest) (*models.PaymentMethodResponse, error)
DeletePaymentMethod(ctx context.Context, id uuid.UUID) error
GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) ([]models.PaymentMethodResponse, error)
}
type PaymentMethodProcessorImpl struct {
paymentMethodRepo repository.PaymentMethodRepository
}
func NewPaymentMethodProcessorImpl(paymentMethodRepo repository.PaymentMethodRepository) *PaymentMethodProcessorImpl {
return &PaymentMethodProcessorImpl{
paymentMethodRepo: paymentMethodRepo,
}
}
func (p *PaymentMethodProcessorImpl) CreatePaymentMethod(ctx context.Context, req *models.CreatePaymentMethodRequest) (*models.PaymentMethodResponse, error) {
exists, err := p.paymentMethodRepo.ExistsByName(ctx, req.OrganizationID, req.Name, nil)
if err != nil {
return nil, fmt.Errorf("failed to check payment method name uniqueness: %w", err)
}
if exists {
return nil, fmt.Errorf("payment method with name '%s' already exists for this organization", req.Name)
}
paymentMethodEntity := mappers.CreatePaymentMethodRequestToEntity(req)
if err := p.paymentMethodRepo.Create(ctx, paymentMethodEntity); err != nil {
return nil, fmt.Errorf("failed to create payment method: %w", err)
}
createdPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, paymentMethodEntity.ID)
if err != nil {
return nil, fmt.Errorf("failed to retrieve created payment method: %w", err)
}
response := mappers.PaymentMethodEntityToResponse(createdPaymentMethod)
return response, nil
}
func (p *PaymentMethodProcessorImpl) GetPaymentMethodByID(ctx context.Context, id uuid.UUID) (*models.PaymentMethodResponse, error) {
paymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("payment method not found: %w", err)
}
response := mappers.PaymentMethodEntityToResponse(paymentMethod)
return response, nil
}
func (p *PaymentMethodProcessorImpl) ListPaymentMethods(ctx context.Context, req *models.ListPaymentMethodsRequest) (*models.ListPaymentMethodsResponse, error) {
filters := make(map[string]interface{})
if req.OrganizationID != nil {
filters["organization_id"] = *req.OrganizationID
}
if req.Type != nil {
filters["type"] = string(*req.Type)
}
if req.IsActive != nil {
filters["is_active"] = *req.IsActive
}
if req.Search != "" {
filters["search"] = req.Search
}
offset := (req.Page - 1) * req.Limit
paymentMethods, total, err := p.paymentMethodRepo.List(ctx, filters, req.Limit, offset)
if err != nil {
return nil, fmt.Errorf("failed to list payment methods: %w", err)
}
// Convert to responses
paymentMethodResponses := make([]models.PaymentMethodResponse, len(paymentMethods))
for i, paymentMethod := range paymentMethods {
response := mappers.PaymentMethodEntityToResponse(paymentMethod)
if response != nil {
paymentMethodResponses[i] = *response
}
}
// Calculate total pages
totalPages := int(total) / req.Limit
if int(total)%req.Limit > 0 {
totalPages++
}
return &models.ListPaymentMethodsResponse{
PaymentMethods: paymentMethodResponses,
TotalCount: int(total),
Page: req.Page,
Limit: req.Limit,
TotalPages: totalPages,
}, nil
}
func (p *PaymentMethodProcessorImpl) UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *models.UpdatePaymentMethodRequest) (*models.PaymentMethodResponse, error) {
// Get existing payment method
existingPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("payment method not found: %w", err)
}
// Check name uniqueness if name is being updated
if req.Name != nil && *req.Name != existingPaymentMethod.Name {
exists, err := p.paymentMethodRepo.ExistsByName(ctx, existingPaymentMethod.OrganizationID, *req.Name, &id)
if err != nil {
return nil, fmt.Errorf("failed to check payment method name uniqueness: %w", err)
}
if exists {
return nil, fmt.Errorf("payment method with name '%s' already exists for this organization", *req.Name)
}
}
// Apply updates
mappers.UpdatePaymentMethodEntityFromRequest(existingPaymentMethod, req)
// Update payment method
if err := p.paymentMethodRepo.Update(ctx, existingPaymentMethod); err != nil {
return nil, fmt.Errorf("failed to update payment method: %w", err)
}
// Get updated payment method
updatedPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to retrieve updated payment method: %w", err)
}
response := mappers.PaymentMethodEntityToResponse(updatedPaymentMethod)
return response, nil
}
func (p *PaymentMethodProcessorImpl) DeletePaymentMethod(ctx context.Context, id uuid.UUID) error {
// Check if payment method exists
_, err := p.paymentMethodRepo.GetByID(ctx, id)
if err != nil {
return fmt.Errorf("payment method not found: %w", err)
}
// TODO: Check if payment method is being used in any payments
// For now, allow deletion
// Delete payment method
if err := p.paymentMethodRepo.Delete(ctx, id); err != nil {
return fmt.Errorf("failed to delete payment method: %w", err)
}
return nil
}
func (p *PaymentMethodProcessorImpl) GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) ([]models.PaymentMethodResponse, error) {
paymentMethods, err := p.paymentMethodRepo.GetActiveByOrganizationID(ctx, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get active payment methods: %w", err)
}
responses := make([]models.PaymentMethodResponse, len(paymentMethods))
for i, paymentMethod := range paymentMethods {
response := mappers.PaymentMethodEntityToResponse(paymentMethod)
if response != nil {
responses[i] = *response
}
}
return responses, nil
}