Skip to content

Commit

Permalink
chore: add tags field to account api proto file (#1455)
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Yuichi Okimoto <[email protected]>
  • Loading branch information
cre8ivejp authored Jan 17, 2025
1 parent c0172bf commit 42d1a7c
Show file tree
Hide file tree
Showing 56 changed files with 5,528 additions and 3,654 deletions.
2 changes: 2 additions & 0 deletions api-description/apidocs.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ paths:
- ENVIRONMENT_COUNT
- LAST_SEEN
- STATE
- TAGS
default: DEFAULT
- name: orderDirection
in: query
Expand Down Expand Up @@ -1563,6 +1564,7 @@ definitions:
- ENVIRONMENT_COUNT
- LAST_SEEN
- STATE
- TAGS
default: DEFAULT
accountListAccountsV2RequestOrderDirection:
type: string
Expand Down
31 changes: 31 additions & 0 deletions api-description/web-api.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ paths:
- ENVIRONMENT_COUNT
- LAST_SEEN
- STATE
- TAGS
default: DEFAULT
- name: orderDirection
in: query
Expand Down Expand Up @@ -925,6 +926,13 @@ paths:
required: false
type: integer
format: int32
- name: tags
in: query
required: false
type: array
items:
type: string
collectionFormat: multi
tags:
- Account
/v1/account/list_api_keys:
Expand Down Expand Up @@ -4439,6 +4447,7 @@ definitions:
enum:
- UNKNOWN
- FEATURE_FLAG
- ACCOUNT
default: UNKNOWN
UpdateAccountV2RequestAccountV2Avatar:
type: object
Expand Down Expand Up @@ -4632,6 +4641,13 @@ definitions:
properties:
role:
$ref: '#/definitions/AccountV2RoleOrganization'
accountChangeAccountV2TagsCommand:
type: object
properties:
tags:
type: array
items:
type: string
accountChangeDefaultSearchFilterCommand:
type: object
properties:
Expand Down Expand Up @@ -4750,6 +4766,10 @@ definitions:
type: string
language:
type: string
tags:
type: array
items:
type: string
accountCreateAccountV2Request:
type: object
properties:
Expand All @@ -4776,6 +4796,10 @@ definitions:
type: string
language:
type: string
tags:
type: array
items:
type: string
accountCreateAccountV2Response:
type: object
properties:
Expand Down Expand Up @@ -4989,6 +5013,7 @@ definitions:
- ENVIRONMENT_COUNT
- LAST_SEEN
- STATE
- TAGS
default: DEFAULT
accountListAccountsV2RequestOrderDirection:
type: string
Expand Down Expand Up @@ -5092,6 +5117,12 @@ definitions:
$ref: '#/definitions/UpdateAccountV2RequestAccountV2Avatar'
disabled:
type: boolean
tags:
type: array
items:
type: string
changeTagsCommand:
$ref: '#/definitions/accountChangeAccountV2TagsCommand'
accountUpdateAccountV2Response:
type: object
properties:
Expand Down
2 changes: 1 addition & 1 deletion manifests/bucketeer/charts/api/values.yaml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions manifests/bucketeer/charts/web/values.yaml

Large diffs are not rendered by default.

127 changes: 126 additions & 1 deletion pkg/account/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/jinzhu/copier"
"go.uber.org/zap"
Expand All @@ -33,8 +34,10 @@ import (
"github.com/bucketeer-io/bucketeer/pkg/log"
"github.com/bucketeer-io/bucketeer/pkg/storage"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
tagdomain "github.com/bucketeer-io/bucketeer/pkg/tag/domain"
accountproto "github.com/bucketeer-io/bucketeer/proto/account"
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
tagproto "github.com/bucketeer-io/bucketeer/proto/tag"
)

func (s *AccountService) CreateAccountV2(
Expand Down Expand Up @@ -71,6 +74,7 @@ func (s *AccountService) CreateAccountV2(
req.Command.LastName,
req.Command.Language,
req.Command.AvatarImageUrl,
req.Command.Tags,
req.OrganizationId,
req.Command.OrganizationRole,
req.Command.EnvironmentRoles,
Expand Down Expand Up @@ -121,6 +125,9 @@ func (s *AccountService) CreateAccountV2(
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("organizationID", req.OrganizationId),
zap.Any("environmentRoles", req.Command.EnvironmentRoles),
zap.String("email", req.Command.Email),
zap.Strings("tags", req.Command.Tags),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Expand All @@ -132,6 +139,22 @@ func (s *AccountService) CreateAccountV2(
}
return nil, dt.Err()
}
// Upsert tags
for _, envRole := range req.Command.EnvironmentRoles {
if err := s.upsertTags(ctx, req.Command.Tags, envRole.EnvironmentId); err != nil {
s.logger.Error(
"Failed to upsert account tags",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("organizationId", req.OrganizationId),
zap.String("environmentId", envRole.EnvironmentId),
zap.String("email", req.Command.Email),
zap.Strings("tags", req.Command.Tags),
)...,
)
return nil, statusInternal.Err()
}
}
return &accountproto.CreateAccountV2Response{Account: account.AccountV2}, nil
}

Expand Down Expand Up @@ -159,6 +182,7 @@ func (s *AccountService) createAccountV2NoCommand(
req.LastName,
req.Language,
req.AvatarImageUrl,
req.Tags,
req.OrganizationId,
req.OrganizationRole,
req.EnvironmentRoles,
Expand Down Expand Up @@ -242,7 +266,22 @@ func (s *AccountService) createAccountV2NoCommand(
)
return nil, err
}

// Upsert tags
for _, envRole := range req.EnvironmentRoles {
if err := s.upsertTags(ctx, req.Tags, envRole.EnvironmentId); err != nil {
s.logger.Error(
"Failed to upsert account tags",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("organizationId", req.OrganizationId),
zap.String("environmentId", envRole.EnvironmentId),
zap.String("email", req.Email),
zap.Strings("tags", req.Tags),
)...,
)
return nil, statusInternal.Err()
}
}
return &accountproto.CreateAccountV2Response{Account: account.AccountV2}, nil
}

Expand Down Expand Up @@ -295,6 +334,35 @@ func (s *AccountService) changeExistedAccountV2EnvironmentRoles(
return nil
}

func (s *AccountService) upsertTags(
ctx context.Context,
tags []string,
environmentID string,
) error {
for _, tag := range tags {
trimed := strings.TrimSpace(tag)
if trimed == "" {
continue
}
t, err := tagdomain.NewTag(trimed, environmentID, tagproto.Tag_ACCOUNT)
if err != nil {
s.logger.Error(
"Failed to create domain tag",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("environmentId", environmentID),
zap.String("tagId", tag),
)...,
)
return err
}
if err := s.tagStorage.UpsertTag(ctx, t); err != nil {
return err
}
}
return nil
}

func (s *AccountService) UpdateAccountV2(
ctx context.Context,
req *accountproto.UpdateAccountV2Request,
Expand Down Expand Up @@ -390,6 +458,24 @@ func (s *AccountService) UpdateAccountV2(
}
return nil, dt.Err()
}
// Upsert tags
if req.ChangeTagsCommand != nil {
for _, envRole := range updatedAccountPb.EnvironmentRoles {
if err := s.upsertTags(ctx, req.ChangeTagsCommand.Tags, envRole.EnvironmentId); err != nil {
s.logger.Error(
"Failed to upsert account tags",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("organizationId", req.OrganizationId),
zap.String("environmentId", envRole.EnvironmentId),
zap.String("email", updatedAccountPb.Email),
zap.Strings("tags", req.ChangeTagsCommand.Tags),
)...,
)
return nil, statusInternal.Err()
}
}
}
return &accountproto.UpdateAccountV2Response{
Account: updatedAccountPb,
}, nil
Expand Down Expand Up @@ -438,6 +524,7 @@ func (s *AccountService) updateAccountV2NoCommand(
req.Language,
req.AvatarImageUrl,
req.Avatar,
req.Tags,
req.OrganizationRole,
req.EnvironmentRoles,
req.Disabled,
Expand Down Expand Up @@ -470,6 +557,24 @@ func (s *AccountService) updateAccountV2NoCommand(
}
return nil, dt.Err()
}
// Upsert tags
if req.Tags != nil {
for _, envRole := range updatedAccountPb.EnvironmentRoles {
if err := s.upsertTags(ctx, req.Tags, envRole.EnvironmentId); err != nil {
s.logger.Error(
"Failed to upsert account tags",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
zap.String("organizationId", req.OrganizationId),
zap.String("environmentId", envRole.EnvironmentId),
zap.String("email", req.Email),
zap.Strings("tags", req.Tags),
)...,
)
return nil, statusInternal.Err()
}
}
}
return &accountproto.UpdateAccountV2Response{
Account: updatedAccountPb,
}, nil
Expand All @@ -482,6 +587,7 @@ func isNoUpdateAccountV2Command(req *accountproto.UpdateAccountV2Request) bool {
req.ChangeLanguageCommand == nil &&
req.ChangeAvatarUrlCommand == nil &&
req.ChangeAvatarCommand == nil &&
req.ChangeTagsCommand == nil &&
req.ChangeOrganizationRoleCommand == nil &&
req.ChangeEnvironmentRolesCommand == nil &&
req.ChangeLastSeenCommand == nil
Expand All @@ -507,6 +613,9 @@ func (s *AccountService) getUpdateAccountV2Commands(req *accountproto.UpdateAcco
if req.ChangeAvatarCommand != nil {
commands = append(commands, req.ChangeAvatarCommand)
}
if req.ChangeTagsCommand != nil {
commands = append(commands, req.ChangeTagsCommand)
}
if req.ChangeOrganizationRoleCommand != nil {
commands = append(commands, req.ChangeOrganizationRoleCommand)
}
Expand Down Expand Up @@ -555,6 +664,7 @@ func (s *AccountService) EnableAccountV2(
nil,
nil,
nil,
[]string{},
nil,
nil,
wrapperspb.Bool(false),
Expand Down Expand Up @@ -628,6 +738,7 @@ func (s *AccountService) DisableAccountV2(
nil,
nil,
nil,
[]string{},
nil,
nil,
wrapperspb.Bool(true),
Expand Down Expand Up @@ -699,6 +810,7 @@ func (s *AccountService) updateAccountV2NoCommandMysql(
email, organizationID string,
name, firstName, lastName, language, avatarImageURL *wrapperspb.StringValue,
avatar *accountproto.UpdateAccountV2Request_AccountV2Avatar,
tags []string,
organizationRole *accountproto.UpdateAccountV2Request_OrganizationRoleValue,
environmentRoles []*accountproto.AccountV2_EnvironmentRole,
isDisabled *wrapperspb.BoolValue,
Expand All @@ -717,6 +829,7 @@ func (s *AccountService) updateAccountV2NoCommandMysql(
language,
avatarImageURL,
avatar,
tags,
organizationRole,
environmentRoles,
isDisabled,
Expand Down Expand Up @@ -1002,6 +1115,16 @@ func (s *AccountService) ListAccountsV2(
if req.Disabled != nil {
whereParts = append(whereParts, mysql.NewFilter("disabled", "=", req.Disabled.Value))
}
tagValues := make([]interface{}, 0, len(req.Tags))
for _, tag := range req.Tags {
tagValues = append(tagValues, tag)
}
if len(tagValues) > 0 {
whereParts = append(
whereParts,
mysql.NewJSONFilter("tags", mysql.JSONContainsString, tagValues),
)
}
if req.OrganizationRole != nil {
whereParts = append(whereParts, mysql.NewFilter("organization_role", "=", req.OrganizationRole.Value))
}
Expand Down Expand Up @@ -1098,6 +1221,8 @@ func (s *AccountService) newAccountV2ListOrders(
column = "last_seen"
case accountproto.ListAccountsV2Request_STATE:
column = "disabled"
case accountproto.ListAccountsV2Request_TAGS:
column = "tags"
default:
dt, err := statusInvalidOrderBy.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Expand Down
4 changes: 3 additions & 1 deletion pkg/account/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"

v2 "github.com/bucketeer-io/bucketeer/pkg/account/storage/v2"

environmentclient "github.com/bucketeer-io/bucketeer/pkg/environment/client"
"github.com/bucketeer-io/bucketeer/pkg/locale"
"github.com/bucketeer-io/bucketeer/pkg/log"
"github.com/bucketeer-io/bucketeer/pkg/pubsub/publisher"
"github.com/bucketeer-io/bucketeer/pkg/role"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
tagstorage "github.com/bucketeer-io/bucketeer/pkg/tag/storage"
proto "github.com/bucketeer-io/bucketeer/proto/account"
environmentproto "github.com/bucketeer-io/bucketeer/proto/environment"
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
Expand Down Expand Up @@ -60,6 +60,7 @@ func WithLogger(logger *zap.Logger) Option {
type AccountService struct {
environmentClient environmentclient.Client
accountStorage v2.AccountStorage
tagStorage tagstorage.TagStorage
publisher publisher.Publisher
opts *options
logger *zap.Logger
Expand All @@ -78,6 +79,7 @@ func NewAccountService(
return &AccountService{
environmentClient: e,
accountStorage: v2.NewAccountStorage(mysqlClient),
tagStorage: tagstorage.NewTagStorage(mysqlClient),
publisher: publisher,
opts: &options,
logger: options.logger.Named("api"),
Expand Down
Loading

0 comments on commit 42d1a7c

Please sign in to comment.