Skip to content

Commit

Permalink
Merge new Zendesk users into existing users
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Dec 6, 2024
1 parent 43e8498 commit c71af42
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
37 changes: 29 additions & 8 deletions services/tickets/zendesk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,15 @@ type User struct {
Organization struct {
Name string `json:"name"`
} `json:"organization,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Identities []struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"identities,omitempty"`
Verified bool `json:"verified,omitempty"`
Role string `json:"role,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Identities []Identity `json:"identities,omitempty"`
Verified bool `json:"verified,omitempty"`
Role string `json:"role,omitempty"`
}

type Identity struct {
Type string `json:"type"`
Value string `json:"value"`
}

// CreateUser creates a new user in zendesk
Expand All @@ -349,7 +351,7 @@ type SearchUserResponse struct {

// SearchUser returns the user with the given external ID or nil if it doesn't exist
func (c *RESTClient) SearchUser(externalID string) (*User, *httpx.Trace, error) {
endpoint := fmt.Sprintf("users/search.json?external_id=%s", externalID)
endpoint := fmt.Sprintf("users/search.json?query=%s", externalID)
var response SearchUserResponse
trace, err := c.get(endpoint, nil, &response)
if err != nil {
Expand All @@ -360,3 +362,22 @@ func (c *RESTClient) SearchUser(externalID string) (*User, *httpx.Trace, error)
}
return &response.Users[0], trace, nil
}

// MergeUser merge two users
func (c *RESTClient) MergeUser(userID int64, unmergedUserID int64) (*User, *httpx.Trace, error) {
endpoint := fmt.Sprintf("users/%s/merge", unmergedUserID)

Check failure on line 368 in services/tickets/zendesk/client.go

View workflow job for this annotation

GitHub Actions / Test (12)

Sprintf format %s has arg unmergedUserID of wrong type int64

Check failure on line 368 in services/tickets/zendesk/client.go

View workflow job for this annotation

GitHub Actions / Test (13)

Sprintf format %s has arg unmergedUserID of wrong type int64

payload := struct {
User *User `json:"user"`
}{User: &User{ID: userID}}

response := &struct {
User *User `json:"user"`
}{}

trace, err := c.post(endpoint, payload, &response)
if err != nil {
return nil, trace, err
}
return response.User, trace, nil
}
33 changes: 31 additions & 2 deletions services/tickets/zendesk/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return nil, err
}
if trace.Response.StatusCode == http.StatusNotFound || user == nil {
user := &User{
newUser := &User{
Name: contactDisplay,
ExternalID: contactUUID,
Verified: true,
Role: "end-user",
Identities: []Identity{
{
Type: "phone_number",
Value: string(session.Contact().PreferredURN().URN().Path()),
},
},
}
_, trace, err = s.restClient.CreateUser(user)
user, trace, err = s.restClient.CreateUser(newUser)
if trace != nil {
logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor))
}
Expand All @@ -113,6 +119,12 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
Author: Author{
ExternalID: contactUUID,
Name: contactDisplay,
Fields: []FieldValue{
{
ID: "details",
Value: contactUUID,
},
},
},
AllowChannelback: true,
}
Expand Down Expand Up @@ -174,6 +186,23 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
if err := s.push(msg, logHTTP); err != nil {
return nil, err
}

unmergedUser, trace, err := s.restClient.SearchUser("type:user details:\"" + contactUUID + "\"")
if trace != nil {
logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor))
}
if err != nil && trace.Response.StatusCode != http.StatusNotFound {
return nil, err
}

_, trace, err = s.restClient.MergeUser(user.ID, unmergedUser.ID)
if trace != nil {
logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor))
}
if err != nil && trace.Response.StatusCode != http.StatusNotFound {
return nil, err
}

return ticket, nil
}

Expand Down

0 comments on commit c71af42

Please sign in to comment.