Skip to content

Commit

Permalink
отправка сообщения с типами product, order
Browse files Browse the repository at this point in the history
  • Loading branch information
addfs committed Jul 23, 2024
1 parent 28e6945 commit c5b496e
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 16 deletions.
129 changes: 128 additions & 1 deletion v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
)

type MGClientTest struct {
Expand Down Expand Up @@ -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()

Expand Down
33 changes: 18 additions & 15 deletions v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c5b496e

Please sign in to comment.