package account 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/interface/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "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 } type Controller struct { logger *zap.Logger service accountService } func New(deps Deps) *Controller { if deps.Logger == nil { log.Panicln("logger is nil on ") } if deps.Service == nil { log.Panicln("account service is nil on ") } return &Controller{ logger: deps.Logger, service: deps.Service, } } func (receiver *Controller) GetAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.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 := receiver.service.GetAccountByUserID(ctx.Request().Context(), userID) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) GetDirectAccount(ctx echo.Context, userID string) error { account, err := receiver.service.GetAccountByUserID(ctx.Request().Context(), userID) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) GetAccounts(ctx echo.Context, params swagger.PaginationAccountsParams) error { response, err := receiver.service.GetAccountsList( ctx.Request().Context(), utils.DeterminePagination(params.Page, params.Limit), ) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, response) } func (receiver *Controller) CreateAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.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 := receiver.service.CreateAccountByUserID(ctx.Request().Context(), userID) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) RemoveAccount(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.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 := receiver.service.RemoveAccount(ctx.Request().Context(), userID) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) RemoveDirectAccount(ctx echo.Context, userID string) error { account, err := receiver.service.RemoveAccount(ctx.Request().Context(), userID) if err != nil { return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) SetVerificationStatus(ctx echo.Context, userID string) error { request, bindErr := echotools.Bind[models.SetAccountStatus](ctx) if bindErr != nil { receiver.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 := receiver.service.SetVerificationStatus(ctx.Request().Context(), userID, request.Status) if err != nil { receiver.logger.Error("failed set status on of ", zap.Error(err)) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) } func (receiver *Controller) UpdateAccountName(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.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 { receiver.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 := receiver.service.UpdateAccountName(ctx.Request().Context(), userID, request) if err != nil { receiver.logger.Error("failed to update account name on of ", zap.Error(err)) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, account) }