add method for adding product to promotion

This commit is contained in:
diPhantxm
2023-03-16 19:51:25 +03:00
parent 12773269d4
commit 2c95f2e4c2
3 changed files with 111 additions and 1 deletions

View File

@@ -40,7 +40,7 @@
- [x] Available promotions
- [ ] Products that can participate in a promotion
- [ ] Products in a promotion
- [ ] Add products to promotion
- [x] Add products to promotion
- [ ] Remove products from promotion
- [ ] List of available Hot Sale promotions
- [ ] List of products participating in the Hot Sale promotion

View File

@@ -69,6 +69,7 @@ type GetAvailablePromotionsResponse struct {
} `json:"result"`
}
// A method for getting a list of promotions that you can participate in
func (c Promotions) GetAvailablePromotions() (*GetAvailablePromotionsResponse, error) {
url := "/v1/actions"
@@ -82,3 +83,56 @@ func (c Promotions) GetAvailablePromotions() (*GetAvailablePromotionsResponse, e
return resp, nil
}
type AddProductToPromotionParams struct {
// Promotion identifier
ActionId float64 `json:"action_id"`
// Products list
Products []AddProductToPromotionProduct `json:"products"`
}
type AddProductToPromotionProduct struct {
// Product identifier
ProductId float64 `json:"produt_id"`
// Promotional product price
ActionPrice float64 `json:"action_price"`
// Number of product units in a stock discount type promotion
Stock float64 `json:"stock"`
}
type AddProductToPromotionResponse struct {
core.CommonResponse
// Method result
Result struct {
// List of product identifiers that were added to the promotion
ProductIds []float64 `json:"product_ids"`
// List of products that weren't added to the promotion
Rejected []struct {
// Product identifier
ProductId float64 `json:"product_id"`
// Reason why the product wasn't added to the promotion
Reason string `json:"reason"`
} `json:"rejected"`
} `json:"result"`
}
// A method for adding products to an available promotion
func (c Promotions) AddToPromotion(params *AddProductToPromotionParams) (*AddProductToPromotionResponse, error) {
url := "/v1/actions/products/activate"
resp := &AddProductToPromotionResponse{}
response, err := c.client.Request(http.MethodGet, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}

View File

@@ -64,3 +64,59 @@ func TestGetAvailablePromotions(t *testing.T) {
}
}
}
func TestAddToPromotion(t *testing.T) {
tests := []struct {
statusCode int
headers map[string]string
params *AddProductToPromotionParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&AddProductToPromotionParams{
ActionId: 60564,
Products: []AddProductToPromotionProduct{
{
ProductId: 1389,
ActionPrice: 356,
Stock: 10,
},
},
},
`{
"result": {
"product_ids": [
1389
],
"rejected": []
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&AddProductToPromotionParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Promotions().AddToPromotion(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}