From 9bd3d646fc7b7a3385bc119cd422d61f31d9b6d2 Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Mon, 22 May 2023 09:00:51 +0300 Subject: [PATCH] correct templates methods --- client_test.go | 14 ++++++++++++-- template.go | 28 ++++++++++++++-------------- testutils.go | 11 +++++++---- types.go | 29 +++++++++++++++-------------- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/client_test.go b/client_test.go index d9c51eb..21b0ff6 100644 --- a/client_test.go +++ b/client_test.go @@ -7796,12 +7796,22 @@ func TestClient_ListMGChannelTemplates(t *testing.T) { assert.Equal(t, 1, template.Channel.ID) assert.Equal(t, 1, template.Channel.ExternalID) assert.Equal(t, "NAMEAAA", template.Name) + assert.Equal(t, TemplateItemTypeText, template.BodyTemplate[0].Type) + assert.Equal(t, "Text_0", template.BodyTemplate[0].Text) + assert.Equal(t, TemplateItemTypeVar, template.BodyTemplate[1].Type) + assert.Equal(t, TemplateVarCustom, template.BodyTemplate[1].VarType) + assert.Equal(t, []string{"Text_1"}, template.BodyTemplateExample) assert.Equal(t, "en", template.Lang) assert.Equal(t, "test_0", template.Category) - assert.Equal(t, []string{"Hello,", "{{1}}"}, template.Header.Text.Parts) + + assert.Equal(t, TemplateItemTypeText, template.Header.Text.Parts[0].Type) + assert.Equal(t, "JABAAA", template.Header.Text.Parts[0].Text) + assert.Equal(t, TemplateItemTypeVar, template.Header.Text.Parts[1].Type) + assert.Equal(t, TemplateVarCustom, template.Header.Text.Parts[1].VarType) assert.Equal(t, []string{"AAAAAA"}, template.Header.Text.Example) + assert.Equal(t, "https://example.com/file/123.png", template.Header.Image.Example) assert.Equal(t, "https://example.com/file/123.pdf", template.Header.Document.Example) assert.Equal(t, "https://example.com/file/123.mp4", template.Header.Video.Example) @@ -7823,7 +7833,7 @@ func TestClient_ListMGChannelTemplates(t *testing.T) { data, err := json.Marshal(template.BodyTemplate) assert.NoError(t, err) - assert.NotEmpty(t, `["Text_0",{}]`, string(data)) + assert.Equal(t, `["Text_0",{"var":"custom"}]`, string(data)) } func TestClient_ListMGChannelEmptyHeaderButtons(t *testing.T) { diff --git a/template.go b/template.go index 018a664..330f9c8 100644 --- a/template.go +++ b/template.go @@ -36,8 +36,8 @@ var templateVarAssoc = map[string]interface{}{ } type Text struct { - Parts []string `json:"parts"` - Example []string `json:"example,omitempty"` + Parts []TextTemplateItem `json:"parts"` + Example []string `json:"example,omitempty"` } type Media struct { @@ -51,17 +51,17 @@ type Header struct { Video *Media `json:"video,omitempty"` } -type TemplateItemList []BodyTemplateItem +type TemplateItemList []TextTemplateItem -// BodyTemplateItem is a part of template. -type BodyTemplateItem struct { +// TextTemplateItem is a part of template. +type TextTemplateItem struct { Text string VarType string Type uint8 } -// MarshalJSON controls how BodyTemplateItem will be marshaled into JSON. -func (t BodyTemplateItem) MarshalJSON() ([]byte, error) { +// MarshalJSON controls how TextTemplateItem will be marshaled into JSON. +func (t TextTemplateItem) MarshalJSON() ([]byte, error) { switch t.Type { case TemplateItemTypeText: return json.Marshal(t.Text) @@ -71,11 +71,11 @@ func (t BodyTemplateItem) MarshalJSON() ([]byte, error) { }) } - return nil, errors.New("unknown BodyTemplateItem type") + return nil, errors.New("unknown TextTemplateItem type") } -// UnmarshalJSON will correctly unmarshal BodyTemplateItem. -func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error { +// UnmarshalJSON will correctly unmarshal TextTemplateItem. +func (t *TextTemplateItem) UnmarshalJSON(b []byte) error { var obj interface{} err := json.Unmarshal(b, &obj) if err != nil { @@ -89,8 +89,8 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error { case map[string]interface{}: // {} case if len(bodyPart) == 0 { - t.Type = TemplateItemTypeVar - t.VarType = TemplateVarCustom + t.Type = TemplateItemTypeText + t.Text = "{}" return nil } @@ -102,10 +102,10 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error { t.Type = TemplateItemTypeVar t.VarType = varTypeCurr } else { - return errors.New("invalid BodyTemplateItem") + return errors.New("invalid TextTemplateItem") } default: - return errors.New("invalid BodyTemplateItem") + return errors.New("invalid TextTemplateItem") } return nil diff --git a/testutils.go b/testutils.go index 9e6e259..ba13e20 100644 --- a/testutils.go +++ b/testutils.go @@ -464,15 +464,18 @@ func getMGTemplatesResponse() string { "var": "custom" } ], + "templateExample": ["Text_1"], "namespace": "namespace_0", "lang": "en", "category": "test_0", "header": { "text": { "parts": [ - "Hello,", - "{{1}}" - ], + "JABAAA", + { + "var": "custom" + } + ], "example": [ "AAAAAA" ] @@ -514,5 +517,5 @@ func getMGTemplatesResponse() string { } func getMGTemplatesForEdit() string { - return `[{"header":{"text":{"parts":["Hello,","{{1}}"],"example":["Henry"]},"document":{"example":"https://example.com/file/123.pdf"},"image":{"example":"https://example.com/file/123.png"},"video":{"example":"https://example.com/file/123.mp4"}},"lang":"en","category":"test_0","code":"namespace#name_0#ru","name":"name_0","namespace":"namespace_0","footer":"footer_0","verificationStatus":"REJECTED","template":["Text_0",{"var":"custom"}],"buttons":[{"type":"PHONE_NUMBER","text":"your-phone-button-text","phoneNumber":"+79895553535"},{"type":"QUICK_REPLY","text":"Yes"},{"type":"URL","url":"https://example.com/file/{{1}}","text":"button","example":["https://www.website.com/dynamic-url-example"]}],"channel":{"type":"fbmessenger","name":"JABAAAAAAAAAA","id":1,"externalId":1,"allowedSendByPhone":false,"active":true},"id":1,"externalId":10,"active":true}]` + return `[{"header":{"text":{"parts":["Hello,",{"var":"custom"}],"example":["Henry"]},"document":{"example":"https://example.com/file/123.pdf"},"image":{"example":"https://example.com/file/123.png"},"video":{"example":"https://example.com/file/123.mp4"}},"lang":"en","category":"test_0","code":"namespace#name_0#ru","name":"name_0","namespace":"namespace","footer":"footer_0","verificationStatus":"REJECTED","template":["Text_0",{"var":"custom"}],"buttons":[{"type":"PHONE_NUMBER","text":"your-phone-button-text","phoneNumber":"+79895553535"},{"type":"QUICK_REPLY","text":"Yes"},{"type":"URL","url":"https://example.com/file/{{1}}","text":"button","example":["https://www.website.com/dynamic-url-example"]}],"channel":{"type":"fbmessenger","name":"JABAAAAAAAAAA","id":1,"externalId":1,"allowedSendByPhone":false,"active":true},"id":1,"externalId":10,"active":true}]` } diff --git a/types.go b/types.go index 95548b3..21ff671 100644 --- a/types.go +++ b/types.go @@ -1474,18 +1474,19 @@ type MGChannel struct { } type MGChannelTemplate struct { - Header *Header `json:"header"` - Lang string `json:"lang"` - Category string `json:"category"` - Code string `json:"code"` - Name string `json:"name"` - Namespace string `json:"namespace"` - Footer string `json:"footer,omitempty"` - VerificationStatus string `json:"verificationStatus"` - BodyTemplate TemplateItemList `json:"template,omitempty"` - Buttons []Button `json:"buttons,omitempty"` - Channel MGChannel `json:"channel"` - ID int `json:"id"` - ExternalID int `json:"externalId,omitempty"` - Active bool `json:"active"` + Header *Header `json:"header"` + Lang string `json:"lang"` + Category string `json:"category"` + Code string `json:"code,omitempty"` + Name string `json:"name"` + Namespace string `json:"namespace,omitempty"` + Footer string `json:"footer,omitempty"` + VerificationStatus string `json:"verificationStatus,omitempty"` + BodyTemplate TemplateItemList `json:"template"` + BodyTemplateExample []string `json:"templateExample"` + Buttons []Button `json:"buttons,omitempty"` + Channel MGChannel `json:"channel"` + ID int `json:"id,omitempty"` + ExternalID int `json:"externalId,omitempty"` + Active bool `json:"active"` }