Packs methods

This commit is contained in:
Alex Lushpai 2018-02-06 17:03:35 +03:00
parent 76761cc927
commit 395576d062
6 changed files with 323 additions and 2 deletions

View file

@ -518,7 +518,106 @@ func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (*CustomersHisto
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("%s/customers/history?%s", versionedPrefix, params.Encode()))
data, status, err := c.GetRequest(fmt.Sprintf("%s/orders/history?%s", versionedPrefix, params.Encode()))
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// Pack get method
func (c *Client) Pack(id int) (*PackResponse, int, ErrorResponse) {
var resp PackResponse
data, status, err := c.GetRequest(fmt.Sprintf("%s/orders/packs/%d", versionedPrefix, id))
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// Packs list method
func (c *Client) Packs(parameters PacksRequest) (*PacksResponse, int, ErrorResponse) {
var resp PacksResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("%s/orders/packs?%s", versionedPrefix, params.Encode()))
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// PackCreate method
func (c *Client) PackCreate(pack Pack) (*CreateResponse, int, ErrorResponse) {
var resp CreateResponse
packJson, _ := json.Marshal(&pack)
p := url.Values{
"pack": {string(packJson[:])},
}
data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/packs/create", versionedPrefix), p)
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// PackEdit method
func (c *Client) PackEdit(pack Pack) (*CreateResponse, int, ErrorResponse) {
var resp CreateResponse
packJson, _ := json.Marshal(&pack)
p := url.Values{
"pack": {string(packJson[:])},
}
data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/packs/%d/edit", versionedPrefix, pack.Id), p)
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// PackDelete method
func (c *Client) PackDelete(id int) (*SucessfulResponse, int, ErrorResponse) {
var resp SucessfulResponse
data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/packs/%d/delete", versionedPrefix, id), url.Values{})
if err.ErrorMsg != "" {
return &resp, status, err
}
json.Unmarshal(data, &resp)
return &resp, status, err
}
// PacksHistory method
func (c *Client) PacksHistory(parameters PacksHistoryRequest) (*PacksHistoryResponse, int, ErrorResponse) {
var resp PacksHistoryResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("%s/orders/packs/history?%s", versionedPrefix, params.Encode()))
if err.ErrorMsg != "" {
return &resp, status, err
}

View file

@ -1694,3 +1694,143 @@ func TestClient_StoreEdit(t *testing.T) {
t.Fail()
}
}
func TestClient_PackChange(t *testing.T) {
c := client()
o, status, err := c.OrderCreate(Order{
FirstName: "Понтелей",
LastName: "Турбин",
Patronymic: "Аристархович",
ExternalId: RandomString(8),
Email: fmt.Sprintf("%s@example.com", RandomString(8)),
Items: []OrderItem{{Offer: Offer{Id: 1609}, Quantity: 5}},
})
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status != http.StatusCreated {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if o.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
g, status, err := c.Order(strconv.Itoa(o.Id), "id", "")
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status != http.StatusOK {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if o.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
p, status, err := c.PackCreate(Pack{
Store: "test-store",
ItemId: g.Order.Items[0].Id,
Quantity: 1,
})
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status != http.StatusCreated {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if p.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
e, status, err := c.PackEdit(Pack{Id: p.Id, Quantity: 2})
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status != http.StatusOK {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if e.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
d, status, err := c.PackDelete(p.Id)
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status != http.StatusOK {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if d.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
}
func TestClient_PacksHistory(t *testing.T) {
c := client()
data, status, err := c.PacksHistory(PacksHistoryRequest{Filter: OrdersHistoryFilter{SinceId: 5}})
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status >= http.StatusBadRequest {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if data.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if len(data.History) == 0 {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
}
func TestClient_Packs(t *testing.T) {
c := client()
data, status, err := c.Packs(PacksRequest{Filter: PacksFilter{}, Page: 1})
if err.ErrorMsg != "" {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if status >= http.StatusBadRequest {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
if data.Success != true {
t.Errorf("%v", err.ErrorMsg)
t.Fail()
}
}

View file

@ -212,3 +212,18 @@ type SegmentsFilter struct {
DateFrom string `url:"dateFrom,omitempty"`
DateTo string `url:"dateTo,omitempty"`
}
// PacksFilter type
type PacksFilter struct {
Ids []int `url:"ids,omitempty,brackets"`
Stores []string `url:"stores,omitempty"`
ItemId int `url:"itemId,omitempty"`
OfferXmlId string `url:"offerXmlId,omitempty"`
OfferExternalId string `url:"offerExternalId,omitempty"`
OrderId int `url:"orderId,omitempty"`
OrderExternalId string `url:"orderExternalId,omitempty"`
ShipmentDateFrom string `url:"shipmentDateFrom,omitempty"`
ShipmentDateTo string `url:"shipmentDateTo,omitempty"`
InvoiceNumber string `url:"invoiceNumber,omitempty"`
DeliveryNoteNumber string `url:"deliveryNoteNumber,omitempty"`
}

View file

@ -52,6 +52,20 @@ type OrdersHistoryRequest struct {
Page int `url:"page,omitempty"`
}
// PacksRequest type
type PacksRequest struct {
Filter PacksFilter `url:"filter,omitempty"`
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
}
// PacksHistoryRequest type
type PacksHistoryRequest struct {
Filter OrdersHistoryFilter `url:"filter,omitempty"`
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
}
// UsersRequest type
type UsersRequest struct {
Filter UsersFilter `url:"filter,omitempty"`

View file

@ -108,6 +108,27 @@ type OrdersHistoryResponse struct {
Pagination *Pagination `json:"pagination,omitempty"`
}
// PackResponse type
type PackResponse struct {
Success bool `json:"success"`
Pack *Pack `json:"pack,omitempty,brackets"`
}
// PacksResponse type
type PacksResponse struct {
Success bool `json:"success"`
Pagination *Pagination `json:"pagination,omitempty"`
Packs []Pack `json:"packs,omitempty,brackets"`
}
// PacksHistoryResponse type
type PacksHistoryResponse struct {
Success bool `json:"success,omitempty"`
GeneratedAt string `json:"generatedAt,omitempty"`
History []PacksHistoryRecord `json:"history,omitempty,brackets"`
Pagination *Pagination `json:"pagination,omitempty"`
}
// UserResponse type
type UserResponse struct {
Success bool `json:"success"`

View file

@ -278,7 +278,7 @@ type OrderItem struct {
Status string `json:"status,omitempty"`
Comment string `json:"comment,omitempty"`
IsCanceled bool `json:"isCanceled,omitempty"`
Offer *Offer `json:"offer,omitempty"`
Offer Offer `json:"offer,omitempty"`
Properties []*Property `json:"properties,omitempty,brackets"`
PriceType *PriceType `json:"priceType,omitempty"`
}
@ -296,6 +296,38 @@ type OrdersHistoryRecord struct {
Order *Order `json:"order,omitempty,brackets"`
}
// Pack type
type Pack struct {
Id int `json:"id,omitempty"`
PurchasePrice float32 `json:"purchasePrice,omitempty"`
Quantity float32 `json:"quantity,omitempty"`
Store string `json:"store,omitempty"`
ShipmentDate string `json:"shipmentDate,omitempty"`
InvoiceNumber string `json:"invoiceNumber,omitempty"`
DeliveryNoteNumber string `json:"deliveryNoteNumber,omitempty"`
Item PackItem `json:"item,omitempty"`
ItemId int `json:"itemId,omitempty"`
}
// PackItem type
type PackItem struct {
Id int `json:"id,omitempty"`
Order *Order `json:"order,omitempty"`
Offer *Offer `json:"offer,omitempty"`
}
// PacksHistoryRecord type
type PacksHistoryRecord struct {
Id int `json:"id,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Created bool `json:"created,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Source string `json:"source,omitempty"`
Field string `json:"field,omitempty"`
User *User `json:"user,omitempty,brackets"`
Pack *Pack `json:"pack,omitempty,brackets"`
}
// Offer type
type Offer struct {
Id int `json:"id,omitempty"`