correct templates methods

This commit is contained in:
Ruslan Efanov 2023-05-22 09:00:51 +03:00
parent 16e2bc304c
commit 9bd3d646fc
4 changed files with 48 additions and 34 deletions

View File

@ -7796,12 +7796,22 @@ func TestClient_ListMGChannelTemplates(t *testing.T) {
assert.Equal(t, 1, template.Channel.ID) assert.Equal(t, 1, template.Channel.ID)
assert.Equal(t, 1, template.Channel.ExternalID) assert.Equal(t, 1, template.Channel.ExternalID)
assert.Equal(t, "NAMEAAA", template.Name) assert.Equal(t, "NAMEAAA", template.Name)
assert.Equal(t, TemplateItemTypeText, template.BodyTemplate[0].Type) 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, 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, "en", template.Lang)
assert.Equal(t, "test_0", template.Category) 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, []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.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.pdf", template.Header.Document.Example)
assert.Equal(t, "https://example.com/file/123.mp4", template.Header.Video.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) data, err := json.Marshal(template.BodyTemplate)
assert.NoError(t, err) 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) { func TestClient_ListMGChannelEmptyHeaderButtons(t *testing.T) {

View File

@ -36,7 +36,7 @@ var templateVarAssoc = map[string]interface{}{
} }
type Text struct { type Text struct {
Parts []string `json:"parts"` Parts []TextTemplateItem `json:"parts"`
Example []string `json:"example,omitempty"` Example []string `json:"example,omitempty"`
} }
@ -51,17 +51,17 @@ type Header struct {
Video *Media `json:"video,omitempty"` Video *Media `json:"video,omitempty"`
} }
type TemplateItemList []BodyTemplateItem type TemplateItemList []TextTemplateItem
// BodyTemplateItem is a part of template. // TextTemplateItem is a part of template.
type BodyTemplateItem struct { type TextTemplateItem struct {
Text string Text string
VarType string VarType string
Type uint8 Type uint8
} }
// MarshalJSON controls how BodyTemplateItem will be marshaled into JSON. // MarshalJSON controls how TextTemplateItem will be marshaled into JSON.
func (t BodyTemplateItem) MarshalJSON() ([]byte, error) { func (t TextTemplateItem) MarshalJSON() ([]byte, error) {
switch t.Type { switch t.Type {
case TemplateItemTypeText: case TemplateItemTypeText:
return json.Marshal(t.Text) 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. // UnmarshalJSON will correctly unmarshal TextTemplateItem.
func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error { func (t *TextTemplateItem) UnmarshalJSON(b []byte) error {
var obj interface{} var obj interface{}
err := json.Unmarshal(b, &obj) err := json.Unmarshal(b, &obj)
if err != nil { if err != nil {
@ -89,8 +89,8 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error {
case map[string]interface{}: case map[string]interface{}:
// {} case // {} case
if len(bodyPart) == 0 { if len(bodyPart) == 0 {
t.Type = TemplateItemTypeVar t.Type = TemplateItemTypeText
t.VarType = TemplateVarCustom t.Text = "{}"
return nil return nil
} }
@ -102,10 +102,10 @@ func (t *BodyTemplateItem) UnmarshalJSON(b []byte) error {
t.Type = TemplateItemTypeVar t.Type = TemplateItemTypeVar
t.VarType = varTypeCurr t.VarType = varTypeCurr
} else { } else {
return errors.New("invalid BodyTemplateItem") return errors.New("invalid TextTemplateItem")
} }
default: default:
return errors.New("invalid BodyTemplateItem") return errors.New("invalid TextTemplateItem")
} }
return nil return nil

View File

@ -464,14 +464,17 @@ func getMGTemplatesResponse() string {
"var": "custom" "var": "custom"
} }
], ],
"templateExample": ["Text_1"],
"namespace": "namespace_0", "namespace": "namespace_0",
"lang": "en", "lang": "en",
"category": "test_0", "category": "test_0",
"header": { "header": {
"text": { "text": {
"parts": [ "parts": [
"Hello,", "JABAAA",
"{{1}}" {
"var": "custom"
}
], ],
"example": [ "example": [
"AAAAAA" "AAAAAA"
@ -514,5 +517,5 @@ func getMGTemplatesResponse() string {
} }
func getMGTemplatesForEdit() 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}]`
} }

View File

@ -1477,15 +1477,16 @@ type MGChannelTemplate struct {
Header *Header `json:"header"` Header *Header `json:"header"`
Lang string `json:"lang"` Lang string `json:"lang"`
Category string `json:"category"` Category string `json:"category"`
Code string `json:"code"` Code string `json:"code,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Namespace string `json:"namespace"` Namespace string `json:"namespace,omitempty"`
Footer string `json:"footer,omitempty"` Footer string `json:"footer,omitempty"`
VerificationStatus string `json:"verificationStatus"` VerificationStatus string `json:"verificationStatus,omitempty"`
BodyTemplate TemplateItemList `json:"template,omitempty"` BodyTemplate TemplateItemList `json:"template"`
BodyTemplateExample []string `json:"templateExample"`
Buttons []Button `json:"buttons,omitempty"` Buttons []Button `json:"buttons,omitempty"`
Channel MGChannel `json:"channel"` Channel MGChannel `json:"channel"`
ID int `json:"id"` ID int `json:"id,omitempty"`
ExternalID int `json:"externalId,omitempty"` ExternalID int `json:"externalId,omitempty"`
Active bool `json:"active"` Active bool `json:"active"`
} }