package swagger import ( "fmt" "net/http" "github.com/labstack/echo/v4" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/zap" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/client" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/repository" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools" ) // TODO update echo errors const defaultCurrency = "RUB" // TODO move type API2 struct { logger *zap.Logger history repository.HistoryRepository account repository.AccountRepository currency repository.CurrencyRepository auth *client.AuthClient } var _ ServerInterface = (*API2)(nil) func NewAPI2(logger *zap.Logger, db *mongo.Database, config *models.Config) API2 { return API2{ logger: logger, history: repository.NewHistoryRepository2(logger, db.Collection("histories")), currency: repository.NewCurrencyRepository2(logger, db.Collection("currency_lists")), account: repository.NewAccountRepository2(logger, db.Collection("accounts")), auth: client.NewAuthClient(client.AuthClientDeps{Logger: logger, URLs: &config.Service.AuthMicroservice.URL}), } } // Health func (api *API2) GetHealth(ctx echo.Context) error { return ctx.String(http.StatusOK, "OK") } // Account func (api *API2) DeleteAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { api.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs, )) } account, err := api.account.Remove(ctx.Request().Context(), userID) if err != nil { api.logger.Error("failed to remove account on of ", zap.Error(err), zap.String("userID", userID), ) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (api *API2) ChangeAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { api.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs)) } request, bindErr := echotools.Bind[models.Name](ctx) if bindErr != nil { api.logger.Error("failed to bind json request on of ", zap.Error(bindErr)) return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to bind json: %w", bindErr), errors.ErrInternalError)) } account, err := api.account.UpdateName(ctx.Request().Context(), userID, request) if err != nil { api.logger.Error("failed to update account name on of ", zap.Error(err)) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (api *API2) SetAccountVerificationStatus(ctx echo.Context, userID string) error { request, bindErr := echotools.Bind[models.SetAccountStatus](ctx) if bindErr != nil { api.logger.Error("failed to bind json request on of ", zap.Error(bindErr)) return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to bind json: %w", bindErr), errors.ErrInternalError)) } account, err := api.account.SetStatus(ctx.Request().Context(), userID, request.Status) if err != nil { api.logger.Error("failed to set status on of ", zap.Error(err)) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (api *API2) GetAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { api.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs, )) } account, err := api.account.FindByUserID(ctx.Request().Context(), userID) if err != nil { api.logger.Error("failed to get account by id on of ", zap.Error(err), zap.String("userID", userID), ) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (api *API2) AddAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { api.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs, )) } account, err := api.account.FindByUserID(ctx.Request().Context(), userID) if err != nil && err.Type() != errors.ErrNotFound { api.logger.Error("failed to find account on of ", zap.Error(err), ) return errors.HTTP(ctx, err) } if account != nil { return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to create account with <%s> on of : %w", userID, errors.ErrConflict, ), errors.ErrConflict, )) } user, err := api.auth.GetUser(ctx.Request().Context(), userID) if err != nil { api.logger.Error("failed to get user on of ", zap.Error(err), zap.String("userID", userID), ) return errors.HTTP(ctx, err) } account, err = api.account.Insert(ctx.Request().Context(), &models.Account{UserID: user.ID, Wallet: models.Wallet{Currency: defaultCurrency}}) if err != nil { api.logger.Error("failed to create account on of ", zap.Error(err), zap.String("userID", userID), ) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (api *API2) DeleteDirectAccount(ctx echo.Context, userID string) error { // return receiver.accountController.RemoveDirectAccount(ctx, userID) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) GetDirectAccount(ctx echo.Context, userID string) error { // return receiver.accountController.GetDirectAccount(ctx, userID) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsParams) error { // return receiver.accountController.GetAccounts(ctx, params) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } // Cart func (api *API2) RemoveFromCart(ctx echo.Context, params RemoveFromCartParams) error { // return receiver.cartController.Remove(ctx, params) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) Add2cart(ctx echo.Context, params Add2cartParams) error { // return receiver.cartController.Add(ctx, params) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) PayCart(ctx echo.Context) error { // return receiver.cartController.Pay(ctx) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } // Currency func (api *API2) GetCurrencies(ctx echo.Context) error { // return receiver.currencyController.GetCurrencies(ctx) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) UpdateCurrencies(ctx echo.Context) error { // return receiver.currencyController.PutCurrencies(ctx) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } // History func (api *API2) GetHistory(ctx echo.Context, params GetHistoryParams) error { // return receiver.historyController.GetHistoryList(ctx, params) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } // Wallet func (api *API2) RequestMoney(ctx echo.Context) error { // return receiver.walletController.GetPaymentLink(ctx) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) } func (api *API2) ChangeCurrency(ctx echo.Context) error { // return receiver.walletController.ChangeCurrency(ctx) return ctx.JSON(http.StatusBadRequest, models.ResponseErrorHTTP{Message: "TODO", StatusCode: 888}) }