Skip to content

Commit

Permalink
feat: filter get user account apps by user ID
Browse files Browse the repository at this point in the history
  • Loading branch information
commoddity committed Sep 21, 2023
1 parent 798ebb9 commit 6a2cd18
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 111 deletions.
60 changes: 46 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type (

// GetAllAccounts returns all Accounts - GET `/v2/account`
GetAllAccounts(ctx context.Context, options ...AccountOptions) ([]*types.Account, error)
// GetAccountByID returns a single Account by its account ID - GET `/v2/account/{id}`
GetAccountByID(ctx context.Context, accountID types.AccountID) (*types.Account, error)
// GetAccountsByUser returns all accounts for a given user ID - GET `/v2/user/{userID}/account`
GetAccountsByUser(ctx context.Context, userID types.UserID, options ...AccountOptions) ([]*types.Account, error)
// GetUserAccounts returns all Accounts for a given user ID - GET `/v2/user/{userID}/account`
GetUserAccounts(ctx context.Context, userID types.UserID, options ...AccountOptions) ([]*types.Account, error)
// GetUserAccount returns a single user Account by its account ID and user ID - GET `/v2/user/{userID}/account/{id}`
GetUserAccount(ctx context.Context, accountID types.AccountID, userID types.UserID, options ...AccountOptions) (*types.Account, error)

// GetPortalUser returns the Portal User for a given user ID, either provider ID or portal ID - GET `/v2/user/{userID}?full_details=true`
// The userID is a plain string because you can provide the method with either a provider user ID or a portal user ID
Expand Down Expand Up @@ -499,27 +499,59 @@ func (db *DBClient) GetAllAccounts(ctx context.Context, optionParams ...AccountO
return getReq[[]*types.Account](endpoint, db.getAuthHeaderForRead(), db.httpClient)
}

// GetAccountByID returns a single Account by its account ID - GET `/v2/account/{id}`
func (db *DBClient) GetAccountByID(ctx context.Context, accountID types.AccountID) (*types.Account, error) {
if accountID == "" {
return nil, errNoAccountID
// GetUserAccounts returns all Accounts for a given user ID - GET `/v2/user/{userID}/account`
func (db *DBClient) GetUserAccounts(ctx context.Context, userID types.UserID, optionParams ...AccountOptions) ([]*types.Account, error) {
if userID == "" {
return nil, errNoUserID
}
if len(optionParams) > 1 {
return nil, errMoreThanOneOption
}

endpoint := fmt.Sprintf("%s/%s", db.v2BasePath(accountPath), accountID)
endpoint := fmt.Sprintf("%s/%s/%s", db.v2BasePath(userPath), userID, accountPath)

return getReq[*types.Account](endpoint, db.getAuthHeaderForRead(), db.httpClient)
options := AccountOptions{}
if len(optionParams) > 0 {
options = optionParams[0]
}

queryParams := make([]string, 0)

if len(options.RoleNameFilters) > 0 {
roleNameStrs := make([]string, len(options.RoleNameFilters))
for i, roleName := range options.RoleNameFilters {
if !roleName.IsValid() {
return nil, fmt.Errorf("%w: %s", errInvalidRoleName, roleName)
}
roleNameStrs[i] = string(roleName)
}
queryParams = append(queryParams, fmt.Sprintf("%s=%s", AccountParams.RoleNameFilters, strings.Join(roleNameStrs, ",")))
}

if options.Accepted != nil {
queryParams = append(queryParams, fmt.Sprintf("%s=%t", AccountParams.Accepted, *options.Accepted))
}

if len(queryParams) > 0 {
endpoint = fmt.Sprintf("%s?%s", endpoint, strings.Join(queryParams, "&"))
}

return getReq[[]*types.Account](endpoint, db.getAuthHeaderForRead(), db.httpClient)
}

// GetAccountsByUser returns all accounts for a given user ID - GET `/v2/user/{userID}/account`
func (db *DBClient) GetAccountsByUser(ctx context.Context, userID types.UserID, optionParams ...AccountOptions) ([]*types.Account, error) {
// GetUserAccount returns a single user Account by its account ID and user ID - GET `/v2/user/{userID}/account/{id}`
func (db *DBClient) GetUserAccount(ctx context.Context, accountID types.AccountID, userID types.UserID, optionParams ...AccountOptions) (*types.Account, error) {
if accountID == "" {
return nil, errNoAccountID
}
if userID == "" {
return nil, errNoUserID
}
if len(optionParams) > 1 {
return nil, errMoreThanOneOption
}

endpoint := fmt.Sprintf("%s/%s/%s", db.v2BasePath(userPath), userID, accountPath)
endpoint := fmt.Sprintf("%s/%s/%s/%s", db.v2BasePath(userPath), userID, accountPath, accountID)

options := AccountOptions{}
if len(optionParams) > 0 {
Expand Down Expand Up @@ -547,7 +579,7 @@ func (db *DBClient) GetAccountsByUser(ctx context.Context, userID types.UserID,
endpoint = fmt.Sprintf("%s?%s", endpoint, strings.Join(queryParams, "&"))
}

return getReq[[]*types.Account](endpoint, db.getAuthHeaderForRead(), db.httpClient)
return getReq[*types.Account](endpoint, db.getAuthHeaderForRead(), db.httpClient)
}

/* -- User Read Methods -- */
Expand Down
Loading

0 comments on commit 6a2cd18

Please sign in to comment.