From 5899e22e8392ffed4fefdd6c03c616498f59d1ea Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Tue, 17 Sep 2024 17:24:34 -0300 Subject: [PATCH 1/5] feat: implement organization domains CRUD --- organization_domain.go | 29 +++++ organizationdomain/api.go | 35 ++++++ organizationdomain/client.go | 122 +++++++++++++++++++ organizationdomain/client_test.go | 187 ++++++++++++++++++++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 organization_domain.go create mode 100644 organizationdomain/api.go create mode 100644 organizationdomain/client.go create mode 100644 organizationdomain/client_test.go diff --git a/organization_domain.go b/organization_domain.go new file mode 100644 index 0000000..b8498cf --- /dev/null +++ b/organization_domain.go @@ -0,0 +1,29 @@ +package clerk + +type organizationDomainVerificationResponse struct { + Status string `json:"status"` + Strategy string `json:"strategy"` + Attempts *int `json:"attempts"` + ExpireAt *int64 `json:"expire_at"` +} + +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 +} + +type OrganizationDomainList struct { + APIResource + OrganizationDomains []*OrganizationDomain `json:"data"` + TotalCount int64 `json:"total_count"` +} diff --git a/organizationdomain/api.go b/organizationdomain/api.go new file mode 100644 index 0000000..095d1c6 --- /dev/null +++ b/organizationdomain/api.go @@ -0,0 +1,35 @@ +// Code generated by "gen"; DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. +package organizationdomain + +import ( + "context" + + "github.com/clerk/clerk-sdk-go/v2" +) + +// Create adds a new domain to the organization. +func Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationDomain, error) { + return getClient().Create(ctx, params) +} + +// Update updates an organization domain. +func Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationDomain, error) { + return getClient().Update(ctx, params) +} + +// Delete removes a domain from an organization. +func Delete(ctx context.Context, params *DeleteParams) (*clerk.DeletedResource, error) { + return getClient().Delete(ctx, params) +} + +// List returns a list of organization domains. +func List(ctx context.Context, params *ListParams) (*clerk.OrganizationDomainList, error) { + return getClient().List(ctx, params) +} + +func getClient() *Client { + return &Client{ + Backend: clerk.GetBackend(), + } +} diff --git a/organizationdomain/client.go b/organizationdomain/client.go new file mode 100644 index 0000000..bf2057c --- /dev/null +++ b/organizationdomain/client.go @@ -0,0 +1,122 @@ +// Package organizationdomain provides the Organization Domains API. +package organizationdomain + +import ( + "context" + "net/http" + "net/url" + "strconv" + + "github.com/clerk/clerk-sdk-go/v2" +) + +//go:generate go run ../cmd/gen/main.go + +const path = "/organizations" + +// Client is used to invoke the Organization Domains API. +type Client struct { + Backend clerk.Backend +} + +func NewClient(config *clerk.ClientConfig) *Client { + return &Client{ + Backend: clerk.NewBackend(&config.BackendConfig), + } +} + +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"` +} + +// 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") + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodPost, path) + req.SetParams(params) + domain := &clerk.OrganizationDomain{} + err = c.Backend.Call(ctx, req, domain) + return domain, err +} + +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"` +} + +// 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) + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodPatch, path) + req.SetParams(params) + domain := &clerk.OrganizationDomain{} + err = c.Backend.Call(ctx, req, domain) + return domain, err +} + +type DeleteParams struct { + clerk.APIParams + OrganizationID string `json:"-"` + DomainID string `json:"-"` +} + +// Delete removes a domain from an organization. +func (c *Client) Delete(ctx context.Context, params *DeleteParams) (*clerk.DeletedResource, error) { + path, err := clerk.JoinPath(path, params.OrganizationID, "/domains", params.DomainID) + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodDelete, path) + res := &clerk.DeletedResource{} + err = c.Backend.Call(ctx, req, res) + return res, err +} + +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"` +} + +// ToQuery returns the parameters as url.Values so they can be used +// in a URL query string. +func (params *ListParams) ToQuery() url.Values { + q := params.ListParams.ToQuery() + + if params.Verified != nil { + q.Set("verified", strconv.FormatBool(*params.Verified)) + } + + 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") + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodGet, path) + req.SetParams(params) + domains := &clerk.OrganizationDomainList{} + err = c.Backend.Call(ctx, req, domains) + return domains, err +} diff --git a/organizationdomain/client_test.go b/organizationdomain/client_test.go new file mode 100644 index 0000000..fdc3fbe --- /dev/null +++ b/organizationdomain/client_test.go @@ -0,0 +1,187 @@ +package organizationdomain + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strconv" + "testing" + + "github.com/clerk/clerk-sdk-go/v2" + "github.com/clerk/clerk-sdk-go/v2/clerktest" + "github.com/stretchr/testify/require" +) + +func TestOrganizationDomainClientCreate(t *testing.T) { + t.Parallel() + id := "orgdm_123" + organizationID := "org_123" + domain := "mydomain.com" + verified := false + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + In: json.RawMessage(fmt.Sprintf(`{"name": "%s", "enrollment_mode": "automatic_invitation", "verified": %s}`, domain, strconv.FormatBool(verified))), + Out: json.RawMessage(fmt.Sprintf(`{"enrollment_mode":"automatic_invitation","id":"%s","name":"%s","object":"organization_domain","organization_id":"%s","verification":{"status":"unverified"}}`, + id, domain, organizationID)), + Method: http.MethodPost, + Path: "/v1/organizations/" + organizationID + "/domains", + }, + } + client := NewClient(config) + response, err := client.Create(context.Background(), &CreateParams{ + OrganizationID: organizationID, + Name: domain, + EnrollmentMode: "automatic_invitation", + Verified: &verified, + }) + require.NoError(t, err) + require.Equal(t, id, response.ID) + require.Equal(t, domain, response.Name) + require.Equal(t, "automatic_invitation", response.EnrollmentMode) + require.Equal(t, "unverified", response.Verification.Status) +} + +func TestOrganizationDomainClientCreate_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":"create-error-code" + }], + "clerk_trace_id":"create-trace-id" +}`), + }, + } + client := NewClient(config) + _, err := client.Create(context.Background(), &CreateParams{}) + require.Error(t, err) + apiErr, ok := err.(*clerk.APIErrorResponse) + require.True(t, ok) + require.Equal(t, "create-trace-id", apiErr.TraceID) + require.Equal(t, 1, len(apiErr.Errors)) + require.Equal(t, "create-error-code", apiErr.Errors[0].Code) +} + +func TestOrganizationDomainClientUpdate(t *testing.T) { + t.Parallel() + id := "orgdm_123" + organizationID := "org_123" + verified := true + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + In: json.RawMessage(fmt.Sprintf(`{"verified": %s, "enrollment_mode": null}`, 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, + }) + require.NoError(t, err) + require.Equal(t, id, domain.ID) + require.Equal(t, "verified", domain.Verification.Status) +} + +func TestOrganizationDomainClientUpdate_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":"update-error-code" + }], + "clerk_trace_id":"update-trace-id" +}`), + }, + } + client := NewClient(config) + _, err := client.Update(context.Background(), &UpdateParams{}) + require.Error(t, err) + apiErr, ok := err.(*clerk.APIErrorResponse) + require.True(t, ok) + require.Equal(t, "update-trace-id", apiErr.TraceID) + require.Equal(t, 1, len(apiErr.Errors)) + require.Equal(t, "update-error-code", apiErr.Errors[0].Code) +} + +func TestOrganizationDomainClientDelete(t *testing.T) { + t.Parallel() + id := "orgdm_123" + organizationID := "org_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","object":"organization_domain"}`, id)), + Method: http.MethodDelete, + Path: "/v1/organizations/" + organizationID + "/domains/" + id, + }, + } + client := NewClient(config) + deletedResource, err := client.Delete(context.Background(), &DeleteParams{ + OrganizationID: organizationID, + DomainID: id, + }) + require.NoError(t, err) + require.Equal(t, id, deletedResource.ID) +} + +func TestOrganizationDomainClientList(t *testing.T) { + t.Parallel() + id := "orgdm_123" + domain := "mydomain.com" + organizationID := "org_123" + verified := true + + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{ +"data": [ + {"enrollment_mode":"automatic_suggestion","id":"%s","name":"%s","object":"organization_domain","organization_id":"%s","verification":{"status":"unverified"}} +], +"total_count": 1 +}`, + id, domain, organizationID)), + Method: http.MethodGet, + Path: "/v1/organizations/" + organizationID + "/domains", + Query: &url.Values{ + "limit": []string{"1"}, + "offset": []string{"2"}, + "verified": []string{"true"}, + "enrollment_mode": []string{"automatic_invitation"}, + }, + }, + } + client := NewClient(config) + params := &ListParams{ + OrganizationID: organizationID, + Verified: &verified, + EnrollmentModes: []string{"automatic_invitation"}, + } + params.Limit = clerk.Int64(1) + params.Offset = clerk.Int64(2) + list, err := client.List(context.Background(), params) + require.NoError(t, err) + require.Equal(t, id, list.OrganizationDomains[0].ID) + require.Equal(t, organizationID, list.OrganizationDomains[0].OrganizationID) +} From cc58653f91f7100fd87773157b14d0044d734c74 Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Tue, 17 Sep 2024 18:10:41 -0300 Subject: [PATCH 2/5] linter --- organizationdomain/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/organizationdomain/client.go b/organizationdomain/client.go index bf2057c..c314df0 100644 --- a/organizationdomain/client.go +++ b/organizationdomain/client.go @@ -102,7 +102,7 @@ func (params *ListParams) ToQuery() url.Values { q.Set("verified", strconv.FormatBool(*params.Verified)) } - if params.EnrollmentModes != nil && len(params.EnrollmentModes) > 0 { + if len(params.EnrollmentModes) > 0 { q["enrollment_mode"] = params.EnrollmentModes } return q From 5fbdbc3d87e23c21eb744ef762686f735e84b8fe Mon Sep 17 00:00:00 2001 From: nicolas lopes <57234795+NicolasLopes7@users.noreply.github.com> Date: Wed, 18 Sep 2024 09:49:27 -0300 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Giannis Katsanos --- organization_domain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/organization_domain.go b/organization_domain.go index b8498cf..2cd2d07 100644 --- a/organization_domain.go +++ b/organization_domain.go @@ -1,6 +1,6 @@ package clerk -type organizationDomainVerificationResponse struct { +type OrganizationDomainVerification struct { Status string `json:"status"` Strategy string `json:"strategy"` Attempts *int `json:"attempts"` @@ -19,7 +19,7 @@ type OrganizationDomain struct { TotalPendingInvitations int `json:"total_pending_invitations"` TotalPendingSuggestions int `json:"total_pending_suggestions"` CreatedAt int64 `json:"created_at"` - UpdatedAt int64 + UpdatedAt int64 `json:"updated_at"` } type OrganizationDomainList struct { From 955d1b8740846156b9aaad7f920bce81592c503a Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Wed, 18 Sep 2024 11:47:41 -0300 Subject: [PATCH 4/5] 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) From 3e341954e854f1d44cb2b8c59ee6c10eed7c89a3 Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Wed, 18 Sep 2024 13:01:04 -0300 Subject: [PATCH 5/5] refactor: ensure the same pattern is being followed --- organization_domain.go | 6 +++--- organizationdomain/api.go | 4 ++-- organizationdomain/client.go | 6 ++++-- organizationdomain/client_test.go | 8 +++++--- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/organization_domain.go b/organization_domain.go index 2cd6f6e..e2e0c23 100644 --- a/organization_domain.go +++ b/organization_domain.go @@ -3,7 +3,7 @@ package clerk type OrganizationDomainVerification struct { Status string `json:"status"` Strategy string `json:"strategy"` - Attempts *int `json:"attempts"` + Attempts int64 `json:"attempts"` ExpireAt *int64 `json:"expire_at"` } @@ -16,8 +16,8 @@ type OrganizationDomain struct { 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"` + TotalPendingInvitations int64 `json:"total_pending_invitations"` + TotalPendingSuggestions int64 `json:"total_pending_suggestions"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } diff --git a/organizationdomain/api.go b/organizationdomain/api.go index b0ab04e..2674f7e 100644 --- a/organizationdomain/api.go +++ b/organizationdomain/api.go @@ -14,8 +14,8 @@ func Create(ctx context.Context, organizationID string, params *CreateParams) (* } // Update updates an organization domain. -func Update(ctx context.Context, organizationID, domainID string, params *UpdateParams) (*clerk.OrganizationDomain, error) { - return getClient().Update(ctx, organizationID, domainID, params) +func Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationDomain, error) { + return getClient().Update(ctx, params) } // Delete removes a domain from an organization. diff --git a/organizationdomain/client.go b/organizationdomain/client.go index 514f9cc..5fdc5ba 100644 --- a/organizationdomain/client.go +++ b/organizationdomain/client.go @@ -47,13 +47,15 @@ func (c *Client) Create(ctx context.Context, organizationID string, params *Crea type UpdateParams struct { clerk.APIParams + OrganizationID string `json:"-"` + DomainID string `json:"-"` EnrollmentMode *string `json:"enrollment_mode,omitempty"` Verified *bool `json:"verified,omitempty"` } // Update updates an organization domain. -func (c *Client) Update(ctx context.Context, organizationID, domainID string, params *UpdateParams) (*clerk.OrganizationDomain, error) { - path, err := clerk.JoinPath(path, organizationID, "/domains", domainID) +func (c *Client) Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationDomain, error) { + path, err := clerk.JoinPath(path, params.OrganizationID, "/domains", params.DomainID) if err != nil { return nil, err } diff --git a/organizationdomain/client_test.go b/organizationdomain/client_test.go index 45c65da..b5ba96b 100644 --- a/organizationdomain/client_test.go +++ b/organizationdomain/client_test.go @@ -85,8 +85,10 @@ func TestOrganizationDomainClientUpdate(t *testing.T) { }, } client := NewClient(config) - domain, err := client.Update(context.Background(), organizationID, id, &UpdateParams{ - Verified: clerk.Bool(verified), + domain, err := client.Update(context.Background(), &UpdateParams{ + OrganizationID: organizationID, + DomainID: id, + Verified: clerk.Bool(verified), }) require.NoError(t, err) require.Equal(t, id, domain.ID) @@ -109,7 +111,7 @@ func TestOrganizationDomainClientUpdate_Error(t *testing.T) { }, } client := NewClient(config) - _, err := client.Update(context.Background(), "org_123", "orgdm_123", &UpdateParams{}) + _, err := client.Update(context.Background(), &UpdateParams{}) require.Error(t, err) apiErr, ok := err.(*clerk.APIErrorResponse) require.True(t, ok)