Skip to content

Commit 7221e41

Browse files
committed
handle edge cases
1 parent 4496bf2 commit 7221e41

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

pkg/account/api/api_key.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,10 @@ func (s *AccountService) UpdateAPIKeyLastUsedAt(
800800
apiKey := &domain.APIKey{
801801
APIKey: envAPIKey.ApiKey,
802802
}
803-
apiKey.UsedAt(req.LastUsedAt)
803+
err = apiKey.SetUsedAt(req.LastUsedAt)
804+
if err != nil {
805+
return err
806+
}
804807
return s.accountStorage.UpdateAPIKey(contextWithTx, apiKey, envAPIKey.Environment.Id)
805808
})
806809
if err != nil {
@@ -840,5 +843,16 @@ func validateUpdateAPIKeyLastUsedAtRequest(
840843
}
841844
return dt.Err()
842845
}
846+
847+
if req.LastUsedAt <= 0 {
848+
dt, err := statusInvalidLastUsedAt.WithDetails(&errdetails.LocalizedMessage{
849+
Locale: localizer.GetLocale(),
850+
Message: localizer.MustLocalizeWithTemplate(locale.InvalidArgumentError, "last_used_at"),
851+
})
852+
if err != nil {
853+
return statusInternal.Err()
854+
}
855+
return dt.Err()
856+
}
843857
return nil
844858
}

pkg/account/api/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ var (
4848
statusSearchFilterIDIsEmpty = api.NewGRPCStatus(pkgErr.NewErrorInvalidArgEmpty(pkgErr.AccountPackageName, "search filter ID is empty", "search_filter_ID"))
4949
statusSearchFilterIDNotFound = api.NewGRPCStatus(pkgErr.NewErrorNotFound(pkgErr.AccountPackageName, "search filter not found", "search_filter"))
5050
statusInvalidListAPIKeyRequest = api.NewGRPCStatus(pkgErr.NewErrorInvalidArgEmpty(pkgErr.AccountPackageName, "invalid list api key request", "list_api_key_request"))
51+
statusInvalidLastUsedAt = api.NewGRPCStatus(pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.AccountPackageName, "last used at is invalid", "last_used_at"))
5152
)

pkg/account/domain/api_key.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ import (
2222
wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
2323
"github.com/jinzhu/copier"
2424

25+
pkgErr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
2526
proto "github.com/bucketeer-io/bucketeer/v2/proto/account"
2627
)
2728

2829
const keyBytes = 32
2930

31+
var (
32+
ErrLastUsedAtNotUpdated = pkgErr.NewErrorFailedPrecondition(pkgErr.AccountPackageName, "last used at not updated")
33+
ErrInvalidLastUsedAt = pkgErr.NewErrorInvalidArgNotMatchFormat(pkgErr.AccountPackageName, "invalid last used at", "last_used_at")
34+
)
35+
3036
type APIKey struct {
3137
*proto.APIKey
3238
}
@@ -116,8 +122,13 @@ func (a *APIKey) Update(
116122
return updated, nil
117123
}
118124

119-
func (a *APIKey) UsedAt(lastUsedAt int64) {
120-
if a.LastUsedAt < lastUsedAt {
121-
a.LastUsedAt = lastUsedAt
125+
func (a *APIKey) SetUsedAt(lastUsedAt int64) error {
126+
if lastUsedAt <= 0 {
127+
return ErrInvalidLastUsedAt
122128
}
129+
if a.LastUsedAt >= lastUsedAt {
130+
return ErrLastUsedAtNotUpdated
131+
}
132+
a.LastUsedAt = lastUsedAt
133+
return nil
123134
}

0 commit comments

Comments
 (0)