admin-push-notification-supplier #1

Merged
sinhluu merged 4 commits from admin-push-notification-supplier into master 2022-11-28 03:41:19 +00:00
4 changed files with 89 additions and 66 deletions

7
const.go Normal file
View File

@ -0,0 +1,7 @@
package notification
const (
SortOrderDesc = "-order"
SortOrderAsc = "order"
SortDefault = "-lastPushAt"
)

View File

@ -24,37 +24,37 @@ func main() {
userID := "61a499ad8d5770f8872b03d8"
requestID, err := c.PushToUsers(notification.PushRequest{
Title: "Notification 1",
Body: "nats stream view notification",
Title: "Notification campaign 111",
Body: "Notification campaign 11",
Data: "{}",
Users: []string{userID},
Label: "tracking-label",
Category: "order",
Label: "tracking-campaign",
Category: "campaign",
})
if err != nil {
fmt.Println("Push err: ", err)
}
fmt.Println("Request id: ", requestID)
total, err := c.CountUnread(notification.CountUnread{
unread, err := c.CountUnread(notification.CountUnread{
User: userID,
Category: "order",
Category: "", // count all if not specify category
})
fmt.Println("Count: ", total, err)
fmt.Println("Count: ", unread, err)
res, err := c.Query(notification.Query{
User: userID,
Category: "order",
Page: 0,
Limit: 20,
Category: "", // get all if not specify category
})
fmt.Println("Query : ", res, err)
total, err = c.CountUnread(notification.CountUnread{
unread, err = c.CountUnread(notification.CountUnread{
User: userID,
Category: "order",
Category: "",
})
fmt.Println("Count: 2", total, err)
fmt.Printf("Count: 2 %+v, %v\n", unread, err)
err = c.Subscribe("test", []string{
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",

View File

@ -10,14 +10,16 @@ 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,omitempty"`
Category string `json:"category,omitempty"`
Sound *Sound `json:"sound,omitempty"`
Link string `json:"link"` // for webpush
Title string `json:"title"`
Body string `json:"body"`
Data string `json:"data"`
Users []string `json:"users"`
Label string `json:"label,omitempty"`
Category string `json:"category,omitempty"`
Sound *Sound `json:"sound,omitempty"`
Link string `json:"link"` // for webpush
Order int `json:"order"` // for sort
DisplayUntil int64 `json:"displayUntil"` // time.Unix
}
// Sound ...
@ -32,34 +34,25 @@ type PushResponse struct {
}
type pushRequest struct {
Version string `json:"version"`
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"`
Sound *Sound `json:"sound,omitempty"`
Link string `json:"link"`
PushRequest
Version string `json:"version"`
APIKey string `json:"apiKey"`
SendBy string `json:"sendBy"`
}
// Query ...
type Query struct {
User string `json:"user"`
Category string `json:"category,omitempty"`
Status string `json:"status"`
Page int64 `json:"page,omitempty"`
Limit int64 `json:"limit,omitempty"`
Sort string `json:"sort,omitempty"` // SortDefault, SortOrderDesc, SortOrderAsc
}
type query struct {
APIKey string `json:"apiKey"`
User string `json:"user"`
Category string `json:"category,omitempty"`
Page int64 `json:"page,omitempty"`
Limit int64 `json:"limit,omitempty"`
APIKey string `json:"apiKey"`
Query
}
// Notification ...
@ -99,13 +92,17 @@ type CountUnread struct {
// CountUnreadResponse ...
type CountUnreadResponse struct {
Total int64 `json:"total"`
List []CategoryCount `json:"list"`
}
type CategoryCount struct {
Category string `json:"category"`
TotalUnread int64 `json:"totalUnread"`
}
type countUnread struct {
APIKey string `json:"apiKey"`
User string `json:"user"`
Category string `json:"category"`
CountUnread
APIKey string `json:"apiKey"`
}
// CommonError ...
@ -123,3 +120,9 @@ type subscribe struct {
Subscribe
APIKey string `json:"apiKey"`
}
// ReadAll ...
type ReadAll struct {
User string `json:"user"`
Category string `json:"category,omitempty"`
}

View File

@ -9,7 +9,7 @@ import (
)
// version specify current version of client
const version = "1.0.2"
const version = "1.0.3"
const (
SendByTopic = "topic"
@ -20,6 +20,7 @@ const (
SubjectPushNotification = "push_notification"
SubjectGetNotification = "get_notification"
SubjectReadNotification = "read_notification"
SubjectReadAllNotification = "read_all_notification"
SubjectCountUnreadNotification = "count_unread_notification"
SubjectSubscribeTopic = "subscribe_topic"
SubjectUnsubscribeTopic = "unsubscribe_topic"
@ -56,17 +57,10 @@ 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{
Version: version,
APIKey: c.Config.APIKey,
Title: payload.Title,
Body: payload.Body,
Data: payload.Data,
SendBy: SendByUsers,
Users: payload.Users,
Label: payload.Label,
Category: payload.Category,
Sound: payload.Sound,
Link: payload.Link,
Version: version,
APIKey: c.Config.APIKey,
SendBy: SendByUsers,
PushRequest: payload,
}
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
if err != nil {
@ -88,11 +82,8 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error)
// Query get list notification by user id
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
p := query{
APIKey: c.Config.APIKey,
User: q.User,
Category: q.Category,
Page: q.Page,
Limit: q.Limit,
APIKey: c.Config.APIKey,
Query: q,
}
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
if err != nil {
@ -112,27 +103,26 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
}
// CountUnread count total unread notification
func (c *Client) CountUnread(q CountUnread) (int64, error) {
func (c *Client) CountUnread(q CountUnread) ([]CategoryCount, error) {
p := countUnread{
APIKey: c.Config.APIKey,
User: q.User,
Category: q.Category,
APIKey: c.Config.APIKey,
CountUnread: q,
}
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
if err != nil {
return 0, err
return nil, err
}
var res struct {
Data CountUnreadResponse `json:"data"`
Error string `json:"error"`
}
if err := json.Unmarshal(msg.Data, &res); err != nil {
return 0, err
if err = json.Unmarshal(msg.Data, &res); err != nil {
return nil, err
}
if res.Error != "" {
return 0, errors.New(res.Error)
return nil, errors.New(res.Error)
}
return res.Data.Total, nil
return res.Data.List, nil
}
// Read mark notification as read
@ -155,6 +145,29 @@ func (c *Client) Read(notificationID string) error {
return err
}
// ReadAll mark notification as read
func (c *Client) ReadAll(r ReadAll) error {
p := struct {
ReadAll
APIKey string `json:"apiKey"`
}{
ReadAll: r,
APIKey: c.Config.APIKey,
}
msg, err := c.natsServer.Request(SubjectReadAllNotification, toBytes(p))
if err != nil {
return err
}
var res CommonError
if err := json.Unmarshal(msg.Data, &res); err != nil {
return err
}
if res.Error != "" {
err = errors.New(res.Error)
}
return err
}
// Subscribe tokens to topic
func (c *Client) Subscribe(topic string, tokens []string) error {
p := subscribe{