diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..805f069 --- /dev/null +++ b/.env.dist @@ -0,0 +1,5 @@ +RETAILCRM_URL="" +RETAILCRM_KEY="" +RETAILCRM_USER="" +RETAILCRM_SITE="" +RETAILCRM_VERSION="" \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 22da27f..5d551ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 ./... diff --git a/v5/client.go b/v5/client.go index d8ce064..532fe97 100644 --- a/v5/client.go +++ b/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 diff --git a/v5/client_test.go b/v5/client_test.go index fbb4f37..dbb4301 100644 --- a/v5/client_test.go +++ b/v5/client_test.go @@ -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() diff --git a/v5/response.go b/v5/response.go index e68c0cd..f0c3457 100644 --- a/v5/response.go +++ b/v5/response.go @@ -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"` +} diff --git a/v5/types.go b/v5/types.go index 3fc6cab..410cca2 100644 --- a/v5/types.go +++ b/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 */