feat: set access token on get tariffs

This commit is contained in:
Kirill 2023-06-29 14:50:48 +00:00
parent 6bc5f3868d
commit 8729a55b22
8 changed files with 240 additions and 50 deletions

@ -35,3 +35,7 @@ JWT_PUBLIC_KEY - публичный ключ для верификации jwt
JWT_ISSUER - издатель токена JWT_ISSUER - издатель токена
JWT_AUDIENCE - аудитория, которая может верифицировать токен JWT_AUDIENCE - аудитория, которая может верифицировать токен
``` ```
## Полезные ссылки:
- [**Диаграммы**](./docs/diagram/README.md)

182
docs/diagram/README.md Normal file

@ -0,0 +1,182 @@
# Диаграммы последовательности и зависимостей
## UseCase оплата корзины (успешно)
```plantuml
participant Frontend
participant CustomerService
participant HubAdminService
participant DiscountService
database CustomerServiceDB
Frontend -> CustomerService : Запрос на оплату
CustomerService -> CustomerServiceDB : Поиск аккаунта по ID пользователья единой авторизации
CustomerService <-- CustomerServiceDB : Найденный аккаунт
CustomerService -> HubAdminService : Получение тарифов из массива id тарифов в корзине
CustomerService <-- HubAdminService : Список тарифов
CustomerService -> CustomerService : Подсчитывается сумма тарифов
CustomerService <-- CustomerService : Сумма тарифов
CustomerService -> DiscountService : Приминение скидок
CustomerService <-- DiscountService : Сформированная цена после применения скидок
CustomerService -> CustomerService : Проверка на наличие средств
CustomerService <-- CustomerService : Средств достаточно
CustomerService -> CustomerServiceDB : Списание средств с кошелька
CustomerService <-- CustomerServiceDB : Обновлённый аккаунт
CustomerService -> CustomerServiceDB : Запись в историю успех оплаты (ошибка игнорируется)
CustomerService <-- CustomerServiceDB : Записанная история
CustomerService -> CustomerServiceDB : Очистка корзины
CustomerService <-- CustomerServiceDB : Обновлённый аккаунт
Frontend <-- CustomerService : Ответ об успешной оплате корзины
```
---
## UseCase оплата корзины (недостаточно средств)
```plantuml
participant Frontend
participant CustomerService
participant HubAdminService
participant DiscountService
database CustomerServiceDB
Frontend -> CustomerService : Запрос на оплату
CustomerService -> CustomerServiceDB : Поиск аккаунта по ID пользователья единой авторизации
CustomerService <-- CustomerServiceDB : Найденный аккаунт
CustomerService -> HubAdminService : Получение тарифов из массива id тарифов в корзине
CustomerService <-- HubAdminService : Список тарифов
CustomerService -> CustomerService : Подсчитывается сумма тарифов
CustomerService <-- CustomerService : Сумма тарифов
CustomerService -> DiscountService : Приминение скидок
CustomerService <-- DiscountService : Сформированная цена после применения скидок
CustomerService -> CustomerService : Проверка на наличие средств
CustomerService <-- CustomerService : Средств не достаточно
Frontend <-- CustomerService : Ответ об ошибке по причине нехватки средств (insufficient funds: 50)
```
---
## UseCase получение ссылки на оплату для пополнения средств в корзине (успешно)
```plantuml
participant Frontend
participant CustomerService
participant PaymentService
database CustomerServiceDB
Frontend -> CustomerService : Запрос на получение ссылки
CustomerService -> CustomerService : Определение способа оплаты
CustomerService <-- CustomerService : Успешно определено
CustomerService -> PaymentService : Запрос на получение платёжной ссылки
CustomerService <-- PaymentService : Платёжная ссылка
Frontend <-- CustomerService : Платёжная ссылка
```
---
## UseCase изменения валюты в кошельке (успешно)
```plantuml
participant Frontend
participant CustomerService
participant CbrfService
database CustomerServiceDB
Frontend -> CustomerService : Запрос на изменения валюты
CustomerService -> CustomerServiceDB : Получение аккаунта по ID пользователья единой авторизации
CustomerService <-- CustomerServiceDB : Найденный аккаунт
CustomerService -> CbrfService : Перевод валюты с одной на другую
CustomerService <-- CbrfService : Результат перевода валюты
CustomerService -> CustomerServiceDB : Обновление кошелька аккаунта
CustomerService <-- CustomerServiceDB : Обновлённый аккаунт
Frontend <-- CustomerService : Обновлённый аккаунт
```
---
## UseCase регистрация аккаунта (успешно)
```plantuml
participant Frontend
participant CustomerService
participant AuthService
database CustomerServiceDB
Frontend -> CustomerService : Запрос на регистрацию аккаунта
CustomerService -> CustomerServiceDB : Поиск аккаунта по ID пользователя единой авторизации
CustomerService <-- CustomerServiceDB : Ошибка: аккаунт не найден
CustomerService -> AuthService : Поиск пользователя единой авторизации по ID
CustomerService <-- AuthService : Найденный пользователь
CustomerService -> CustomerServiceDB : Создание аккаунта с прикрученным \nID пользователя единой авторизации
CustomerService <-- CustomerServiceDB : Созданный аккаунт
Frontend <-- CustomerService : Созданный аккаунт
```
---
## UseCase уведомление об успешной оплате (успешно, id платежей не равны)
```plantuml
participant PaymentService
participant CustomerService
database CustomerServiceDB
PaymentService -> CustomerService : Запрос отправки уведомления об успешной оплате
CustomerService -> CustomerServiceDB : Поиск аккаунта по ID пользователя единой авторизации
CustomerService <-- CustomerServiceDB : Найденный аккаунт
CustomerService -> CustomerService : Сравнение ID платежа с прошлым
CustomerService <-- CustomerService : ID платежей не равны
CustomerService -> CustomerServiceDB : Изменение кошелька аккаунта
CustomerService <-- CustomerServiceDB : Обновлённый аккаунт
CustomerService -> CustomerServiceDB : Создание истории об успешном пополнении средств
CustomerService <-- CustomerServiceDB : Созданная история
PaymentService <-- CustomerService : Успешный ответ
```
---
## UseCase уведомление об успешной оплате (успешно, id платежей равны)
```plantuml
participant PaymentService
participant CustomerService
database CustomerServiceDB
PaymentService -> CustomerService : Запрос отправки уведомления об успешной оплате
CustomerService -> CustomerServiceDB : Поиск аккаунта по ID пользователя единой авторизации
CustomerService <-- CustomerServiceDB : Найденный аккаунт
CustomerService -> CustomerService : Сравнение ID платежа с прошлым
CustomerService <-- CustomerService : ID платежей равны
PaymentService <-- CustomerService : Успешный ответ
```
## Диаграмма зависимостей
```plantuml
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml
Container(CustomerService, "Customer Service", "Golang", "Сервис customer")
Container(AuthService, "Auth Service", "NodeJS", "Микросервис единой авторизации")
Container(DiscountService, "Discount Service", "Golang", "Микросервис скидок")
Container(CbrfService, "CBRF Worker Service", "Golang", "Микросервис по выдаче и перевода курсов валют")
Container(PaymentService, "Payment Service", "Golang", "Платёжный микросервис")
Container(HubadminService, "Hub admin Service", "NodeJS", "Севрис управления тарифами и привелегиями")
ContainerDb(CustomerDB, "Customer Service Database", "MongoDB", "Хранит информацию об аккаунтах")
ContainerDb(AuthServiceDB, "Auth Service Database", "MongoDB", "Хранит информацию о пользователях и сессиях")
ContainerDb(CbrfServiceDB, "CBRF Worker Database", "MongoDB", "Хранит информацию о курсах валют")
ContainerDb(PaymentServiceDB, "Payment Service Database", "MongoDB", "Хранит информацию о платежах и их состоянии")
ContainerDb(HubadminServiceDB, "Hubadmin service Database", "MongoDB", "Хранит информацию о тарифах и привелегиях")
Rel(CustomerService, AuthService, "Использует для получения актуальной информации о пользователе")
Rel(CustomerService, CbrfService, "Использует для перевода валюты с одного курса на другой")
Rel(CustomerService, PaymentService, "Использует для проведения оплаты, получения платёжной ссылки и уведомления об успешной/не_успешной оплате")
Rel(CustomerService, HubadminService, "Использует для получения информации о тарифах")
Rel(CustomerService, DiscountService, "Использует для приминения скидок")
Rel_R(CustomerService, CustomerDB, "Читает/Записывает")
Rel_R(AuthService, AuthServiceDB, "Читает/Записывает")
Rel_R(CbrfService, CbrfServiceDB, "Читает/Записывает")
Rel_R(PaymentService, PaymentServiceDB, "Читает/Записывает")
Rel_R(HubadminService, HubadminServiceDB, "Читает/Записывает")
```

@ -38,18 +38,18 @@ func NewHubadminClient(deps HubadminClientDeps) *HubadminClient {
} }
} }
func (receiver *HubadminClient) GetTariff(ctx context.Context, tariffID string) (*models.Tariff, errors.Error) { func (receiver *HubadminClient) GetTariff(ctx context.Context, accessToken string, tariffID string) (*models.Tariff, errors.Error) {
tariffURL, err := url.JoinPath(receiver.urls.Tariff, tariffID) tariffURL, err := url.JoinPath(receiver.urls.Tariff, tariffID)
if err != nil { if err != nil {
return nil, errors.New( return nil, errors.New(fmt.Errorf("failed to join path on <GetTariff> of <HubadminClient>: %w", err), errors.ErrInternalError)
fmt.Errorf("failed to join path on <GetTariff> of <HubadminClient>: %w", err),
errors.ErrInternalError,
)
} }
response, err := client.Get[models.Tariff, models.FastifyError](ctx, &client.RequestSettings{ response, err := client.Get[models.Tariff, models.FastifyError](ctx, &client.RequestSettings{
URL: tariffURL, URL: tariffURL,
Headers: map[string]string{"Content-Type": "application/json"}, Headers: map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", accessToken),
},
}) })
if err != nil { if err != nil {
receiver.logger.Error("failed to request get tariff on <GetTariff> of <HubadminClient>", receiver.logger.Error("failed to request get tariff on <GetTariff> of <HubadminClient>",
@ -77,11 +77,11 @@ func (receiver *HubadminClient) GetTariff(ctx context.Context, tariffID string)
return response.Body, nil return response.Body, nil
} }
func (receiver *HubadminClient) GetTariffs(ctx context.Context, tarriffIDs []string) ([]models.Tariff, errors.Error) { func (receiver *HubadminClient) GetTariffs(ctx context.Context, accessToken string, tarriffIDs []string) ([]models.Tariff, errors.Error) {
tariffs := make([]models.Tariff, len(tarriffIDs)) tariffs := make([]models.Tariff, len(tarriffIDs))
for index, tariffID := range tarriffIDs { for index, tariffID := range tarriffIDs {
tariff, err := receiver.GetTariff(ctx, tariffID) tariff, err := receiver.GetTariff(ctx, accessToken, tariffID)
if err != nil { if err != nil {
receiver.logger.Error("failed to get tariff on <GetTariffs> of <HubadminClient>", zap.Error(err), zap.String("tariffID", tariffID)) receiver.logger.Error("failed to get tariff on <GetTariffs> of <HubadminClient>", zap.Error(err), zap.String("tariffID", tariffID))
return []models.Tariff{}, err return []models.Tariff{}, err

@ -16,8 +16,8 @@ import (
type cartService interface { type cartService interface {
Remove(ctx context.Context, userID, itemID string) ([]string, errors.Error) Remove(ctx context.Context, userID, itemID string) ([]string, errors.Error)
Add(ctx context.Context, userID, itemID string) ([]string, errors.Error) Add(context.Context, *models.AddItemToCart) ([]string, errors.Error)
Pay(ctx context.Context, userID string) (link string, err errors.Error) Pay(ctx context.Context, token, userID string) (link string, err errors.Error)
} }
type Deps struct { type Deps struct {
@ -49,11 +49,7 @@ func (receiver *Controller) Remove(ctx echo.Context, params swagger.RemoveFromCa
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok { if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Remove> of <CartController>") receiver.logger.Error("failed to convert jwt payload to string on <Remove> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
} }
if validate.IsStringEmpty(params.Id) { if validate.IsStringEmpty(params.Id) {
@ -65,11 +61,7 @@ func (receiver *Controller) Remove(ctx echo.Context, params swagger.RemoveFromCa
cartItems, err := receiver.cartService.Remove(ctx.Request().Context(), userID, params.Id) cartItems, err := receiver.cartService.Remove(ctx.Request().Context(), userID, params.Id)
if err != nil { if err != nil {
receiver.logger.Error( receiver.logger.Error("failed to remove item from cart on <Remove> of <CartController>", zap.Error(err))
"failed to remove item from cart on <Remove> of <CartController>",
zap.Error(err),
)
return errors.HTTP(ctx, err) return errors.HTTP(ctx, err)
} }
@ -80,11 +72,13 @@ func (receiver *Controller) Add(ctx echo.Context, params swagger.Add2cartParams)
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok { if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Add> of <CartController>") receiver.logger.Error("failed to convert jwt payload to string on <Add> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
}
return errors.HTTP(ctx, errors.New( token, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
fmt.Errorf("failed to convert jwt payload to string: %s", userID), if !ok {
errors.ErrInvalidArgs, receiver.logger.Error("failed to convert access token payload to string on <Add> of <CartController>")
)) return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
} }
if validate.IsStringEmpty(params.Id) { if validate.IsStringEmpty(params.Id) {
@ -94,13 +88,13 @@ func (receiver *Controller) Add(ctx echo.Context, params swagger.Add2cartParams)
)) ))
} }
cartItems, err := receiver.cartService.Add(ctx.Request().Context(), userID, params.Id) cartItems, err := receiver.cartService.Add(ctx.Request().Context(), &models.AddItemToCart{
UserID: userID,
TariffID: params.Id,
AccessToken: token,
})
if err != nil { if err != nil {
receiver.logger.Error( receiver.logger.Error("failed to add item to cart on <Add> of <CartController>", zap.Error(err))
"failed to add item to cart on <Add> of <CartController>",
zap.Error(err),
)
return errors.HTTP(ctx, err) return errors.HTTP(ctx, err)
} }
@ -111,17 +105,18 @@ func (receiver *Controller) Pay(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok { if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Pay> of <CartController>") receiver.logger.Error("failed to convert jwt payload to string on <Pay> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
} }
link, err := receiver.cartService.Pay(ctx.Request().Context(), userID) token, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
if !ok {
receiver.logger.Error("failed to convert access token payload to string on <Pay> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
}
link, err := receiver.cartService.Pay(ctx.Request().Context(), token, userID)
if err != nil { if err != nil {
receiver.logger.Error("failed to pay cart on <Pay> of <CartController>", zap.Error(err)) receiver.logger.Error("failed to pay cart on <Pay> of <CartController>", zap.Error(err))
return errors.HTTP(ctx, err) return errors.HTTP(ctx, err)
} }

@ -14,3 +14,4 @@ type User struct {
} }
const AuthJWTDecodedUserIDKey = "userID" const AuthJWTDecodedUserIDKey = "userID"
const AuthJWTDecodedAccessTokenKey = "access-token"

@ -1,5 +1,7 @@
package models package models
type AddItemToCart struct { type AddItemToCart struct {
ID string `json:"id"` UserID string
TariffID string
AccessToken string
} }

@ -23,8 +23,8 @@ type accountRepository interface {
} }
type hubadminClient interface { type hubadminClient interface {
GetTariff(ctx context.Context, tariffID string) (*models.Tariff, errors.Error) GetTariff(ctx context.Context, accessToken, tariffID string) (*models.Tariff, errors.Error)
GetTariffs(ctx context.Context, tarriffIDs []string) ([]models.Tariff, errors.Error) GetTariffs(ctx context.Context, accessToken string, tarriffIDs []string) ([]models.Tariff, errors.Error)
} }
type discountClient interface { type discountClient interface {
@ -103,21 +103,26 @@ func (receiver *Service) Remove(ctx context.Context, userID, itemID string) ([]s
return account.Cart, nil return account.Cart, nil
} }
func (receiver *Service) Add(ctx context.Context, userID, itemID string) ([]string, errors.Error) { func (receiver *Service) Add(ctx context.Context, request *models.AddItemToCart) ([]string, errors.Error) {
tariff, err := receiver.hubadminClient.GetTariff(ctx, itemID) tariff, err := receiver.hubadminClient.GetTariff(ctx, request.AccessToken, request.TariffID)
if err != nil { if err != nil {
receiver.logger.Error("failed to get tariff on <Add> of <CartService>", zap.Error(err), zap.String("tariffID", itemID)) receiver.logger.Error("failed to get tariff on <Add> of <CartService>",
zap.Error(err),
zap.String("tariffID", request.TariffID),
zap.String("accessToken", request.AccessToken),
)
return []string{}, err return []string{}, err
} }
if tariff == nil { if tariff == nil {
return []string{}, errors.New( return []string{}, errors.New(
fmt.Errorf("failed to get tariff <%s> on <Add> of <CartService>: tariff not found", itemID), fmt.Errorf("failed to get tariff <%s> on <Add> of <CartService>: tariff not found", request.TariffID),
errors.ErrNotFound, errors.ErrNotFound,
) )
} }
account, err := receiver.repository.AddItemToCart(ctx, userID, itemID) account, err := receiver.repository.AddItemToCart(ctx, request.UserID, request.TariffID)
if err != nil { if err != nil {
receiver.logger.Error("failed to add item to cart on <Add> of <CartService>", zap.Error(err)) receiver.logger.Error("failed to add item to cart on <Add> of <CartService>", zap.Error(err))
return []string{}, err return []string{}, err
@ -126,14 +131,14 @@ func (receiver *Service) Add(ctx context.Context, userID, itemID string) ([]stri
return account.Cart, nil return account.Cart, nil
} }
func (receiver *Service) Pay(ctx context.Context, userID string) (string, errors.Error) { func (receiver *Service) Pay(ctx context.Context, accessToken string, userID string) (string, errors.Error) {
account, err := receiver.repository.FindByUserID(ctx, userID) account, err := receiver.repository.FindByUserID(ctx, userID)
if err != nil { if err != nil {
receiver.logger.Error("failed to find account on <Pay> of <CartService>", zap.String("userID", userID), zap.Error(err)) receiver.logger.Error("failed to find account on <Pay> of <CartService>", zap.String("userID", userID), zap.Error(err))
return "", err return "", err
} }
tariffs, err := receiver.hubadminClient.GetTariffs(ctx, account.Cart) tariffs, err := receiver.hubadminClient.GetTariffs(ctx, accessToken, account.Cart)
if err != nil { if err != nil {
receiver.logger.Error("failed to get tarrifs on <Pay> of <CartService>", zap.Strings("cart", account.Cart), zap.Error(err)) receiver.logger.Error("failed to get tarrifs on <Pay> of <CartService>", zap.Strings("cart", account.Cart), zap.Error(err))
return "", err return "", err

@ -42,7 +42,7 @@ func authenticate(ctx context.Context, jwtUtil *JWT, input *openapi3filter.Authe
} }
// if the JWS is valid, we have a JWT, which will contain a bunch of claims. // if the JWS is valid, we have a JWT, which will contain a bunch of claims.
token, validateErr := jwtUtil.Validate(jws) userID, validateErr := jwtUtil.Validate(jws)
if validateErr != nil { if validateErr != nil {
return validateErr return validateErr
} }
@ -51,7 +51,8 @@ func authenticate(ctx context.Context, jwtUtil *JWT, input *openapi3filter.Authe
// access the claims data we generate in here. // access the claims data we generate in here.
echoCtx := middleware.GetEchoContext(ctx) echoCtx := middleware.GetEchoContext(ctx)
echoCtx.Set(models.AuthJWTDecodedUserIDKey, token) echoCtx.Set(models.AuthJWTDecodedUserIDKey, userID)
echoCtx.Set(models.AuthJWTDecodedAccessTokenKey, jws)
return nil return nil
} }