Skip to content

Commit d36ba51

Browse files
authored
fix(apitokens): add find by name (#2253)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent fba2fa2 commit d36ba51

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

app/controlplane/pkg/biz/apitoken.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type APITokenRepo interface {
6464
UpdateLastUsedAt(ctx context.Context, ID uuid.UUID, lastUsedAt time.Time) error
6565
FindByID(ctx context.Context, ID uuid.UUID) (*APIToken, error)
6666
FindByIDInOrg(ctx context.Context, orgID uuid.UUID, id uuid.UUID) (*APIToken, error)
67+
FindByNameInOrg(ctx context.Context, orgID uuid.UUID, name string) (*APIToken, error)
6768
}
6869

6970
type APITokenUseCase struct {
@@ -402,6 +403,15 @@ func (uc *APITokenUseCase) FindByID(ctx context.Context, id string) (*APIToken,
402403
return t, nil
403404
}
404405

406+
func (uc *APITokenUseCase) FindByNameInOrg(ctx context.Context, orgID, name string) (*APIToken, error) {
407+
orgUUID, err := uuid.Parse(orgID)
408+
if err != nil {
409+
return nil, NewErrInvalidUUID(err)
410+
}
411+
412+
return uc.apiTokenRepo.FindByNameInOrg(ctx, orgUUID, name)
413+
}
414+
405415
func NewAPITokenSyncerUseCase(tokenUC *APITokenUseCase) *APITokenSyncerUseCase {
406416
return &APITokenSyncerUseCase{
407417
base: tokenUC,

app/controlplane/pkg/biz/mocks/APITokenRepo.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/pkg/data/apitoken.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ func (r *APITokenRepo) FindByIDInOrg(ctx context.Context, orgID uuid.UUID, id uu
8484
return entAPITokenToBiz(token), nil
8585
}
8686

87+
func (r *APITokenRepo) FindByNameInOrg(ctx context.Context, orgID uuid.UUID, name string) (*biz.APIToken, error) {
88+
token, err := r.data.DB.APIToken.Query().Where(apitoken.Name(name), apitoken.HasOrganizationWith(organization.ID(orgID)), apitoken.RevokedAtIsNil()).WithProject().Only(ctx)
89+
if err != nil {
90+
if ent.IsNotFound(err) {
91+
return nil, biz.NewErrNotFound("API token")
92+
}
93+
94+
return nil, err
95+
}
96+
97+
return entAPITokenToBiz(token), nil
98+
}
99+
87100
func (r *APITokenRepo) List(ctx context.Context, orgID *uuid.UUID, filters *biz.APITokenListFilters) ([]*biz.APIToken, error) {
88101
query := r.data.DB.APIToken.Query().WithProject().WithOrganization()
89102

0 commit comments

Comments
 (0)