2023-05-22 12:43:15 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
|
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
2023-06-13 15:02:10 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils"
|
2023-06-22 09:32:06 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools"
|
2023-05-22 12:43:15 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type walletService interface {
|
|
|
|
ReplenishAccountWallet(context.Context, *models.ReplenishAccountWallet) (*models.Account, errors.Error)
|
|
|
|
ChangeCurrency(ctx context.Context, userID string, currency models.CurrencyKey) (*models.Account, errors.Error)
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:02:10 +00:00
|
|
|
type paymentService interface {
|
2023-06-13 15:59:04 +00:00
|
|
|
GetPaymentLink(context.Context, *models.GetPaymentLinkRequest) (string, errors.Error)
|
2023-06-13 15:02:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 12:43:15 +00:00
|
|
|
type Deps struct {
|
2023-06-13 15:02:10 +00:00
|
|
|
Logger *zap.Logger
|
|
|
|
WalletService walletService
|
|
|
|
PaymentService paymentService
|
2023-05-22 12:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Controller struct {
|
2023-06-13 15:02:10 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
walletService walletService
|
|
|
|
paymentService paymentService
|
2023-05-22 12:43:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 15:02:10 +00:00
|
|
|
func New(deps Deps) *Controller {
|
2023-05-22 12:43:15 +00:00
|
|
|
if deps.Logger == nil {
|
|
|
|
log.Panicln("logger is nil on <New (wallet controller)>")
|
|
|
|
}
|
|
|
|
|
|
|
|
if deps.WalletService == nil {
|
|
|
|
log.Panicln("wallet service is nil on <New (wallet controller)>")
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:02:10 +00:00
|
|
|
if deps.PaymentService == nil {
|
|
|
|
log.Panicln("payment service is nil on <New (wallet controller)>")
|
|
|
|
}
|
|
|
|
|
2023-05-22 12:43:15 +00:00
|
|
|
return &Controller{
|
2023-06-13 15:02:10 +00:00
|
|
|
logger: deps.Logger,
|
|
|
|
walletService: deps.WalletService,
|
|
|
|
paymentService: deps.PaymentService,
|
2023-05-22 12:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *Controller) ChangeCurrency(ctx echo.Context) error {
|
|
|
|
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
|
|
|
if !ok {
|
|
|
|
receiver.logger.Error("failed to convert jwt payload to string on <ChangeCurrency> of <WallerController>")
|
|
|
|
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, errors.New(
|
2023-05-22 12:43:15 +00:00
|
|
|
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
|
|
|
errors.ErrInvalidArgs,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
request, bindErr := echotools.Bind[models.ChangeCurrency](ctx)
|
|
|
|
if bindErr != nil {
|
|
|
|
receiver.logger.Error("failed to bind body on <ChangeCurrency> of <WalletController>", zap.Error(bindErr))
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, errors.New(
|
2023-05-22 12:43:15 +00:00
|
|
|
fmt.Errorf("failed to parse body on <ChangeCurrency> of <WalletController>: %w", bindErr),
|
|
|
|
errors.ErrInvalidArgs,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if validate.IsStringEmpty(request.Currency) {
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, errors.New(
|
2023-05-22 12:43:15 +00:00
|
|
|
fmt.Errorf("empty currency key on <ChangeCurrency> of <WalletController>: %w", errors.ErrInvalidArgs),
|
|
|
|
errors.ErrInvalidArgs,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := receiver.walletService.ChangeCurrency(ctx.Request().Context(), userID, request.Currency)
|
|
|
|
if err != nil {
|
2023-06-13 22:41:33 +00:00
|
|
|
receiver.logger.Error("failed to put money on <ChangeCurrency> of <WalletController>", zap.Error(err))
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, err)
|
2023-05-22 12:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(http.StatusOK, account)
|
|
|
|
}
|
2023-06-13 15:02:10 +00:00
|
|
|
|
|
|
|
func (receiver *Controller) GetPaymentLink(ctx echo.Context) error {
|
|
|
|
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
|
|
|
if !ok {
|
|
|
|
receiver.logger.Error("failed to convert jwt payload to string on <GetPaymentLink> of <WallerController>")
|
|
|
|
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, errors.New(
|
2023-06-13 15:02:10 +00:00
|
|
|
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
|
|
|
errors.ErrInvalidArgs,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:59:04 +00:00
|
|
|
request, bindErr := echotools.Bind[models.GetPaymentLinkBody](ctx)
|
2023-06-13 15:02:10 +00:00
|
|
|
if bindErr != nil {
|
|
|
|
receiver.logger.Error("failed to bind body on <GetPaymentLink> of <WalletController>", zap.Error(bindErr))
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, errors.New(
|
2023-06-13 15:02:10 +00:00
|
|
|
fmt.Errorf("failed to parse body on <GetPaymentLink> of <WalletController>: %w", bindErr),
|
|
|
|
errors.ErrInvalidArgs,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:59:04 +00:00
|
|
|
if validateErr := utils.ValidateGetPaymentLinkBody(request); validateErr != nil {
|
2023-06-13 15:02:10 +00:00
|
|
|
receiver.logger.Error("failed to validate body on <GetPaymentLink> of <WalletController>", zap.Error(validateErr))
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, validateErr)
|
2023-06-13 15:02:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 15:59:04 +00:00
|
|
|
link, err := receiver.paymentService.GetPaymentLink(ctx.Request().Context(), &models.GetPaymentLinkRequest{
|
|
|
|
Body: request,
|
|
|
|
UserID: userID,
|
|
|
|
ClientIP: ctx.RealIP(),
|
|
|
|
})
|
2023-06-13 15:02:10 +00:00
|
|
|
if err != nil {
|
|
|
|
receiver.logger.Error("failed to get payment link on <GetPaymentLink> of <WalletController>", zap.Error(err))
|
2023-06-15 16:01:32 +00:00
|
|
|
return errors.HTTP(ctx, err)
|
2023-06-13 15:02:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 15:59:04 +00:00
|
|
|
return ctx.JSON(http.StatusOK, &models.GetPaymentLinkResponse{Link: link})
|
2023-06-13 15:02:10 +00:00
|
|
|
}
|