mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-21 20:36:03 +03:00
/api/v5/notifications/send support
This commit is contained in:
commit
06b0395a93
38
client.go
38
client.go
@ -6493,3 +6493,41 @@ func (c *Client) GetOrderPlate(by, orderID, site string, plateID int) (io.ReadCl
|
|||||||
|
|
||||||
return reader, resp.StatusCode, nil
|
return reader, resp.StatusCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotificationsSend send a notification
|
||||||
|
//
|
||||||
|
// For more information see https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-notifications-send
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// var client = retailcrm.New("https://demo.url", "09jIJ")
|
||||||
|
//
|
||||||
|
// req := retailcrm.NotificationsSendRequest{
|
||||||
|
// UserGroups: []retailcrm.UserGroupType{retailcrm.UserGroupSuperadmins},
|
||||||
|
// Type: retailcrm.NotificationTypeInfo,
|
||||||
|
// Message: "Hello everyone!",
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// status, err := client.NotificationsSend(req)
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// if apiErr, ok := retailcrm.AsAPIError(err); ok {
|
||||||
|
// log.Fatalf("http status: %d, %s", status, apiErr.String())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// log.Fatalf("http status: %d, error: %s", status, err)
|
||||||
|
// }
|
||||||
|
func (c *Client) NotificationsSend(req NotificationsSendRequest) (int, error) {
|
||||||
|
marshaled, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, status, err := c.PostRequest("/notifications/send",
|
||||||
|
url.Values{"notification": {string(marshaled)}})
|
||||||
|
if err != nil {
|
||||||
|
return status, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
|
@ -3,7 +3,6 @@ package retailcrm
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/go-querystring/query"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -16,6 +15,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-querystring/query"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@ -7723,3 +7724,27 @@ func TestClient_GetOrderPlateFail(t *testing.T) {
|
|||||||
assert.Equal(t, status, http.StatusNotFound)
|
assert.Equal(t, status, http.StatusNotFound)
|
||||||
assert.Equal(t, "Not found", err.(APIError).Error()) //nolint:errorlint
|
assert.Equal(t, "Not found", err.(APIError).Error()) //nolint:errorlint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient_NotificationsSend(t *testing.T) {
|
||||||
|
defer gock.Off()
|
||||||
|
req := NotificationsSendRequest{
|
||||||
|
UserGroups: []UserGroupType{UserGroupSuperadmins},
|
||||||
|
Type: NotificationTypeInfo,
|
||||||
|
Message: "Hello everyone!",
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gock.New(crmURL).
|
||||||
|
Post(prefix + "/notifications/send").
|
||||||
|
BodyString(url.Values{"notification": {string(data)}}.Encode()).
|
||||||
|
Reply(http.StatusOK).
|
||||||
|
JSON(`{"success":true}`)
|
||||||
|
|
||||||
|
status, err := client().NotificationsSend(req)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, statuses[status])
|
||||||
|
}
|
||||||
|
8
enum.go
Normal file
8
enum.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package retailcrm
|
||||||
|
|
||||||
|
var UserGroupSuperadmins UserGroupType = "superadmins"
|
||||||
|
|
||||||
|
var (
|
||||||
|
NotificationTypeError NotificationType = "api.error"
|
||||||
|
NotificationTypeInfo NotificationType = "api.info"
|
||||||
|
)
|
@ -277,6 +277,13 @@ type LoyaltiesRequest struct {
|
|||||||
Filter LoyaltyAPIFilter `url:"filter,omitempty"`
|
Filter LoyaltyAPIFilter `url:"filter,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotificationsSendRequest struct {
|
||||||
|
UserGroups []UserGroupType `json:"userGroups,omitempty"`
|
||||||
|
Type NotificationType `json:"type"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
UserIDs []string `json:"userIds,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// SystemURL returns system URL from the connection request without trailing slash.
|
// SystemURL returns system URL from the connection request without trailing slash.
|
||||||
func (r ConnectRequest) SystemURL() string {
|
func (r ConnectRequest) SystemURL() string {
|
||||||
if r.URL == "" {
|
if r.URL == "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user