Files
apskel-pos-backend/internal/service/payment_method_service.go
T
efrilmandClaude Opus 5.5 cf5332c281 feat(loyalty): EnakPoint payment method
Adds the system payment method for paying with EnakPoint
(docs/prd-point-coin.md F9, §8, §10.5, PC-303).

Migration 000094 allows the point type, keeps one per organization with a
partial unique index, creates it for every existing organization, and adds
a trigger that creates it for new ones, as the walk-in customer is. It adds
payments.points_used and point_value. Their CHECK is written so it can
never be NULL: the PRD form, (both NULL) OR (both > 0), is NULL for
points_used with a NULL point_value, which a CHECK lets through, so a
payment could have lost the value a refund depends on. A test caught it.

The API cannot create, delete or retype the EnakPoint method, nor turn
another method into one; that answers 400. Renaming it is allowed. The
method list takes the outlet from ?outlet_id= or the user's outlet and
leaves EnakPoint out when that outlet does not accept it, filtered in the
query so the count stays right. The organization-wide active list is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 11:33:25 +07:00

137 lines
5.3 KiB
Go

package service
import (
"context"
"errors"
"apskel-pos-be/internal/appcontext"
"apskel-pos-be/internal/constants"
"apskel-pos-be/internal/contract"
"apskel-pos-be/internal/mappers"
"apskel-pos-be/internal/processor"
"github.com/google/uuid"
)
type PaymentMethodService interface {
CreatePaymentMethod(ctx context.Context, contextInfo *appcontext.ContextInfo, req *contract.CreatePaymentMethodRequest) *contract.Response
GetPaymentMethodByID(ctx context.Context, id uuid.UUID) *contract.Response
ListPaymentMethods(ctx context.Context, req *contract.ListPaymentMethodsRequest) *contract.Response
UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *contract.UpdatePaymentMethodRequest) *contract.Response
DeletePaymentMethod(ctx context.Context, id uuid.UUID) *contract.Response
GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) *contract.Response
}
type PaymentMethodServiceImpl struct {
paymentMethodProcessor processor.PaymentMethodProcessor
}
func NewPaymentMethodService(paymentMethodProcessor processor.PaymentMethodProcessor) PaymentMethodService {
return &PaymentMethodServiceImpl{
paymentMethodProcessor: paymentMethodProcessor,
}
}
func (s *PaymentMethodServiceImpl) CreatePaymentMethod(ctx context.Context, contextInfo *appcontext.ContextInfo, req *contract.CreatePaymentMethodRequest) *contract.Response {
modelReq := mappers.CreatePaymentMethodContractToModel(req)
if modelReq.OrganizationID == uuid.Nil && contextInfo != nil {
modelReq.OrganizationID = contextInfo.OrganizationID
}
response, err := s.paymentMethodProcessor.CreatePaymentMethod(ctx, modelReq)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError(paymentMethodErrorCode(err, "PAYMENT_METHOD_CREATE_ERROR"), "payment_method", err.Error()),
})
}
contractResponse := mappers.PaymentMethodResponseToContract(response)
return contract.BuildSuccessResponse(contractResponse)
}
func (s *PaymentMethodServiceImpl) GetPaymentMethodByID(ctx context.Context, id uuid.UUID) *contract.Response {
response, err := s.paymentMethodProcessor.GetPaymentMethodByID(ctx, id)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError("PAYMENT_METHOD_NOT_FOUND", "payment_method", err.Error()),
})
}
contractResponse := mappers.PaymentMethodResponseToContract(response)
return contract.BuildSuccessResponse(contractResponse)
}
func (s *PaymentMethodServiceImpl) ListPaymentMethods(ctx context.Context, req *contract.ListPaymentMethodsRequest) *contract.Response {
// Convert contract to model
modelReq := mappers.ListPaymentMethodsContractToModel(req)
// Process request
response, err := s.paymentMethodProcessor.ListPaymentMethods(ctx, modelReq)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError("PAYMENT_METHOD_LIST_ERROR", "payment_method", err.Error()),
})
}
// Convert model to contract
contractResponse := mappers.ListPaymentMethodsResponseToContract(response)
return contract.BuildSuccessResponse(contractResponse)
}
func (s *PaymentMethodServiceImpl) UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *contract.UpdatePaymentMethodRequest) *contract.Response {
// Convert contract to model
modelReq := mappers.UpdatePaymentMethodContractToModel(req)
// Process request
response, err := s.paymentMethodProcessor.UpdatePaymentMethod(ctx, id, modelReq)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError(paymentMethodErrorCode(err, "PAYMENT_METHOD_UPDATE_ERROR"), "payment_method", err.Error()),
})
}
// Convert model to contract
contractResponse := mappers.PaymentMethodResponseToContract(response)
return contract.BuildSuccessResponse(contractResponse)
}
func (s *PaymentMethodServiceImpl) DeletePaymentMethod(ctx context.Context, id uuid.UUID) *contract.Response {
err := s.paymentMethodProcessor.DeletePaymentMethod(ctx, id)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError(paymentMethodErrorCode(err, "PAYMENT_METHOD_DELETE_ERROR"), "payment_method", err.Error()),
})
}
return contract.BuildSuccessResponse(map[string]string{"message": "Payment method deleted successfully"})
}
func (s *PaymentMethodServiceImpl) GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) *contract.Response {
responses, err := s.paymentMethodProcessor.GetActivePaymentMethodsByOrganization(ctx, organizationID)
if err != nil {
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError("PAYMENT_METHOD_LIST_ERROR", "payment_method", err.Error()),
})
}
// Convert models to contracts
contractResponses := make([]contract.PaymentMethodResponse, len(responses))
for i, response := range responses {
contractResponse := mappers.PaymentMethodResponseToContract(&response)
if contractResponse != nil {
contractResponses[i] = *contractResponse
}
}
return contract.BuildSuccessResponse(contractResponses)
}
// paymentMethodErrorCode answers a request to create, delete or retype the EnakPoint
// method as a bad request instead of a server error.
func paymentMethodErrorCode(err error, fallback string) string {
if errors.Is(err, processor.ErrSystemPaymentMethod) {
return constants.ValidationErrorCode
}
return fallback
}