Skip to content

Commit f5af0cb

Browse files
author
Gildas Cherruel
committed
Merge branch 'release/0.8.2'
2 parents 584f4c7 + d955255 commit f5af0cb

File tree

5 files changed

+224
-19
lines changed

5 files changed

+224
-19
lines changed

responsemanagement_response.go

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gcloudcx
22

33
import (
44
"context"
5+
"encoding/json"
56
"strings"
67
"text/template"
78
"time"
@@ -16,15 +17,20 @@ import (
1617
//
1718
// See https://developer.genesys.cloud/api/rest/v2/responsemanagement
1819
type ResponseManagementResponse struct {
19-
ID uuid.UUID `json:"id"`
20-
Name string `json:"name"`
21-
DateCreated time.Time `json:"dateCreated,omitempty"`
22-
CreatedBy *DomainEntityRef `json:"createdBy,omitempty"`
23-
Libraries []DomainEntityRef `json:"libraries,omitempty"`
24-
Texts []ResponseManagementContent `json:"texts,omitempty"`
25-
Substitutions []ResponseManagementSubstitution `json:"substitutions,omitempty"`
26-
Version int `json:"version"`
27-
SelfURI URI `json:"selfUri,omitempty"`
20+
ID uuid.UUID `json:"id"`
21+
Name string `json:"name"`
22+
Type string `json:"ResponseType"`
23+
DateCreated time.Time `json:"dateCreated,omitempty"`
24+
CreatedBy *DomainEntityRef `json:"createdBy,omitempty"`
25+
Libraries []DomainEntityRef `json:"libraries,omitempty"`
26+
Texts []ResponseManagementContent `json:"texts,omitempty"`
27+
Substitutions []ResponseManagementSubstitution `json:"substitutions,omitempty"`
28+
TemplateType string `json:"-"`
29+
TemplateName string `json:"-"`
30+
TemplateNamespace string `json:"-"`
31+
TemplateLanguage string `json:"-"`
32+
Version int `json:"version"`
33+
SelfURI URI `json:"selfUri,omitempty"`
2834
}
2935

3036
// ResponseManagementContent represent a Response Management Content
@@ -88,6 +94,13 @@ func (response ResponseManagementResponse) GetURI(ids ...uuid.UUID) URI {
8894
return URI("/api/v2/responsemanagement/responses/")
8995
}
9096

97+
// GetType gets the type of this
98+
//
99+
// implements core.TypeCarrier
100+
func (response ResponseManagementResponse) GetType() string {
101+
return response.Type
102+
}
103+
91104
// String gets a string version
92105
//
93106
// implements the fmt.Stringer interface
@@ -184,3 +197,68 @@ func (response ResponseManagementResponse) ApplySubstitutions(context context.Co
184197
}
185198
return expanded.String(), nil
186199
}
200+
201+
// MarshalJSON marshals into JSON
202+
//
203+
// implements json.Marshaler
204+
func (response ResponseManagementResponse) MarshalJSON() ([]byte, error) {
205+
type surrogate ResponseManagementResponse
206+
type MessagingTemplate struct {
207+
WhatsApp struct {
208+
Name string `json:"name"`
209+
Namespace string `json:"namespace"`
210+
Language string `json:"language"`
211+
} `json:"whatsApp"`
212+
}
213+
214+
data, err := json.Marshal(struct {
215+
surrogate
216+
MessagingTemplate *MessagingTemplate `json:"messagingTemplate,omitempty"`
217+
}{
218+
surrogate: surrogate(response),
219+
MessagingTemplate: func() *MessagingTemplate {
220+
switch response.TemplateType {
221+
case "whatsApp":
222+
template := MessagingTemplate{}
223+
template.WhatsApp.Name = response.TemplateName
224+
template.WhatsApp.Namespace = response.TemplateNamespace
225+
template.WhatsApp.Language = response.TemplateLanguage
226+
return &template
227+
default:
228+
return nil
229+
}
230+
}(),
231+
})
232+
return data, errors.JSONMarshalError.Wrap(err)
233+
}
234+
235+
// UnmarshalJSON unmarshals JSON
236+
//
237+
// implements json.Unmarshaler
238+
func (response *ResponseManagementResponse) UnmarshalJSON(payload []byte) (err error) {
239+
type surrogate ResponseManagementResponse
240+
var inner struct {
241+
surrogate
242+
MessagingTemplate *struct {
243+
WhatsApp *struct {
244+
Name string `json:"name"`
245+
Namespace string `json:"namespace"`
246+
Language string `json:"language"`
247+
} `json:"whatsApp"`
248+
}
249+
}
250+
251+
if err := json.Unmarshal(payload, &inner); err != nil {
252+
return errors.JSONUnmarshalError.Wrap(err)
253+
}
254+
*response = ResponseManagementResponse(inner.surrogate)
255+
if inner.MessagingTemplate != nil {
256+
if inner.MessagingTemplate.WhatsApp != nil {
257+
response.TemplateType = "whatsApp"
258+
response.TemplateName = inner.MessagingTemplate.WhatsApp.Name
259+
response.TemplateNamespace = inner.MessagingTemplate.WhatsApp.Namespace
260+
response.TemplateLanguage = inner.MessagingTemplate.WhatsApp.Language
261+
}
262+
}
263+
return
264+
}

responsemanagement_test.go

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package gcloudcx_test
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"os"
8+
"path/filepath"
69
"reflect"
710
"strings"
811
"testing"
@@ -64,12 +67,6 @@ func (suite *ResponseManagementSuite) SetupSuite() {
6467
secret := core.GetEnvAsString("PURECLOUD_CLIENTSECRET", "")
6568
suite.Require().NotEmpty(secret, "PURECLOUD_CLIENTSECRET is not set")
6669

67-
value = core.GetEnvAsString("PURECLOUD_DEPLOYMENTID", "")
68-
suite.Require().NotEmpty(value, "PURECLOUD_DEPLOYMENTID is not set")
69-
70-
deploymentID, err := uuid.Parse(value)
71-
suite.Require().NoError(err, "PURECLOUD_DEPLOYMENTID is not a valid UUID")
72-
7370
value = core.GetEnvAsString("RESPONSE_MANAGEMENT_LIBRARY_ID", "")
7471
suite.Require().NotEmpty(value, "RESPONSE_MANAGEMENT_LIBRARY_ID is not set in your environment")
7572

@@ -89,9 +86,8 @@ func (suite *ResponseManagementSuite) SetupSuite() {
8986
suite.Require().NotEmpty(suite.ResponseName, "RESPONSE_MANAGEMENT_RESPONSE_NAME is not set in your environment")
9087

9188
suite.Client = gcloudcx.NewClient(&gcloudcx.ClientOptions{
92-
Region: region,
93-
DeploymentID: deploymentID,
94-
Logger: suite.Logger,
89+
Region: region,
90+
Logger: suite.Logger,
9591
}).SetAuthorizationGrant(&gcloudcx.ClientCredentialsGrant{
9692
ClientID: clientID,
9793
Secret: secret,
@@ -130,9 +126,71 @@ func (suite *ResponseManagementSuite) AfterTest(suiteName, testName string) {
130126
suite.Logger.Record("duration", duration.String()).Infof("Test End: %s %s", testName, strings.Repeat("-", 80-11-len(testName)))
131127
}
132128

129+
func (suite *ResponseManagementSuite) LoadTestData(filename string) []byte {
130+
data, err := os.ReadFile(filepath.Join(".", "testdata", filename))
131+
suite.Require().NoErrorf(err, "Failed to Load Data. %s", err)
132+
return data
133+
}
134+
135+
func (suite *ResponseManagementSuite) UnmarshalData(filename string, v interface{}) error {
136+
data := suite.LoadTestData(filename)
137+
suite.Logger.Infof("Loaded %s: %s", filename, string(data))
138+
return json.Unmarshal(data, v)
139+
}
140+
133141
// #endregion: Suite Tools }}}
134142
// *****************************************************************************
135143

144+
func (suite *ResponseManagementSuite) TestCanUnmarshalCampaignSMSTemplate() {
145+
var response gcloudcx.ResponseManagementResponse
146+
147+
err := suite.UnmarshalData("response-campaignsms-template.json", &response)
148+
suite.Require().NoError(err, "Failed to unmarshal response")
149+
suite.Assert().Equal("response-01", response.Name)
150+
suite.Assert().Equal(uuid.MustParse("86CABAAE-BD5E-4615-A4F2-712467E808F0"), response.ID)
151+
suite.Assert().Equal(12, response.Version)
152+
suite.Assert().Equal("CampaignSmsTemplate", response.GetType())
153+
suite.Assert().Equal(time.Date(2023, 7, 25, 7, 30, 10, 449000000, time.UTC), response.DateCreated)
154+
suite.Require().NotNil(response.CreatedBy, "Respnose CreatedBy should not be nil")
155+
suite.Assert().Equal(uuid.MustParse("DDA998ED-2258-4317-8070-2745465B8B28"), response.CreatedBy.ID)
156+
suite.Require().Len(response.Libraries, 1)
157+
suite.Assert().Equal(uuid.MustParse("2035D559-793E-4F4B-9A09-118D9C265EFD"), response.Libraries[0].ID)
158+
suite.Assert().Equal("Test Library", response.Libraries[0].Name)
159+
suite.Require().Len(response.Texts, 1)
160+
suite.Assert().Equal("text/plain", response.Texts[0].ContentType)
161+
suite.Assert().Equal("Hello {{Name}}", response.Texts[0].Content)
162+
suite.Require().Len(response.Substitutions, 1)
163+
suite.Assert().Equal("Name", response.Substitutions[0].ID)
164+
suite.Assert().Equal("John Doe", response.Substitutions[0].Default)
165+
}
166+
167+
func (suite *ResponseManagementSuite) TestCanUnmarshalMessageTemplate() {
168+
var response gcloudcx.ResponseManagementResponse
169+
170+
err := suite.UnmarshalData("response-message-template.json", &response)
171+
suite.Require().NoError(err, "Failed to unmarshal response")
172+
suite.Assert().Equal("response-02", response.Name)
173+
suite.Assert().Equal(uuid.MustParse("A7F1F131-7E50-4117-982A-2D5C55C9ED5E"), response.ID)
174+
suite.Assert().Equal(1, response.Version)
175+
suite.Assert().Equal("MessagingTemplate", response.GetType())
176+
suite.Assert().Equal(time.Date(2023, 7, 25, 7, 30, 10, 449000000, time.UTC), response.DateCreated)
177+
suite.Require().NotNil(response.CreatedBy, "Respnose CreatedBy should not be nil")
178+
suite.Assert().Equal(uuid.MustParse("DDA998ED-2258-4317-8070-2745465B8B28"), response.CreatedBy.ID)
179+
suite.Require().Len(response.Libraries, 1)
180+
suite.Assert().Equal(uuid.MustParse("2035D559-793E-4F4B-9A09-118D9C265EFD"), response.Libraries[0].ID)
181+
suite.Assert().Equal("Test Library", response.Libraries[0].Name)
182+
suite.Require().Len(response.Texts, 1)
183+
suite.Assert().Equal("text/plain", response.Texts[0].ContentType)
184+
suite.Assert().Equal("Hello {{Name}}", response.Texts[0].Content)
185+
suite.Require().Len(response.Substitutions, 1)
186+
suite.Assert().Equal("Name", response.Substitutions[0].ID)
187+
suite.Assert().Equal("John Doe", response.Substitutions[0].Default)
188+
suite.Assert().Equal("whatsApp", response.TemplateType)
189+
suite.Assert().Equal("template-01", response.TemplateName)
190+
suite.Assert().Equal("templates", response.TemplateNamespace)
191+
suite.Assert().Equal("en_US", response.TemplateLanguage)
192+
}
193+
136194
func (suite *ResponseManagementSuite) TestCanFetchLibraryByID() {
137195
library, err := gcloudcx.Fetch[gcloudcx.ResponseManagementLibrary](context.Background(), suite.Client, suite.LibraryID)
138196
if err != nil {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"id": "86CABAAE-BD5E-4615-A4F2-712467E808F0",
3+
"name": "response-01",
4+
"version": 12,
5+
"libraries": [
6+
{
7+
"id": "2035D559-793E-4F4B-9A09-118D9C265EFD",
8+
"name": "Test Library",
9+
"selfUri": "/api/v2/responsemanagement/libraries/2035D559-793E-4F4B-9A09-118D9C265EFD"
10+
}
11+
],
12+
"texts": [
13+
{
14+
"content": "Hello {{Name}}",
15+
"contentType": "text/plain"
16+
}
17+
],
18+
"createdBy": {
19+
"id": "DDA998ED-2258-4317-8070-2745465B8B28",
20+
"selfUri": "/api/v2/users/DDA998ED-2258-4317-8070-2745465B8B28"
21+
},
22+
"dateCreated": "2023-07-25T07:30:10.449Z",
23+
"substitutions": [
24+
{
25+
"id": "Name",
26+
"defaultValue": "John Doe"
27+
}
28+
],
29+
"responseType": "CampaignSmsTemplate",
30+
"selfUri": "/api/v2/responsemanagement/responses/86CABAAE-BD5E-4615-A4F2-712467E808F0"
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"id": "A7F1F131-7E50-4117-982A-2D5C55C9ED5E",
3+
"name": "response-02",
4+
"version": 1,
5+
"libraries": [
6+
{
7+
"id": "2035D559-793E-4F4B-9A09-118D9C265EFD",
8+
"name": "Test Library",
9+
"selfUri": "/api/v2/responsemanagement/libraries/2035D559-793E-4F4B-9A09-118D9C265EFD"
10+
}
11+
],
12+
"texts": [
13+
{
14+
"content": "Hello {{Name}}",
15+
"contentType": "text/plain"
16+
}
17+
],
18+
"createdBy": {
19+
"id": "DDA998ED-2258-4317-8070-2745465B8B28",
20+
"selfUri": "/api/v2/users/DDA998ED-2258-4317-8070-2745465B8B28"
21+
},
22+
"dateCreated": "2023-07-25T07:30:10.449Z",
23+
"substitutions": [
24+
{
25+
"id": "Name",
26+
"defaultValue": "John Doe"
27+
}
28+
],
29+
"responseType": "MessagingTemplate",
30+
"messagingTemplate": {
31+
"whatsApp": {
32+
"name": "template-01",
33+
"namespace": "templates",
34+
"language": "en_US"
35+
}
36+
},
37+
"selfUri": "/api/v2/responsemanagement/responses/A7F1F131-7E50-4117-982A-2D5C55C9ED5E"
38+
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package gcloudcx
44
var commit string
55

66
// VERSION is the version of this application
7-
var VERSION = "0.8.1" + commit
7+
var VERSION = "0.8.2" + commit
88

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

0 commit comments

Comments
 (0)