From c5b496e586a79219094cec6d67f8cf0c57ab4d75 Mon Sep 17 00:00:00 2001 From: addfs Date: Tue, 23 Jul 2024 16:20:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=81=20=D1=82=D0=B8=D0=BF=D0=B0=D0=BC=D0=B8=20product,=20o?= =?UTF-8?q?rder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v1/client_test.go | 129 +++++++++++++++++++++++++++++++++++++++++++++- v1/types.go | 33 ++++++------ 2 files changed, 146 insertions(+), 16 deletions(-) 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.