diff --git a/app/controlplane/pkg/biz/apitoken.go b/app/controlplane/pkg/biz/apitoken.go index 2a7d8cb8a..5b00a1cd1 100644 --- a/app/controlplane/pkg/biz/apitoken.go +++ b/app/controlplane/pkg/biz/apitoken.go @@ -64,6 +64,7 @@ type APITokenRepo interface { UpdateLastUsedAt(ctx context.Context, ID uuid.UUID, lastUsedAt time.Time) error FindByID(ctx context.Context, ID uuid.UUID) (*APIToken, error) FindByIDInOrg(ctx context.Context, orgID uuid.UUID, id uuid.UUID) (*APIToken, error) + FindByNameInOrg(ctx context.Context, orgID uuid.UUID, name string) (*APIToken, error) } type APITokenUseCase struct { @@ -402,6 +403,15 @@ func (uc *APITokenUseCase) FindByID(ctx context.Context, id string) (*APIToken, return t, nil } +func (uc *APITokenUseCase) FindByNameInOrg(ctx context.Context, orgID, name string) (*APIToken, error) { + orgUUID, err := uuid.Parse(orgID) + if err != nil { + return nil, NewErrInvalidUUID(err) + } + + return uc.apiTokenRepo.FindByNameInOrg(ctx, orgUUID, name) +} + func NewAPITokenSyncerUseCase(tokenUC *APITokenUseCase) *APITokenSyncerUseCase { return &APITokenSyncerUseCase{ base: tokenUC, diff --git a/app/controlplane/pkg/biz/mocks/APITokenRepo.go b/app/controlplane/pkg/biz/mocks/APITokenRepo.go index 9803d4411..4d64bfa15 100644 --- a/app/controlplane/pkg/biz/mocks/APITokenRepo.go +++ b/app/controlplane/pkg/biz/mocks/APITokenRepo.go @@ -109,6 +109,36 @@ func (_m *APITokenRepo) FindByIDInOrg(ctx context.Context, orgID uuid.UUID, id u return r0, r1 } +// FindByNameInOrg provides a mock function with given fields: ctx, orgID, name +func (_m *APITokenRepo) FindByNameInOrg(ctx context.Context, orgID uuid.UUID, name string) (*biz.APIToken, error) { + ret := _m.Called(ctx, orgID, name) + + if len(ret) == 0 { + panic("no return value specified for FindByNameInOrg") + } + + var r0 *biz.APIToken + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) (*biz.APIToken, error)); ok { + return rf(ctx, orgID, name) + } + if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) *biz.APIToken); ok { + r0 = rf(ctx, orgID, name) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*biz.APIToken) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, uuid.UUID, string) error); ok { + r1 = rf(ctx, orgID, name) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // List provides a mock function with given fields: ctx, orgID, filters func (_m *APITokenRepo) List(ctx context.Context, orgID *uuid.UUID, filters *biz.APITokenListFilters) ([]*biz.APIToken, error) { ret := _m.Called(ctx, orgID, filters) diff --git a/app/controlplane/pkg/data/apitoken.go b/app/controlplane/pkg/data/apitoken.go index 57f060fa3..a53c2ac89 100644 --- a/app/controlplane/pkg/data/apitoken.go +++ b/app/controlplane/pkg/data/apitoken.go @@ -84,6 +84,19 @@ func (r *APITokenRepo) FindByIDInOrg(ctx context.Context, orgID uuid.UUID, id uu return entAPITokenToBiz(token), nil } +func (r *APITokenRepo) FindByNameInOrg(ctx context.Context, orgID uuid.UUID, name string) (*biz.APIToken, error) { + token, err := r.data.DB.APIToken.Query().Where(apitoken.Name(name), apitoken.HasOrganizationWith(organization.ID(orgID)), apitoken.RevokedAtIsNil()).WithProject().Only(ctx) + if err != nil { + if ent.IsNotFound(err) { + return nil, biz.NewErrNotFound("API token") + } + + return nil, err + } + + return entAPITokenToBiz(token), nil +} + func (r *APITokenRepo) List(ctx context.Context, orgID *uuid.UUID, filters *biz.APITokenListFilters) ([]*biz.APIToken, error) { query := r.data.DB.APIToken.Query().WithProject().WithOrganization()