diff --git a/model.go b/model.go index 0067a86..c6246d1 100644 --- a/model.go +++ b/model.go @@ -50,6 +50,26 @@ type query struct { Limit int64 `json:"limit,omitempty"` } +// Notification ... +type Notification struct { + ID string `json:"id"` + Category string `json:"category,omitempty"` + Title string `json:"title"` + Body string `json:"body"` + IsRead bool `json:"isRead"` + Data string `json:"data,omitempty"` + CreatedAt string `json:"createdAt"` + LastPushAt string `json:"lastPushAt"` +} + +// ListNotificationResponse ... +type ListNotificationResponse struct { + List []Notification `json:"list"` + Total int64 `json:"total"` + Limit int64 `json:"limit"` +} + +// Read ... type Read struct { APIKey string `json:"apiKey"` ID string `json:"id"` diff --git a/notification.go b/notification.go index edc2b3a..f06a5c4 100644 --- a/notification.go +++ b/notification.go @@ -48,7 +48,7 @@ func NewClient(cfg Config) (*Client, error) { return c, nil } -// PushToUsers ... +// PushToUsers push notification to list user id func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) { p := pushRequest{ APIKey: c.Config.APIKey, @@ -73,6 +73,26 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) return res.RequestID, nil } +// 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, + } + msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p)) + if err != nil { + return ListNotificationResponse{}, err + } + var res ListNotificationResponse + if err := json.Unmarshal(msg.Data, &res); err != nil { + return ListNotificationResponse{}, err + } + return res, nil +} + func toBytes(data interface{}) []byte { b, _ := json.Marshal(data) return b