mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-24 05:46:05 +03:00
Product units
This commit is contained in:
parent
6d9a068d65
commit
eaeabeac6e
5
.env.dist
Normal file
5
.env.dist
Normal file
@ -0,0 +1,5 @@
|
||||
RETAILCRM_URL=""
|
||||
RETAILCRM_KEY=""
|
||||
RETAILCRM_USER=""
|
||||
RETAILCRM_SITE=""
|
||||
RETAILCRM_VERSION=""
|
@ -7,4 +7,5 @@ go:
|
||||
before_install:
|
||||
- go get -v github.com/google/go-querystring/query
|
||||
- go get -v github.com/h2non/gock
|
||||
- cp .env.dist .env
|
||||
script: go test -v ./...
|
||||
|
46
v5/client.go
46
v5/client.go
@ -2779,6 +2779,52 @@ func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, errs.Failure)
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
// Units returns units list
|
||||
//
|
||||
// For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-reference-units
|
||||
func (c *Client) Units() (UnitsResponse, int, errs.Failure) {
|
||||
var resp UnitsResponse
|
||||
|
||||
data, status, err := c.GetRequest("/reference/units")
|
||||
if err.RuntimeErr != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp.Success == false {
|
||||
return resp, status, buildErr(data)
|
||||
}
|
||||
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
// UnitEdit unit create/edit
|
||||
//
|
||||
// For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#post--api-v5-reference-units-code-edit
|
||||
func (c *Client) UnitEdit(unit Unit) (SuccessfulResponse, int, errs.Failure) {
|
||||
var resp SuccessfulResponse
|
||||
|
||||
objJSON, _ := json.Marshal(&unit)
|
||||
|
||||
p := url.Values{
|
||||
"unit": {string(objJSON[:])},
|
||||
}
|
||||
|
||||
data, status, err := c.PostRequest(fmt.Sprintf("/reference/units/%s/edit", unit.Code), p)
|
||||
if err.RuntimeErr != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp.Success == false {
|
||||
return resp, status, buildErr(data)
|
||||
}
|
||||
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
// Segments returns segments
|
||||
//
|
||||
// For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-segments
|
||||
|
@ -3448,6 +3448,107 @@ func TestClient_StoreEdit_Fail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_Units(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
defer gock.Off()
|
||||
|
||||
gock.New(crmURL).
|
||||
Get("/api/v5/reference/units").
|
||||
Reply(200).
|
||||
BodyString(`{"success": true, "units": []}`)
|
||||
|
||||
data, st, err := c.Units()
|
||||
if err.RuntimeErr != nil {
|
||||
t.Errorf("%v", err.Error())
|
||||
}
|
||||
|
||||
if st != http.StatusOK {
|
||||
t.Errorf("%v", err.ApiError())
|
||||
}
|
||||
|
||||
if data.Success != true {
|
||||
t.Errorf("%v", err.ApiError())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_UnitsEdit(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
defer gock.Off()
|
||||
|
||||
unit := Unit{
|
||||
Code: RandomString(5),
|
||||
Name: RandomString(5),
|
||||
Sym: RandomString(2),
|
||||
Default: false,
|
||||
Active: true,
|
||||
}
|
||||
|
||||
jr, _ := json.Marshal(&unit)
|
||||
|
||||
p := url.Values{
|
||||
"unit": {string(jr[:])},
|
||||
}
|
||||
|
||||
gock.New(crmURL).
|
||||
Post(fmt.Sprintf("/api/v5/reference/units/%s/edit", unit.Code)).
|
||||
MatchType("url").
|
||||
BodyString(p.Encode()).
|
||||
Reply(201).
|
||||
BodyString(`{"success": true}`)
|
||||
|
||||
data, st, err := c.UnitEdit(unit)
|
||||
if err.RuntimeErr != nil {
|
||||
t.Errorf("%v", err.Error())
|
||||
}
|
||||
|
||||
if !statuses[st] {
|
||||
t.Errorf("%v", err.ApiError())
|
||||
}
|
||||
|
||||
if data.Success != true {
|
||||
t.Errorf("%v", err.ApiError())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_UnitEdit_Fail(t *testing.T) {
|
||||
c := client()
|
||||
|
||||
defer gock.Off()
|
||||
|
||||
unit := Unit{
|
||||
Name: RandomString(5),
|
||||
Active: false,
|
||||
}
|
||||
|
||||
jr, _ := json.Marshal(&unit)
|
||||
|
||||
p := url.Values{
|
||||
"store": {string(jr[:])},
|
||||
}
|
||||
|
||||
gock.New(crmURL).
|
||||
Post(fmt.Sprintf("/api/v5/reference/units/%s/edit", unit.Code)).
|
||||
MatchType("url").
|
||||
BodyString(p.Encode()).
|
||||
Reply(404).
|
||||
BodyString(`{"success": false, "errorMsg": "API method not found"}`)
|
||||
|
||||
data, st, err := c.UnitEdit(unit)
|
||||
if err.RuntimeErr != nil {
|
||||
t.Errorf("%v", err.Error())
|
||||
}
|
||||
|
||||
if st < http.StatusBadRequest {
|
||||
t.Error(statusFail)
|
||||
}
|
||||
|
||||
if data.Success != false {
|
||||
t.Error(successFail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_PackChange(t *testing.T) {
|
||||
c := client()
|
||||
defer gock.Off()
|
||||
|
@ -398,3 +398,9 @@ type CustomFieldResponse struct {
|
||||
Success bool `json:"success"`
|
||||
CustomField CustomFields `json:"customField,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// UnitsResponse type
|
||||
type UnitsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Units *[]Unit `json:"units,omitempty,brackets"`
|
||||
}
|
||||
|
11
v5/types.go
11
v5/types.go
@ -318,6 +318,7 @@ type Pack struct {
|
||||
DeliveryNoteNumber string `json:"deliveryNoteNumber,omitempty"`
|
||||
Item *PackItem `json:"item,omitempty"`
|
||||
ItemID int `json:"itemId,omitempty"`
|
||||
Unit *Unit `json:"unit,omitempty"`
|
||||
}
|
||||
|
||||
// PackItem type
|
||||
@ -358,6 +359,7 @@ type Offer struct {
|
||||
Properties map[string]string `json:"properties,omitempty,brackets"`
|
||||
Prices []OfferPrice `json:"prices,omitempty,brackets"`
|
||||
Images []string `json:"images,omitempty,brackets"`
|
||||
Unit *Unit `json:"unit,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// Inventory type
|
||||
@ -404,6 +406,15 @@ type PriceUpload struct {
|
||||
Price float32 `json:"price,omitempty"`
|
||||
}
|
||||
|
||||
// Unit type
|
||||
type Unit struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Sym string `json:"sym"`
|
||||
Default bool `json:"default,omitempty"`
|
||||
Active bool `json:"active,omitempty"`
|
||||
}
|
||||
|
||||
/**
|
||||
User related types
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user