/api/v5/notifications/send support

This commit is contained in:
Pavel 2022-12-29 16:17:29 +03:00
parent 6c5eb72848
commit 67d72f2fe1
5 changed files with 82 additions and 1 deletions

View File

@ -6493,3 +6493,41 @@ func (c *Client) GetOrderPlate(by, orderID, site string, plateID int) (io.ReadCl
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
}

View File

@ -3,7 +3,6 @@ package retailcrm
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"io/ioutil"
"log"
"math/rand"
@ -16,6 +15,8 @@ import (
"testing"
"time"
"github.com/google/go-querystring/query"
"github.com/stretchr/testify/assert"
"github.com/joho/godotenv"
@ -7723,3 +7724,27 @@ func TestClient_GetOrderPlateFail(t *testing.T) {
assert.Equal(t, status, http.StatusNotFound)
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
View File

@ -0,0 +1,8 @@
package retailcrm
var UserGroupSuperadmins UserGroupType = "superadmins"
var (
NotificationTypeError NotificationType = "api.error"
NotificationTypeInfo NotificationType = "api.info"
)

View File

@ -277,6 +277,13 @@ type LoyaltiesRequest struct {
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.
func (r ConnectRequest) SystemURL() string {
if r.URL == "" {

View File

@ -1431,3 +1431,6 @@ type SerializedLoyalty struct {
type ExternalID struct {
ExternalID string `json:"externalId,omitempty"`
}
type UserGroupType string
type NotificationType string