diff --git a/v1/client_test.go b/v1/client_test.go index fb06b40..059f749 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -13,7 +13,6 @@ import ( "time" "github.com/stretchr/testify/suite" - "gopkg.in/h2non/gock.v1" ) type MGClientTest struct { @@ -605,6 +604,134 @@ func (t *MGClientTest) Test_ImageMessages() { t.Assert().Equal(1, data.MessageID) } +func (t *MGClientTest) Test_ProductMessages() { + c := t.client() + + snd := SendData{ + Message: Message{ + ExternalID: "external_id", + Type: MsgTypeProduct, + Product: &MessageDataProduct{ + ID: 2, + Name: "Product name", + Article: "Product article", + Url: "https://example.loca/product/1", + Img: "https://example.loca/product/1/img", + Cost: &MessageDataOrderCost{ + Value: 100, + Currency: "USD", + }, + Unit: "pcs", + }, + }, + Originator: OriginatorCustomer, + Customer: Customer{ + ExternalID: "6", + Nickname: "octopus", + Firstname: "Joe", + Utm: &Utm{ + Source: "test-source", + Term: "", + }, + }, + Channel: 1, + ExternalChatID: "24798237492374", + } + + defer gock.Off() + t.gock(). + Post(t.transportURL("messages")). + Filter(func(request *http.Request) bool { + data, err := ioutil.ReadAll(request.Body) + if err != nil { + return false + } + request.Body = ioutil.NopCloser(bytes.NewReader(data)) + + var snd SendData + t.Require().NoError(json.Unmarshal(data, &snd)) + return t.Assert().Equal(uint64(2), snd.Message.Product.ID) + }). + Reply(http.StatusOK). + JSON( + MessagesResponse{ + MessageID: 1, + Time: time.Now(), + }, + ) + + data, status, err := c.Messages(snd) + t.Require().NoError(err) + t.Assert().Equal(http.StatusOK, status) + t.Assert().NotEmpty(data.Time.String()) + t.Assert().Equal(1, data.MessageID) +} + +func (t *MGClientTest) Test_OrderMessages() { + c := t.client() + + snd := SendData{ + Message: Message{ + ExternalID: "external_id", + Type: MsgTypeOrder, + Order: &MessageDataOrder{ + Number: "C1234", + ExternalID: 123, + Date: time.Now().String(), + Cost: &MessageDataOrderCost{ + Value: 100, + Currency: "USD", + }, + Discount: nil, + Status: nil, + Delivery: nil, + Payments: nil, + Items: nil, + }, + }, + Originator: OriginatorCustomer, + Customer: Customer{ + ExternalID: "6", + Nickname: "octopus", + Firstname: "Joe", + Utm: &Utm{ + Source: "test-source", + Term: "", + }, + }, + Channel: 1, + ExternalChatID: "24798237492374", + } + + defer gock.Off() + t.gock(). + Post(t.transportURL("messages")). + Filter(func(request *http.Request) bool { + data, err := ioutil.ReadAll(request.Body) + if err != nil { + return false + } + request.Body = ioutil.NopCloser(bytes.NewReader(data)) + + var snd SendData + t.Require().NoError(json.Unmarshal(data, &snd)) + return t.Assert().Equal(int64(123), snd.Message.Order.ExternalID) + }). + Reply(http.StatusOK). + JSON( + MessagesResponse{ + MessageID: 1, + Time: time.Now(), + }, + ) + + data, status, err := c.Messages(snd) + t.Require().NoError(err) + t.Assert().Equal(http.StatusOK, status) + t.Assert().NotEmpty(data.Time.String()) + t.Assert().Equal(1, data.MessageID) +} + func (t *MGClientTest) Test_UpdateMessages() { c := t.client() diff --git a/v1/types.go b/v1/types.go index caaf0db..3c8d8cf 100644 --- a/v1/types.go +++ b/v1/types.go @@ -306,12 +306,14 @@ type Utm struct { // Message struct. type Message struct { - ExternalID string `json:"external_id"` - Type string `json:"type,omitempty"` - Text string `json:"text,omitempty"` - Note string `json:"note,omitempty"` - Items []Item `json:"items,omitempty"` - PageLink string `json:"page_link,omitempty"` + ExternalID string `json:"external_id"` + Type string `json:"type,omitempty"` + Text string `json:"text,omitempty"` + Note string `json:"note,omitempty"` + Items []Item `json:"items,omitempty"` + PageLink string `json:"page_link,omitempty"` + Product *MessageDataProduct `json:"product,omitempty"` + Order *MessageDataOrder `json:"order,omitempty"` } // SendMessage struct. @@ -546,15 +548,16 @@ type MessageDataProduct struct { // MessageDataOrder order data from webhook. type MessageDataOrder struct { - Number string `json:"number"` - Url string `json:"url,omitempty"` - Date string `json:"date,omitempty"` - Cost *MessageDataOrderCost `json:"cost,omitempty"` - Discount *MessageDataOrderCost `json:"discount,omitempty"` - Status *MessageDataOrderStatus `json:"status,omitempty"` - Delivery *MessageDataOrderDelivery `json:"delivery"` - Payments []MessageDataOrderPayment `json:"payments"` - Items []MessageDataOrderItem `json:"items,omitempty"` + ExternalID int64 `json:"external_id"` + Number string `json:"number"` + URL string `json:"url,omitempty"` + Date string `json:"date,omitempty"` + Cost *MessageDataOrderCost `json:"cost,omitempty"` + Discount *MessageDataOrderCost `json:"discount,omitempty"` + Status *MessageDataOrderStatus `json:"status,omitempty"` + Delivery *MessageDataOrderDelivery `json:"delivery"` + Payments []MessageDataOrderPayment `json:"payments"` + Items []MessageDataOrderItem `json:"items,omitempty"` } // MessageDataOrderStatus type.