From 955d1b8740846156b9aaad7f920bce81592c503a Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Wed, 18 Sep 2024 11:47:41 -0300 Subject: [PATCH] code review --- organization_domain.go | 22 ++++++++++---------- organizationdomain/api.go | 12 +++++------ organizationdomain/client.go | 34 ++++++++++++++----------------- organizationdomain/client_test.go | 27 ++++++++++-------------- 4 files changed, 43 insertions(+), 52 deletions(-) diff --git a/organization_domain.go b/organization_domain.go index 2cd2d07..2cd6f6e 100644 --- a/organization_domain.go +++ b/organization_domain.go @@ -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 { diff --git a/organizationdomain/api.go b/organizationdomain/api.go index 095d1c6..b0ab04e 100644 --- a/organizationdomain/api.go +++ b/organizationdomain/api.go @@ -9,13 +9,13 @@ import ( ) // Create adds a new domain to the organization. -func Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationDomain, error) { - return getClient().Create(ctx, params) +func Create(ctx context.Context, organizationID string, params *CreateParams) (*clerk.OrganizationDomain, error) { + return getClient().Create(ctx, organizationID, params) } // Update updates an organization domain. -func Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationDomain, error) { - return getClient().Update(ctx, params) +func Update(ctx context.Context, organizationID, domainID string, params *UpdateParams) (*clerk.OrganizationDomain, error) { + return getClient().Update(ctx, organizationID, domainID, params) } // Delete removes a domain from an organization. @@ -24,8 +24,8 @@ func Delete(ctx context.Context, params *DeleteParams) (*clerk.DeletedResource, } // List returns a list of organization domains. -func List(ctx context.Context, params *ListParams) (*clerk.OrganizationDomainList, error) { - return getClient().List(ctx, params) +func List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationDomainList, error) { + return getClient().List(ctx, organizationID, params) } func getClient() *Client { diff --git a/organizationdomain/client.go b/organizationdomain/client.go index c314df0..514f9cc 100644 --- a/organizationdomain/client.go +++ b/organizationdomain/client.go @@ -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 } @@ -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 } @@ -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 @@ -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 } diff --git a/organizationdomain/client_test.go b/organizationdomain/client_test.go index fdc3fbe..45c65da 100644 --- a/organizationdomain/client_test.go +++ b/organizationdomain/client_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)