1
0
mirror of synced 2024-11-22 04:56:06 +03:00

отправка сообщения с типами product, order

This commit is contained in:
addfs 2024-07-23 16:20:47 +03:00
parent 28e6945670
commit c5b496e586
2 changed files with 146 additions and 16 deletions

View File

@ -13,7 +13,6 @@ import (
"time" "time"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
) )
type MGClientTest struct { type MGClientTest struct {
@ -605,6 +604,134 @@ func (t *MGClientTest) Test_ImageMessages() {
t.Assert().Equal(1, data.MessageID) 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() { func (t *MGClientTest) Test_UpdateMessages() {
c := t.client() c := t.client()

View File

@ -312,6 +312,8 @@ type Message struct {
Note string `json:"note,omitempty"` Note string `json:"note,omitempty"`
Items []Item `json:"items,omitempty"` Items []Item `json:"items,omitempty"`
PageLink string `json:"page_link,omitempty"` PageLink string `json:"page_link,omitempty"`
Product *MessageDataProduct `json:"product,omitempty"`
Order *MessageDataOrder `json:"order,omitempty"`
} }
// SendMessage struct. // SendMessage struct.
@ -546,8 +548,9 @@ type MessageDataProduct struct {
// MessageDataOrder order data from webhook. // MessageDataOrder order data from webhook.
type MessageDataOrder struct { type MessageDataOrder struct {
ExternalID int64 `json:"external_id"`
Number string `json:"number"` Number string `json:"number"`
Url string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Date string `json:"date,omitempty"` Date string `json:"date,omitempty"`
Cost *MessageDataOrderCost `json:"cost,omitempty"` Cost *MessageDataOrderCost `json:"cost,omitempty"`
Discount *MessageDataOrderCost `json:"discount,omitempty"` Discount *MessageDataOrderCost `json:"discount,omitempty"`