Skip to content

Commit

Permalink
chore(dynamodb): refactor multiple apikey metadb calls into a single one
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Jan 8, 2025
1 parent 1e00a5a commit 26747d7
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/meta/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1730,23 +1730,29 @@ func (dwr DynamoDB) GetUserAPIKeys(ctx context.Context) ([]mTypes.APIKeyDetails,
return nil, fmt.Errorf("failed to get userData for identity %s %w", userid, err)
}

changed := false

for hashedKey, apiKeyDetails := range userData.APIKeys {
// if expiresAt is not nil value
if !apiKeyDetails.ExpirationDate.Equal(time.Time{}) && time.Now().After(apiKeyDetails.ExpirationDate) {
apiKeyDetails.IsExpired = true

changed = true
}

userData.APIKeys[hashedKey] = apiKeyDetails

err = dwr.SetUserData(ctx, userData)
if err != nil {
return nil, err
}

apiKeys = append(apiKeys, apiKeyDetails)
}

return apiKeys, nil
if !changed {
// return early, no need to make a call to update key expiry in the DB
return apiKeys, nil
}

err = dwr.SetUserData(ctx, userData)

return apiKeys, err
}

func (dwr DynamoDB) AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDetails *mTypes.APIKeyDetails) error {
Expand Down

0 comments on commit 26747d7

Please sign in to comment.