Skip to content

Commit

Permalink
Merge pull request #21 from gildas/release/0.9.7
Browse files Browse the repository at this point in the history
Merge release/0.9.7
  • Loading branch information
gildas authored Oct 3, 2024
2 parents c18d78b + a893217 commit 2c12f99
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 32 deletions.
75 changes: 44 additions & 31 deletions conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package gcloudcx

import (
"context"
"strings"
"time"

"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
)
Expand All @@ -12,18 +14,18 @@ import (
//
// See: https://developer.mypurecloud.com/api/rest/v2/conversations
type Conversation struct {
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Name string `json:"name"`
ExternalTag string `json:"externalTag,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Address string `json:"address"`
Participants []*Participant `json:"participants"`
ConversationIDs []uuid.UUID `json:"conversationIds"`
MaxParticipants int `json:"maxParticipants"`
RecordingState string `json:"recordingState"`
State string `json:"state"`
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Name string `json:"name"`
ExternalTag string `json:"externalTag,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Address string `json:"address"`
Participants []Participant `json:"participants"`
ConversationIDs []uuid.UUID `json:"conversationIds"`
MaxParticipants int `json:"maxParticipants"`
RecordingState string `json:"recordingState"`
State string `json:"state"`
Divisions []struct {
Division DomainEntityRef `json:"division"`
Entities []DomainEntityRef `json:"entities"`
Expand Down Expand Up @@ -153,24 +155,35 @@ func (conversation Conversation) UpdateState(context context.Context, identifiab
)
}

/*
func (conversation *Conversation) GetParticipants(context context.Context) []*Participant {
log := conversation.logger.Child(nil, "participants")
data := struct {
ID uuid.UUID `json:"id"`
Participants []*Participant `json:"participants"`
OtherMediaURIs []URI `json:"otherMediaUris"`
SelfURI URI `json:"selfUri"`
}{}
err := conversation.client.Get(
context,
NewURI("/conversations/messages/%s", conversation.ID),
&data,
)
if err != nil {
log.Errorf("Failed to get participants", err)
return []*Participant{}
// GetParticipantByPurpose get the conversation's participant by its purpose
func (conversation Conversation) GetParticipantByPurpose(purpose string) (participant *Participant, found bool) {
for _, current := range conversation.Participants {
if current.Purpose == purpose {
return &current, true
}
}
return data.Participants
return nil, false
}

// AssociateExternalContact associates an ExternalContact to this Conversation
func (conversation Conversation) AssociateExternalContact(context context.Context, contact *ExternalContact, communicationID uuid.UUID, mediaType string) error {
if contact == nil {
return errors.ArgumentMissing.With("contact")
}
return conversation.client.Put(
context,
NewURI("/externalcontacts/conversations/%s", conversation.ID),
struct {
MediaType string `json:"mediaType"`
ConversationID string `json:"conversationId"`
CommunicationID string `json:"communicationId"`
ExternalContactID string `json:"externalContactId"`
}{
MediaType: strings.ToUpper(mediaType),
ConversationID: conversation.ID.String(),
CommunicationID: communicationID.String(),
ExternalContactID: contact.ID.String(),
},
nil,
)
}
*/
87 changes: 87 additions & 0 deletions external_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gcloudcx

import (
"context"
"encoding/json"

"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
)

// ExternalContact represents an external contact
//
// See: https://developer.genesys.cloud/devapps/api-explorer#post-api-v2-externalcontacts-contacts
type ExternalContact struct {
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Name string `json:"name"`
client *Client `json:"-"`
logger *logger.Logger `json:"-"`
}

// Initialize initializes the object
//
// accepted parameters: *gcloudcx.Client, *logger.Logger
//
// implements Initializable
func (contact *ExternalContact) Initialize(parameters ...interface{}) {
for _, raw := range parameters {
switch parameter := raw.(type) {
case uuid.UUID:
contact.ID = parameter
case *Client:
contact.client = parameter
case *logger.Logger:
contact.logger = parameter.Child("conversation", "conversation", "id", contact.ID)
}
}
if contact.logger == nil {
contact.logger = logger.Create("gclouccx", &logger.NilStream{})
}
}

// GetID gets the identifier of this
//
// implements Identifiable
func (contact ExternalContact) GetID() uuid.UUID {
return contact.ID
}

// GetURI gets the URI of this
//
// implements Addressable
func (contact ExternalContact) GetURI(ids ...uuid.UUID) URI {
if len(ids) > 0 {
return NewURI("/api/v2/externalcontacts/contacts/%s", ids[0])
}
if contact.ID != uuid.Nil {
return NewURI("/api/v2/externalcontacts/contacts/%s", contact.ID)
}
return URI("/api/v2/externalcontacts/contacts/")
}

// String gets a string version
//
// implements the fmt.Stringer interface
func (contact ExternalContact) String() string {
if len(contact.Name) != 0 {
return contact.Name
}
return contact.ID.String()
}

// SearchExternalContact search for an external contact by one of its attributes
func (client *Client) SearchExternalContact(context context.Context, value string) (contact *ExternalContact, err error) {
entities, err := client.FetchEntities(context, NewURI("/externalcontacts/contacts").WithQuery(Query{"q": value}))
if err != nil {
return nil, err
}
if len(entities) == 0 {
return nil, errors.NotFound.With("value", value)
}
if err = json.Unmarshal(entities[0], &contact); err != nil {
return nil, err
}
return
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package gcloudcx
var commit string

// VERSION is the version of this application
var VERSION = "0.9.6" + commit
var VERSION = "0.9.7" + commit

// APP is the name of the application
const APP string = "GCloudCX Client"

0 comments on commit 2c12f99

Please sign in to comment.