From 282a94a2efacc59b93437c593e858b4fbd5c1528 Mon Sep 17 00:00:00 2001 From: Sinh Date: Fri, 3 Dec 2021 11:58:55 +0700 Subject: [PATCH] update notification payload and add demo --- example/main.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ model.go | 28 +++++++++++++----------- notification.go | 15 +++++++------ 3 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 example/main.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..fb1cd1f --- /dev/null +++ b/example/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + + "github.com/Selly-Modules/natsio" + + "github.com/Selly-Modules/notification" +) + +func main() { + c, err := notification.NewClient(notification.Config{ + APIKey: "UWvieSs2erFfhwvl1g8CavEY2V7ouc3", + Nats: natsio.Config{ + URL: "127.0.0.1:4222", + User: "", + Password: "", + TLS: nil, + }, + }) + if err != nil { + panic(err) + } + + userID := "61a499ad8d5770f8872b03d8" + requestID, err := c.PushToUsers(notification.PushRequest{ + Title: "Notification 1", + Body: "nats stream view notification", + Data: "{}", + Users: []string{userID}, + Label: "tracking-label", + Category: "order", + }) + if err != nil { + fmt.Println("Push err: ", err) + } + fmt.Println("Request id: ", requestID) + + total, err := c.CountUnread(notification.CountUnread{ + User: userID, + Category: "order", + }) + fmt.Println("Count: ", total, err) + + res, err := c.Query(notification.Query{ + User: userID, + Category: "order", + Page: 0, + Limit: 20, + }) + fmt.Println("Query : ", res, err) + + total, err = c.CountUnread(notification.CountUnread{ + User: userID, + Category: "order", + }) + fmt.Println("Count: 2", total, err) +} diff --git a/model.go b/model.go index 1eb9fd7..8b33b37 100644 --- a/model.go +++ b/model.go @@ -10,11 +10,12 @@ type Config struct { // PushRequest ... type PushRequest struct { - Title string `json:"title"` - Body string `json:"body"` - Data string `json:"data"` - Users []string `json:"users"` - Label string `json:"label"` + Title string `json:"title"` + Body string `json:"body"` + Data string `json:"data"` + Users []string `json:"users"` + Label string `json:"label"` + Category string `json:"category"` } // PushResponse ... @@ -24,14 +25,15 @@ type PushResponse struct { } type pushRequest struct { - APIKey string `json:"apiKey"` - Title string `json:"title"` - Body string `json:"body"` - Data string `json:"data"` - SendBy string `json:"sendBy"` - Users []string `json:"users"` - Topic string `json:"topic"` - Label string `json:"label"` + APIKey string `json:"apiKey"` + Title string `json:"title"` + Body string `json:"body"` + Data string `json:"data"` + SendBy string `json:"sendBy"` + Users []string `json:"users"` + Topic string `json:"topic"` + Label string `json:"label"` + Category string `json:"category"` } // Query ... diff --git a/notification.go b/notification.go index 9e19f1d..8dccd4b 100644 --- a/notification.go +++ b/notification.go @@ -51,13 +51,14 @@ func NewClient(cfg Config) (*Client, error) { // PushToUsers push notification to list user id func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) { p := pushRequest{ - APIKey: c.Config.APIKey, - Title: payload.Title, - Body: payload.Body, - Data: payload.Data, - SendBy: SendByUsers, - Users: payload.Users, - Label: payload.Label, + APIKey: c.Config.APIKey, + Title: payload.Title, + Body: payload.Body, + Data: payload.Data, + SendBy: SendByUsers, + Users: payload.Users, + Label: payload.Label, + Category: payload.Category, } msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p)) if err != nil {