Merge pull request #23 from iyzoer/master

Add units
This commit is contained in:
Alex Lushpai 2019-02-20 10:52:26 +03:00 committed by GitHub
commit 129372638a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 170 additions and 0 deletions

5
.env.dist Normal file
View File

@ -0,0 +1,5 @@
RETAILCRM_URL=""
RETAILCRM_KEY=""
RETAILCRM_USER=""
RETAILCRM_SITE=""
RETAILCRM_VERSION=""

View File

@ -7,4 +7,5 @@ go:
before_install: before_install:
- go get -v github.com/google/go-querystring/query - go get -v github.com/google/go-querystring/query
- go get -v github.com/h2non/gock - go get -v github.com/h2non/gock
- cp .env.dist .env
script: go test -v ./... script: go test -v ./...

View File

@ -2779,6 +2779,52 @@ func (c *Client) StoreEdit(store Store) (SuccessfulResponse, int, errs.Failure)
return resp, status, err 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 // Segments returns segments
// //
// For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-segments // For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-segments

View File

@ -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) { func TestClient_PackChange(t *testing.T) {
c := client() c := client()
defer gock.Off() defer gock.Off()

View File

@ -398,3 +398,9 @@ type CustomFieldResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
CustomField CustomFields `json:"customField,omitempty,brackets"` CustomField CustomFields `json:"customField,omitempty,brackets"`
} }
// UnitsResponse type
type UnitsResponse struct {
Success bool `json:"success"`
Units *[]Unit `json:"units,omitempty,brackets"`
}

View File

@ -318,6 +318,7 @@ type Pack struct {
DeliveryNoteNumber string `json:"deliveryNoteNumber,omitempty"` DeliveryNoteNumber string `json:"deliveryNoteNumber,omitempty"`
Item *PackItem `json:"item,omitempty"` Item *PackItem `json:"item,omitempty"`
ItemID int `json:"itemId,omitempty"` ItemID int `json:"itemId,omitempty"`
Unit *Unit `json:"unit,omitempty"`
} }
// PackItem type // PackItem type
@ -358,6 +359,7 @@ type Offer struct {
Properties map[string]string `json:"properties,omitempty,brackets"` Properties map[string]string `json:"properties,omitempty,brackets"`
Prices []OfferPrice `json:"prices,omitempty,brackets"` Prices []OfferPrice `json:"prices,omitempty,brackets"`
Images []string `json:"images,omitempty,brackets"` Images []string `json:"images,omitempty,brackets"`
Unit *Unit `json:"unit,omitempty,brackets"`
} }
// Inventory type // Inventory type
@ -404,6 +406,15 @@ type PriceUpload struct {
Price float32 `json:"price,omitempty"` 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 User related types
*/ */