Reapply "feat(loyalty): EnakPoint & EnakCoin" (#32)
This reverts commit 4e24f9bbb0.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"apskel-pos-be/internal/appcontext"
|
||||
"apskel-pos-be/internal/constants"
|
||||
"apskel-pos-be/internal/contract"
|
||||
"apskel-pos-be/internal/processor"
|
||||
"apskel-pos-be/internal/repository"
|
||||
)
|
||||
|
||||
// PointPaymentService serves what the cashier needs before taking EnakPoint
|
||||
// (docs/prd-point-coin.md F9).
|
||||
type PointPaymentService interface {
|
||||
Preview(ctx context.Context, apctx *appcontext.ContextInfo, orderID uuid.UUID) *contract.Response
|
||||
}
|
||||
|
||||
type PointPaymentServiceImpl struct {
|
||||
payments *processor.PointPaymentProcessor
|
||||
}
|
||||
|
||||
func NewPointPaymentService(payments *processor.PointPaymentProcessor) *PointPaymentServiceImpl {
|
||||
return &PointPaymentServiceImpl{payments: payments}
|
||||
}
|
||||
|
||||
func (s *PointPaymentServiceImpl) Preview(ctx context.Context, apctx *appcontext.ContextInfo, orderID uuid.UUID) *contract.Response {
|
||||
preview, err := s.payments.Preview(ctx, apctx.OrganizationID, orderID)
|
||||
if err != nil {
|
||||
return PointPaymentErrorResponse(err)
|
||||
}
|
||||
return contract.BuildSuccessResponse(preview)
|
||||
}
|
||||
|
||||
// PointPaymentErrorResponse answers a refused EnakPoint payment as a bad request, PIN
|
||||
// problems with their own codes, and anything else as a server error.
|
||||
func PointPaymentErrorResponse(err error) *contract.Response {
|
||||
var pinErr *processor.PinError
|
||||
if errors.As(err, &pinErr) {
|
||||
return PinErrorResponse(err)
|
||||
}
|
||||
code := constants.InternalServerErrorCode
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrPointPaymentOrderNotFound):
|
||||
code = constants.NotFoundErrorCode
|
||||
case errors.Is(err, processor.ErrPointPaymentRejected):
|
||||
code = constants.ValidationErrorCode
|
||||
}
|
||||
return contract.BuildErrorResponse([]*contract.ResponseError{
|
||||
contract.NewResponseError(code, constants.WalletServiceEntity, err.Error()),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user