Skip to content

Commit

Permalink
feat(organizationinvitation): implement method to list organization i…
Browse files Browse the repository at this point in the history
…nvitations from a given instance (#336)

* feat(organizationinvitation): implement method to list organization invitations from a given instance

* docs
  • Loading branch information
NicolasLopes7 authored Oct 10, 2024
1 parent 9674114 commit f6dae3f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions organizationinvitation/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions organizationinvitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,42 @@ func (c *Client) Revoke(ctx context.Context, params *RevokeParams) (*clerk.Organ
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}

type ListFromInstanceParams struct {
clerk.APIParams
clerk.ListParams
Statuses *[]string `json:"statuses,omitempty"`
Query *string `json:"query,omitempty"`
OrderBy *string `json:"order_by,omitempty"`
}

func (p *ListFromInstanceParams) ToQuery() url.Values {
q := p.ListParams.ToQuery()

if p.Statuses != nil && len(*p.Statuses) > 0 {
q["status"] = *p.Statuses
}

if p.Query != nil {
q.Add("query", *p.Query)
}

if p.OrderBy != nil {
q.Add("order_by", *p.OrderBy)
}

return q
}

// ListAllFromInstance lists all the organization invitations from the current instance
func (c *Client) ListFromInstance(ctx context.Context, params *ListFromInstanceParams) (*clerk.OrganizationInvitationList, error) {
path, err := clerk.JoinPath("/organization_invitations")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
invitation := &clerk.OrganizationInvitationList{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}
57 changes: 57 additions & 0 deletions organizationinvitation/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,60 @@ func TestOrganizationInvitationClientRevoke_Error(t *testing.T) {
require.Equal(t, 1, len(apiErr.Errors))
require.Equal(t, "revoke-error-code", apiErr.Errors[0].Code)
}

func TestOrganizationInvitationClientListFromInstance(t *testing.T) {
t.Parallel()
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{"data":[{"id":"orginv_123","object":"organization_invitation","email_address":"string","role":"string","organization_id":"org_123","status":"string","public_metadata":{},"private_metadata":{},"created_at":0,"updated_at":0}],"total_count":1}`),
Method: http.MethodGet,
Path: "/v1/organization_invitations",
Query: &url.Values{
"limit": []string{"10"},
"order_by": []string{"-created_at"},
"query": []string{"query"},
"status": []string{"pending", "accepted"},
},
},
}
client := NewClient(config)
response, err := client.ListFromInstance(context.Background(), &ListFromInstanceParams{
Statuses: &[]string{"pending", "accepted"},
Query: clerk.String("query"),
OrderBy: clerk.String("-created_at"),
ListParams: clerk.ListParams{
Limit: clerk.Int64(10),
},
})
require.NoError(t, err)
require.Len(t, response.OrganizationInvitations, 1)
require.Equal(t, int64(1), response.TotalCount)
require.Equal(t, "orginv_123", response.OrganizationInvitations[0].ID)
}

func TestOrganizationInvitationClientListFromInstance_Error(t *testing.T) {
t.Parallel()
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Status: http.StatusBadRequest,
Out: json.RawMessage(`{
"errors":[{
"code":"list-from-instance-error-code"
}],
"clerk_trace_id":"list-from-instance-trace-id"
}`),
},
}
client := NewClient(config)
_, err := client.ListFromInstance(context.Background(), &ListFromInstanceParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
require.Equal(t, "list-from-instance-trace-id", apiErr.TraceID)
require.Equal(t, 1, len(apiErr.Errors))
require.Equal(t, "list-from-instance-error-code", apiErr.Errors[0].Code)
}

0 comments on commit f6dae3f

Please sign in to comment.