diff --git a/.gitignore b/.gitignore index 52d12d0..d2d054a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ .idea/ .vscode .env diff --git a/internal/dto/get_histories.go b/internal/dto/get_histories.go deleted file mode 100644 index fb95446..0000000 --- a/internal/dto/get_histories.go +++ /dev/null @@ -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 -} diff --git a/internal/interface/controller/grpc/customer/customer.go b/internal/interface/controller/grpc/customer/customer.go index ffa75a1..0227608 100644 --- a/internal/interface/controller/grpc/customer/customer.go +++ b/internal/interface/controller/grpc/customer/customer.go @@ -9,20 +9,17 @@ import ( "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/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 { Logger *zap.Logger - HistoryService historyService + HistoryService *history.Service } type Controller struct { logger *zap.Logger - historyService historyService + historyService *history.Service } func New(deps Deps) *Controller { diff --git a/internal/interface/controller/grpc/payment/payment.go b/internal/interface/controller/grpc/payment/payment.go index 3c22940..35d75f6 100644 --- a/internal/interface/controller/grpc/payment/payment.go +++ b/internal/interface/controller/grpc/payment/payment.go @@ -9,21 +9,17 @@ import ( "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/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 { Logger *zap.Logger - PaymentCallbackService paymentCallbackService + PaymentCallbackService *callback.PaymentCallbackService } type Controller struct { logger *zap.Logger - paymentCallbackService paymentCallbackService + paymentCallbackService *callback.PaymentCallbackService } func New(deps Deps) *Controller { diff --git a/internal/interface/controller/rest/account/account.go b/internal/interface/controller/rest/account/account.go index 9492b7f..f484474 100644 --- a/internal/interface/controller/rest/account/account.go +++ b/internal/interface/controller/rest/account/account.go @@ -1,7 +1,6 @@ package account import ( - "context" "fmt" "log" "net/http" @@ -11,29 +10,19 @@ import ( "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/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/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 { Logger *zap.Logger - Service accountService + Service *account.Service } type Controller struct { logger *zap.Logger - service accountService + service *account.Service } func New(deps Deps) *Controller { diff --git a/internal/interface/controller/rest/cart/cart.go b/internal/interface/controller/rest/cart/cart.go index aef0225..a049653 100644 --- a/internal/interface/controller/rest/cart/cart.go +++ b/internal/interface/controller/rest/cart/cart.go @@ -1,7 +1,6 @@ package cart import ( - "context" "fmt" "log" "net/http" @@ -11,23 +10,18 @@ import ( "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/models" + "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/cart" "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 { Logger *zap.Logger - CartService cartService + CartService *cart.Service } type Controller struct { logger *zap.Logger - cartService cartService + cartService *cart.Service } func New(deps Deps) *Controller { diff --git a/internal/interface/controller/rest/currency/currency.go b/internal/interface/controller/rest/currency/currency.go index 110abb6..a5993c4 100644 --- a/internal/interface/controller/rest/currency/currency.go +++ b/internal/interface/controller/rest/currency/currency.go @@ -1,7 +1,6 @@ package currency import ( - "context" "fmt" "log" "net/http" @@ -9,22 +8,18 @@ import ( "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/service/currency" "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 { Logger *zap.Logger - CurrencyService currencyService + CurrencyService *currency.Service } type Controller struct { logger *zap.Logger - currencyService currencyService + currencyService *currency.Service } func New(deps Deps) *Controller { diff --git a/internal/interface/controller/rest/history/history.go b/internal/interface/controller/rest/history/history.go index d8ef7d6..1c44365 100644 --- a/internal/interface/controller/rest/history/history.go +++ b/internal/interface/controller/rest/history/history.go @@ -1,32 +1,26 @@ package history import ( - "context" "fmt" "log" "net/http" "github.com/labstack/echo/v4" "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/interface/swagger" "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 { Logger *zap.Logger - HistoryService historyService + HistoryService *history.Service } type Controller struct { logger *zap.Logger - historyService historyService + historyService *history.Service } 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, UserID: userID, Pagination: &models.Pagination{ diff --git a/internal/interface/controller/rest/wallet/wallet.go b/internal/interface/controller/rest/wallet/wallet.go index e2d5841..c883440 100644 --- a/internal/interface/controller/rest/wallet/wallet.go +++ b/internal/interface/controller/rest/wallet/wallet.go @@ -1,7 +1,6 @@ package wallet import ( - "context" "fmt" "log" "net/http" @@ -10,30 +9,23 @@ import ( "go.uber.org/zap" "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/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/pkg/echotools" "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 { Logger *zap.Logger - WalletService walletService - PaymentService paymentService + WalletService *wallet.Service + PaymentService *payment.Service } type Controller struct { logger *zap.Logger - walletService walletService - paymentService paymentService + walletService *wallet.Service + paymentService *payment.Service } func New(deps Deps) *Controller { diff --git a/internal/interface/repository/history.go b/internal/interface/repository/history.go index 6800851..fe433fd 100644 --- a/internal/interface/repository/history.go +++ b/internal/interface/repository/history.go @@ -10,10 +10,10 @@ import ( "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "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/fields" "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" ) @@ -62,7 +62,7 @@ func (receiver *HistoryRepository) Insert(ctx context.Context, history *models.H 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.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 } -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()) if err != nil { receiver.logger.Error("failed to count all documents on of ", @@ -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{ {Key: "$sort", Value: bson.D{ {Key: "createdAt", Value: -1}, @@ -130,7 +142,7 @@ func (receiver *HistoryRepository) GetRecentTariffs(ctx context.Context, userID {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 { receiver.logger.Error("failed to get recent tariffs on of ", zap.String("userId", userID), diff --git a/internal/service/history/history.go b/internal/service/history/history.go index 465894c..75cada3 100644 --- a/internal/service/history/history.go +++ b/internal/service/history/history.go @@ -6,15 +6,31 @@ import ( "log" "math" + "go.mongodb.org/mongo-driver/bson" "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/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, + } + + return query +} + type historyRepository interface { - CountAll(context.Context, *dto.GetHistories) (int64, errors.Error) - FindMany(context.Context, *dto.GetHistories) ([]models.History, errors.Error) + CountAll(context.Context, *GetHistories) (int64, errors.Error) + FindMany(context.Context, *GetHistories) ([]models.History, errors.Error) Insert(context.Context, *models.History) (*models.History, errors.Error) 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 { return nil, errors.New( fmt.Errorf("pagination is nil on of : %w", errors.ErrInternalError),