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 {