From 67d72f2fe1b90ca82923f3ce8d477d43b639a918 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 29 Dec 2022 16:17:29 +0300 Subject: [PATCH] `/api/v5/notifications/send` support --- client.go | 38 ++++++++++++++++++++++++++++++++++++++ client_test.go | 27 ++++++++++++++++++++++++++- enum.go | 8 ++++++++ request.go | 7 +++++++ types.go | 3 +++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 enum.go diff --git a/client.go b/client.go index 6583b82..d36cb49 100644 --- a/client.go +++ b/client.go @@ -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 +} diff --git a/client_test.go b/client_test.go index db426fa..082d997 100644 --- a/client_test.go +++ b/client_test.go @@ -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]) +} diff --git a/enum.go b/enum.go new file mode 100644 index 0000000..2a1bb2b --- /dev/null +++ b/enum.go @@ -0,0 +1,8 @@ +package retailcrm + +var UserGroupSuperadmins UserGroupType = "superadmins" + +var ( + NotificationTypeError NotificationType = "api.error" + NotificationTypeInfo NotificationType = "api.info" +) diff --git a/request.go b/request.go index 543a115..bd88231 100644 --- a/request.go +++ b/request.go @@ -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 == "" { diff --git a/types.go b/types.go index 1144f38..fab6649 100644 --- a/types.go +++ b/types.go @@ -1431,3 +1431,6 @@ type SerializedLoyalty struct { type ExternalID struct { ExternalID string `json:"externalId,omitempty"` } + +type UserGroupType string +type NotificationType string