Skip to content

Commit

Permalink
New templates support
Browse files Browse the repository at this point in the history
  • Loading branch information
Neur0toxine authored Nov 10, 2023
2 parents 9f3cda8 + 33a8c6c commit 19bb8f8
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 102 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- gomnd
- lll
- bodyclose
Expand Down
1 change: 1 addition & 0 deletions mg-transport-api-client-go
6 changes: 3 additions & 3 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ func (c *MgClient) ActivateTemplate(channelID uint64, request ActivateTemplateRe
// if err != nil {
// fmt.Printf("%#v", err)
// }
func (c *MgClient) UpdateTemplate(request Template) (int, error) {
func (c *MgClient) UpdateTemplate(channelID uint64, code string, request UpdateTemplateRequest) (int, error) {
outgoing, _ := json.Marshal(&request)

if request.ChannelID == 0 || request.Code == "" {
if channelID == 0 || code == "" {
return 0, errors.New("`ChannelID` and `Code` cannot be blank")
}

data, status, err := c.PutRequest(
fmt.Sprintf("/channels/%d/templates/%s", request.ChannelID, url.PathEscape(request.Code)), outgoing)
fmt.Sprintf("/channels/%d/templates/%s", channelID, url.PathEscape(code)), outgoing)
if err != nil {
return status, err
}
Expand Down
58 changes: 29 additions & 29 deletions v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,24 +351,26 @@ func (t *MGClientTest) Test_ActivateTemplate() {
c := t.client()
req := ActivateTemplateRequest{
Code: "tplCode",
Name: "tplCode",
Type: TemplateTypeText,
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Text: "Hello ",
},
{
Type: TemplateItemTypeVar,
VarType: TemplateVarFirstName,
},
{
Type: TemplateItemTypeText,
Text: "!",
UpdateTemplateRequest: UpdateTemplateRequest{
Name: "tplCode",
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Text: "Hello ",
},
{
Type: TemplateItemTypeVar,
VarType: TemplateVarFirstName,
},
{
Type: TemplateItemTypeText,
Text: "!",
},
},
RejectionReason: "",
VerificationStatus: TemplateStatusApproved,
},
RejectionReason: "",
VerificationStatus: "approved",
}

defer gock.Off()
Expand All @@ -384,12 +386,8 @@ func (t *MGClientTest) Test_ActivateTemplate() {

func (t *MGClientTest) Test_UpdateTemplate() {
c := t.client()
tpl := Template{
Code: "encodable#code",
ChannelID: 1,
Name: "updated name",
Enabled: true,
Type: TemplateTypeText,
tpl := UpdateTemplateRequest{
Name: "updated name",
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Expand Down Expand Up @@ -421,27 +419,29 @@ func (t *MGClientTest) Test_UpdateTemplate() {
t.gock().
Get(t.transportURL("templates")).
Reply(http.StatusOK).
JSON([]Template{tpl})
JSON([]ActivateTemplateRequest{ActivateTemplateRequest{
UpdateTemplateRequest: tpl,
Code: "encodable#code",
Type: TemplateTypeText,
}})

status, err := c.UpdateTemplate(tpl)
status, err := c.UpdateTemplate(1, "encodable#code", tpl)
t.Assert().NoError(err, fmt.Sprintf("%d %s", status, err))

templates, status, err := c.TransportTemplates()
t.Assert().NoError(err, fmt.Sprintf("%d %s", status, err))

for _, template := range templates {
if template.Code == tpl.Code {
if template.Code == "encodable#code" {
t.Assert().Equal(tpl.Name, template.Name)
}
}
}

func (t *MGClientTest) Test_UpdateTemplateFail() {
c := t.client()
tpl := Template{
Name: "updated name",
Enabled: true,
Type: TemplateTypeText,
tpl := UpdateTemplateRequest{
Name: "updated name",
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Expand All @@ -467,7 +467,7 @@ func (t *MGClientTest) Test_UpdateTemplateFail() {
},
)

status, err := c.UpdateTemplate(tpl)
status, err := c.UpdateTemplate(1, "encodable#code", tpl)
t.Assert().Error(err, fmt.Sprintf("%d %s", status, err))
}

Expand Down
Loading

0 comments on commit 19bb8f8

Please sign in to comment.