Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLopes7 committed Sep 18, 2024
1 parent 5fbdbc3 commit 955d1b8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
22 changes: 11 additions & 11 deletions organization_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ type OrganizationDomainVerification struct {

type OrganizationDomain struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
OrganizationID string `json:"organization_id"`
Name string `json:"name"`
EnrollmentMode string `json:"enrollment_mode"`
AffiliationEmailAddress *string `json:"affiliation_email_address"`
Verification *organizationDomainVerificationResponse `json:"verification"`
TotalPendingInvitations int `json:"total_pending_invitations"`
TotalPendingSuggestions int `json:"total_pending_suggestions"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Object string `json:"object"`
ID string `json:"id"`
OrganizationID string `json:"organization_id"`
Name string `json:"name"`
EnrollmentMode string `json:"enrollment_mode"`
AffiliationEmailAddress *string `json:"affiliation_email_address"`
Verification *OrganizationDomainVerification `json:"verification"`
TotalPendingInvitations int `json:"total_pending_invitations"`
TotalPendingSuggestions int `json:"total_pending_suggestions"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type OrganizationDomainList struct {
Expand Down
12 changes: 6 additions & 6 deletions organizationdomain/api.go

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

34 changes: 15 additions & 19 deletions organizationdomain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ func NewClient(config *clerk.ClientConfig) *Client {

type CreateParams struct {
clerk.APIParams
Name string `json:"name" form:"name"`
OrganizationID string `json:"-" form:"-"`
EnrollmentMode string `json:"enrollment_mode" form:"enrollment_mode"`
Verified *bool `json:"verified" form:"verified"`
Name *string `json:"name,omitempty"`
EnrollmentMode *string `json:"enrollment_mode,omitempty"`
Verified *bool `json:"verified,omitempty"`
}

// Create adds a new domain to the organization.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationDomain, error) {
path, err := clerk.JoinPath(path, params.OrganizationID, "/domains")
func (c *Client) Create(ctx context.Context, organizationID string, params *CreateParams) (*clerk.OrganizationDomain, error) {
path, err := clerk.JoinPath(path, organizationID, "/domains")
if err != nil {
return nil, err
}
Expand All @@ -48,15 +47,13 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Organ

type UpdateParams struct {
clerk.APIParams
OrganizationID string `json:"-" form:"-"`
DomainID string `json:"-" form:"-"`
EnrollmentMode *string `json:"enrollment_mode" form:"enrollment_mode"`
Verified *bool `json:"verified" form:"verified"`
EnrollmentMode *string `json:"enrollment_mode,omitempty"`
Verified *bool `json:"verified,omitempty"`
}

// Update updates an organization domain.
func (c *Client) Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationDomain, error) {
path, err := clerk.JoinPath(path, params.OrganizationID, "/domains", params.DomainID)
func (c *Client) Update(ctx context.Context, organizationID, domainID string, params *UpdateParams) (*clerk.OrganizationDomain, error) {
path, err := clerk.JoinPath(path, organizationID, "/domains", domainID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,9 +85,8 @@ func (c *Client) Delete(ctx context.Context, params *DeleteParams) (*clerk.Delet
type ListParams struct {
clerk.APIParams
clerk.ListParams
OrganizationID string `json:"-" form:"-"`
Verified *bool `json:"verified" form:"verified"`
EnrollmentModes []string `json:"enrollment_mode" form:"enrollment_mode"`
Verified *bool `json:"verified,omitempty"`
EnrollmentModes *[]string `json:"enrollment_mode,omitempty"`
}

// ToQuery returns the parameters as url.Values so they can be used
Expand All @@ -102,15 +98,15 @@ func (params *ListParams) ToQuery() url.Values {
q.Set("verified", strconv.FormatBool(*params.Verified))
}

if len(params.EnrollmentModes) > 0 {
q["enrollment_mode"] = params.EnrollmentModes
if params.EnrollmentModes != nil && len(*params.EnrollmentModes) > 0 {
q["enrollment_mode"] = *params.EnrollmentModes
}
return q
}

// List returns a list of organization domains.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.OrganizationDomainList, error) {
path, err := clerk.JoinPath(path, params.OrganizationID, "/domains")
func (c *Client) List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationDomainList, error) {
path, err := clerk.JoinPath(path, organizationID, "/domains")
if err != nil {
return nil, err
}
Expand Down
27 changes: 11 additions & 16 deletions organizationdomain/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ func TestOrganizationDomainClientCreate(t *testing.T) {
},
}
client := NewClient(config)
response, err := client.Create(context.Background(), &CreateParams{
OrganizationID: organizationID,
Name: domain,
EnrollmentMode: "automatic_invitation",
Verified: &verified,
response, err := client.Create(context.Background(), organizationID, &CreateParams{
Name: clerk.String(domain),
EnrollmentMode: clerk.String("automatic_invitation"),
Verified: clerk.Bool(verified),
})
require.NoError(t, err)
require.Equal(t, id, response.ID)
Expand All @@ -61,7 +60,7 @@ func TestOrganizationDomainClientCreate_Error(t *testing.T) {
},
}
client := NewClient(config)
_, err := client.Create(context.Background(), &CreateParams{})
_, err := client.Create(context.Background(), "org_123", &CreateParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
Expand All @@ -79,18 +78,15 @@ func TestOrganizationDomainClientUpdate(t *testing.T) {
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"verified": %s, "enrollment_mode": null}`, strconv.FormatBool(verified))),
In: json.RawMessage(fmt.Sprintf(`{"verified": %s}`, strconv.FormatBool(verified))),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","verification":{"status": "verified"}}`, id)),
Method: http.MethodPatch,
Path: "/v1/organizations/" + organizationID + "/domains/" + id,
},
}
client := NewClient(config)
domain, err := client.Update(context.Background(), &UpdateParams{
OrganizationID: organizationID,
DomainID: id,
Verified: &verified,
EnrollmentMode: nil,
domain, err := client.Update(context.Background(), organizationID, id, &UpdateParams{
Verified: clerk.Bool(verified),
})
require.NoError(t, err)
require.Equal(t, id, domain.ID)
Expand All @@ -113,7 +109,7 @@ func TestOrganizationDomainClientUpdate_Error(t *testing.T) {
},
}
client := NewClient(config)
_, err := client.Update(context.Background(), &UpdateParams{})
_, err := client.Update(context.Background(), "org_123", "orgdm_123", &UpdateParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
Expand Down Expand Up @@ -174,13 +170,12 @@ func TestOrganizationDomainClientList(t *testing.T) {
}
client := NewClient(config)
params := &ListParams{
OrganizationID: organizationID,
Verified: &verified,
EnrollmentModes: []string{"automatic_invitation"},
EnrollmentModes: &[]string{"automatic_invitation"},
}
params.Limit = clerk.Int64(1)
params.Offset = clerk.Int64(2)
list, err := client.List(context.Background(), params)
list, err := client.List(context.Background(), organizationID, params)
require.NoError(t, err)
require.Equal(t, id, list.OrganizationDomains[0].ID)
require.Equal(t, organizationID, list.OrganizationDomains[0].OrganizationID)
Expand Down

0 comments on commit 955d1b8

Please sign in to comment.