Merge branch 'dev' into 'staging'

Dev

See merge request pena-services/customer!27
This commit is contained in:
Mikhail 2023-11-26 20:31:07 +00:00
commit b56e3293ea
11 changed files with 62 additions and 100 deletions

2
.gitignore vendored

@ -1,5 +1,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ vendor/
.idea/ .idea/
.vscode .vscode
.env .env

@ -1,23 +0,0 @@
package dto
import (
"go.mongodb.org/mongo-driver/bson"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)
type GetHistories struct {
Pagination *models.Pagination
Type *string
UserID string
}
func (receiver *GetHistories) BSON() bson.M {
query := bson.M{
fields.History.IsDeleted: false,
fields.History.Type: *receiver.Type,
fields.History.UserID: receiver.UserID,
}
return query
}

@ -9,20 +9,17 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/customer" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/customer"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
) )
type historyService interface {
CreateHistory(ctx context.Context, history *models.History) (*models.History, errors.Error)
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
HistoryService historyService HistoryService *history.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
historyService historyService historyService *history.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -9,21 +9,17 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/payment_callback" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/payment_callback"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/callback"
) )
type paymentCallbackService interface {
SuccessEvent(context.Context, *models.PaymentEvent) errors.Error
FailureEvent(context.Context, *models.PaymentEvent) errors.Error
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
PaymentCallbackService paymentCallbackService PaymentCallbackService *callback.PaymentCallbackService
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
paymentCallbackService paymentCallbackService paymentCallbackService *callback.PaymentCallbackService
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -1,7 +1,6 @@
package account package account
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -11,29 +10,19 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/account"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools" "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools"
) )
type accountService interface {
GetAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error)
GetAccountsList(ctx context.Context, pagination *models.Pagination) (*models.PaginationResponse[models.Account], errors.Error)
CreateAccount(ctx context.Context, account *models.Account) (*models.Account, errors.Error)
CreateAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error)
RemoveAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
DeleteAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
SetVerificationStatus(ctx context.Context, userID string, status models.AccountStatus) (*models.Account, errors.Error)
UpdateAccountName(ctx context.Context, userID string, name *models.Name) (*models.Account, errors.Error)
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
Service accountService Service *account.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
service accountService service *account.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -1,7 +1,6 @@
package cart package cart
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -11,23 +10,18 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/cart"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate" "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
) )
type cartService interface {
Remove(ctx context.Context, userID, itemID string) (*models.Account, errors.Error)
Add(context.Context, *models.AddItemToCart) (*models.Account, errors.Error)
Pay(ctx context.Context, token, userID string) (*models.Account, errors.Error)
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
CartService cartService CartService *cart.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
cartService cartService cartService *cart.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -1,7 +1,6 @@
package currency package currency
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -9,22 +8,18 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"go.uber.org/zap" "go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/currency"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools" "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools"
) )
type currencyService interface {
GetCurrencies(context.Context) ([]string, errors.Error)
PutCurrencies(context.Context, []string) ([]string, errors.Error)
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
CurrencyService currencyService CurrencyService *currency.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
currencyService currencyService currencyService *currency.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -1,32 +1,26 @@
package history package history
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"go.uber.org/zap" "go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
) )
type historyService interface {
GetHistoryList(context.Context, *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error)
GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
HistoryService historyService HistoryService *history.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
historyService historyService historyService *history.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {
@ -55,7 +49,7 @@ func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetH
)) ))
} }
histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &dto.GetHistories{ histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &history.GetHistories{
Type: params.Type, Type: params.Type,
UserID: userID, UserID: userID,
Pagination: &models.Pagination{ Pagination: &models.Pagination{

@ -1,7 +1,6 @@
package wallet package wallet
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -10,30 +9,23 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/payment"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/wallet"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools" "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate" "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)
}
type paymentService interface {
GetPaymentLink(context.Context, *models.GetPaymentLinkRequest) (string, errors.Error)
}
type Deps struct { type Deps struct {
Logger *zap.Logger Logger *zap.Logger
WalletService walletService WalletService *wallet.Service
PaymentService paymentService PaymentService *payment.Service
} }
type Controller struct { type Controller struct {
logger *zap.Logger logger *zap.Logger
walletService walletService walletService *wallet.Service
paymentService paymentService paymentService *payment.Service
} }
func New(deps Deps) *Controller { func New(deps Deps) *Controller {

@ -10,10 +10,10 @@ import (
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap" "go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
mongoWrapper "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/mongo" mongoWrapper "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/mongo"
) )
@ -62,7 +62,7 @@ func (receiver *HistoryRepository) Insert(ctx context.Context, history *models.H
return history, nil return history, nil
} }
func (receiver *HistoryRepository) FindMany(ctx context.Context, dto *dto.GetHistories) ([]models.History, errors.Error) { func (receiver *HistoryRepository) FindMany(ctx context.Context, dto *history.GetHistories) ([]models.History, errors.Error) {
findOptions := options.Find() findOptions := options.Find()
findOptions.SetSkip((dto.Pagination.Page - 1) * dto.Pagination.Limit) findOptions.SetSkip((dto.Pagination.Page - 1) * dto.Pagination.Limit)
@ -93,7 +93,7 @@ func (receiver *HistoryRepository) FindMany(ctx context.Context, dto *dto.GetHis
return histories, nil return histories, nil
} }
func (receiver *HistoryRepository) CountAll(ctx context.Context, dto *dto.GetHistories) (int64, errors.Error) { func (receiver *HistoryRepository) CountAll(ctx context.Context, dto *history.GetHistories) (int64, errors.Error) {
count, err := receiver.mongoDB.CountDocuments(ctx, dto.BSON()) count, err := receiver.mongoDB.CountDocuments(ctx, dto.BSON())
if err != nil { if err != nil {
receiver.logger.Error("failed to count all documents on <CountAll> of <HistoryRepository>", receiver.logger.Error("failed to count all documents on <CountAll> of <HistoryRepository>",
@ -120,6 +120,18 @@ func (receiver *HistoryRepository) GetRecentTariffs(ctx context.Context, userID
}}, }},
} }
unwindStage := bson.D{
{Key: "$unwind", Value: bson.D{
{Key: "path", Value: "$rawDetails.tariffs"},
}},
}
groupStage := bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: "$rawDetails.tariffs.id"},
}},
}
sortStage := bson.D{ sortStage := bson.D{
{Key: "$sort", Value: bson.D{ {Key: "$sort", Value: bson.D{
{Key: "createdAt", Value: -1}, {Key: "createdAt", Value: -1},
@ -130,7 +142,7 @@ func (receiver *HistoryRepository) GetRecentTariffs(ctx context.Context, userID
{Key: "$limit", Value: 100}, {Key: "$limit", Value: 100},
} }
cursor, err := receiver.mongoDB.Aggregate(ctx, mongo.Pipeline{matchStage, sortStage, limitStage}) cursor, err := receiver.mongoDB.Aggregate(ctx, mongo.Pipeline{matchStage, unwindStage, sortStage, groupStage, limitStage})
if err != nil { if err != nil {
receiver.logger.Error("failed to get recent tariffs on <GetRecentTariffs> of <HistoryRepository>", receiver.logger.Error("failed to get recent tariffs on <GetRecentTariffs> of <HistoryRepository>",
zap.String("userId", userID), zap.String("userId", userID),

@ -6,15 +6,31 @@ import (
"log" "log"
"math" "math"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap" "go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
) )
type GetHistories struct {
Pagination *models.Pagination
Type *string
UserID string
}
func (receiver *GetHistories) BSON() bson.M {
query := bson.M{
fields.History.IsDeleted: false,
fields.History.Type: *receiver.Type,
}
return query
}
type historyRepository interface { type historyRepository interface {
CountAll(context.Context, *dto.GetHistories) (int64, errors.Error) CountAll(context.Context, *GetHistories) (int64, errors.Error)
FindMany(context.Context, *dto.GetHistories) ([]models.History, errors.Error) FindMany(context.Context, *GetHistories) ([]models.History, errors.Error)
Insert(context.Context, *models.History) (*models.History, errors.Error) Insert(context.Context, *models.History) (*models.History, errors.Error)
GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new
} }
@ -44,7 +60,7 @@ func New(deps Deps) *Service {
} }
} }
func (receiver *Service) GetHistoryList(ctx context.Context, dto *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error) { func (receiver *Service) GetHistoryList(ctx context.Context, dto *GetHistories) (*models.PaginationResponse[models.History], errors.Error) {
if dto == nil { if dto == nil {
return nil, errors.New( return nil, errors.New(
fmt.Errorf("pagination is nil on <GetHistoryList> of <HistoryService>: %w", errors.ErrInternalError), fmt.Errorf("pagination is nil on <GetHistoryList> of <HistoryService>: %w", errors.ErrInternalError),