From 4206ad26952e228b84805d10e8fa74862db0350b Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 11 May 2020 17:41:17 +0300 Subject: [PATCH] Templates fields, additional validation in UpdateTemplate --- v1/client.go | 4 ++++ v1/client_test.go | 26 ++++++++++++++++++++++++++ v1/types.go | 2 ++ 3 files changed, 32 insertions(+) diff --git a/v1/client.go b/v1/client.go index eb1c830..a40e831 100644 --- a/v1/client.go +++ b/v1/client.go @@ -136,6 +136,10 @@ func (c *MgClient) ActivateTemplate(channelID uint64, request ActivateTemplateRe func (c *MgClient) UpdateTemplate(request Template) (int, error) { outgoing, _ := json.Marshal(&request) + if request.ChannelID == 0 || request.Code == "" { + return 0, errors.New("`ChannelID` and `Code` cannot be blank") + } + data, status, err := c.PutRequest(fmt.Sprintf("/channels/%d/templates/%s", request.ChannelID, request.Code), outgoing) if err != nil { return status, err diff --git a/v1/client_test.go b/v1/client_test.go index d7443eb..bb9ac35 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -329,6 +329,32 @@ func TestMgClient_UpdateTemplate(t *testing.T) { } } +func TestMgClient_UpdateTemplateFail(t *testing.T) { + c := client() + tpl := Template{ + Name: "updated name", + Enabled: true, + Type: TemplateTypeText, + Template: []TemplateItem{ + { + Type: TemplateItemTypeText, + Text: "Welcome ", + }, + { + Type: TemplateItemTypeVar, + VarType: TemplateVarFirstName, + }, + { + Type: TemplateItemTypeText, + Text: "!", + }, + }, + } + + status, err := c.UpdateTemplate(tpl) + assert.Error(t, err, fmt.Sprintf("%d %s", status, err)) +} + func TestMgClient_DeactivateTemplate(t *testing.T) { c := client() status, err := c.DeactivateTemplate(templateChannel(t), tplCode) diff --git a/v1/types.go b/v1/types.go index 2243664..3141784 100644 --- a/v1/types.go +++ b/v1/types.go @@ -314,6 +314,8 @@ type WebhookData struct { Product *MessageDataProduct `json:"product,omitempty"` Order *MessageDataOrder `json:"order,omitempty"` Items *[]FileItem `json:"items,omitempty"` + TemplateCode string `json:"template_code,omitempty"` + TemplateArgs []string `json:"template_args,omitempty"` } // FileItem struct