From 41dd61841215277c76130c5e4de17283cfa4f3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 13 May 2025 13:22:37 +0300 Subject: [PATCH] MSP-5372 Implement /application/get_application_by_token --- controller/application.go | 32 ++++++++++++++++++++++++++ docs/swagger.yaml | 48 +++++++++++++++++++++++++++++++++++++++ domain/application.go | 9 ++++++++ routes/routes.go | 5 ++++ service/application.go | 19 ++++++++++++++++ service/interfaces.go | 1 + tests/application_test.go | 36 +++++++++++++++++++++++++++++ 7 files changed, 150 insertions(+) diff --git a/controller/application.go b/controller/application.go index aad3bb8..5915302 100644 --- a/controller/application.go +++ b/controller/application.go @@ -11,8 +11,10 @@ import ( "google.golang.org/grpc/codes" ) +// nolint:interfacebloat type ApplicationService interface { GetById(ctx context.Context, appId int) (*domain.ApplicationWithTokens, error) + GetByToken(ctx context.Context, token string) (*domain.GetApplicationByTokenResponse, error) GetByIdList(ctx context.Context, idList []int) ([]*domain.ApplicationWithTokens, error) GetByServiceId(ctx context.Context, id int) ([]*domain.ApplicationWithTokens, error) SystemTree(ctx context.Context, systemId int) ([]*domain.DomainWithService, error) @@ -64,6 +66,36 @@ func (c Application) GetById(ctx context.Context, req domain.Identity) (*domain. } } +// GetByToken godoc +// +// @Tags application +// @Summary Получить приложение по токену +// @Description Возвращает идентификатор приложения по токену +// @Accept json +// @Produce json +// @Param body body domain.GetApplicationByTokenRequest true "Идентификатор приложения" +// @Success 200 {object} domain.GetApplicationByTokenResponse +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error +// @Router /application/get_application_by_token [POST] +func (c Application) GetByToken(ctx context.Context, req domain.GetApplicationByTokenRequest) (*domain.GetApplicationByTokenResponse, error) { + result, err := c.service.GetByToken(ctx, req.Token) + switch { + case errors.Is(err, domain.ErrApplicationNotFound): + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with token '%s' not found", req.Token), + err, + ) + case err != nil: + return nil, err + default: + return result, nil + } +} + // GetByIdList godoc // // @Tags application diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 59d2f99..bce5245 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -230,6 +230,20 @@ definitions: $ref: '#/definitions/domain.ServiceWithApps' type: array type: object + domain.GetApplicationByTokenRequest: + properties: + token: + type: string + required: + - token + type: object + domain.GetApplicationByTokenResponse: + properties: + applicationGroupId: + type: integer + applicationId: + type: integer + type: object domain.IdListRequest: properties: idList: @@ -629,6 +643,40 @@ paths: summary: Получить приложение по идентификатору tags: - application + /application/get_application_by_token: + post: + consumes: + - application/json + description: Возвращает идентификатор приложения по токену + parameters: + - description: Идентификатор приложения + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.GetApplicationByTokenRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.GetApplicationByTokenResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/apierrors.Error' + "404": + description: Not Found + schema: + $ref: '#/definitions/apierrors.Error' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/apierrors.Error' + summary: Получить приложение по токену + tags: + - application /application/get_applications: post: consumes: diff --git a/domain/application.go b/domain/application.go index 7296013..9ced13a 100644 --- a/domain/application.go +++ b/domain/application.go @@ -54,3 +54,12 @@ type UpdateApplicationRequest struct { Name string `validate:"required"` Description string } + +type GetApplicationByTokenRequest struct { + Token string `validate:"required"` +} + +type GetApplicationByTokenResponse struct { + ApplicationId int + ApplicationGroupId int +} diff --git a/routes/routes.go b/routes/routes.go index 79f6d36..4115fd2 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -154,6 +154,11 @@ func applicationCluster(c Controllers) []cluster.EndpointDescriptor { Inner: true, Handler: c.Application.GetById, }, + { + Path: "system/application/get_application_by_token", + Inner: true, + Handler: c.Application.GetByToken, + }, { Path: "system/application/delete_applications", Inner: true, diff --git a/service/application.go b/service/application.go index 19987a0..ac437d4 100644 --- a/service/application.go +++ b/service/application.go @@ -55,6 +55,25 @@ func (s Application) GetById(ctx context.Context, appId int) (*domain.Applicatio return arr[0], nil } +func (s Application) GetByToken(ctx context.Context, tokenStr string) (*domain.GetApplicationByTokenResponse, error) { + token, err := s.tokenRepo.GetTokenById(ctx, tokenStr) + if err != nil { + return nil, errors.WithMessage(err, "get token by id") + } + if token == nil { + return nil, domain.ErrApplicationNotFound + } + + app, err := s.appRepo.GetApplicationById(ctx, token.AppId) + if err != nil { + return nil, errors.WithMessage(err, "get app by id") + } + return &domain.GetApplicationByTokenResponse{ + ApplicationId: app.Id, + ApplicationGroupId: app.ApplicationGroupId, + }, nil +} + func (s Application) GetByIdList(ctx context.Context, idList []int) ([]*domain.ApplicationWithTokens, error) { res, err := s.appRepo.GetApplicationByIdList(ctx, idList) if err != nil { diff --git a/service/interfaces.go b/service/interfaces.go index ee8a5a6..528c352 100644 --- a/service/interfaces.go +++ b/service/interfaces.go @@ -7,6 +7,7 @@ import ( type TokenRepo interface { GetTokenByAppIdList(ctx context.Context, appIdList []int) ([]entity.Token, error) + GetTokenById(ctx context.Context, token string) (*entity.Token, error) } type DomainRepo interface { diff --git a/tests/application_test.go b/tests/application_test.go index 15cc1f7..7e9d9a0 100644 --- a/tests/application_test.go +++ b/tests/application_test.go @@ -31,6 +31,7 @@ type ApplicationSuite struct { domainRepo repository.Domain appRepo repository.Application appGroupRepo repository.AppGroup + tokenRepo repository.Token api *client.Client } @@ -45,6 +46,7 @@ func (s *ApplicationSuite) SetupTest() { s.domainRepo = repository.NewDomain(s.testDb) s.appRepo = repository.NewApplication(s.testDb) s.appGroupRepo = repository.NewAppGroup(s.testDb) + s.tokenRepo = repository.NewToken(s.testDb) } func (s *ApplicationSuite) TestGetAllApplications() { @@ -243,6 +245,40 @@ func (s *ApplicationSuite) TestUpdate_AppNotFound() { s.Require().Equal(domain.ErrCodeApplicationNotFound, apiError.ErrorCode) } +func (s *ApplicationSuite) TestGetByToken() { + toInsert := fake.It[[]domain.Application](fake.MaxSliceSize(2), fake.MinSliceSize(2)) + insertedApps := s.insertApps(toInsert) + token := fake.It[string]() + expectedApp := domain.GetApplicationByTokenResponse{ + ApplicationId: insertedApps[0].Id, + ApplicationGroupId: insertedApps[0].ServiceId, + } + _, err := s.tokenRepo.SaveToken(s.T().Context(), token, expectedApp.ApplicationId, fake.It[int]()) + s.Require().NoError(err) + + result := domain.GetApplicationByTokenResponse{} + err = s.api.Invoke("system/application/get_application_by_token"). + JsonRequestBody(domain.GetApplicationByTokenRequest{Token: token}). + JsonResponseBody(&result). + Do(s.T().Context()) + s.Require().NoError(err) + s.Require().Equal(expectedApp, result) +} + +func (s *ApplicationSuite) TestGetByToken_NotFound() { + toInsert := fake.It[[]domain.Application](fake.MaxSliceSize(2), fake.MinSliceSize(2)) + s.insertApps(toInsert) + + result := domain.GetApplicationByTokenResponse{} + err := s.api.Invoke("system/application/get_application_by_token"). + JsonRequestBody(domain.GetApplicationByTokenRequest{Token: fake.It[string]()}). + JsonResponseBody(&result). + Do(s.T().Context()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationNotFound, apiError.ErrorCode) +} + func (s *ApplicationSuite) createAppGroup() *entity.AppGroup { createdDomain, err := s.domainRepo.CreateDomain( s.T().Context(),