get list notification by user

This commit is contained in:
Sinh 2021-12-03 11:09:25 +07:00
parent b9ce18f0f4
commit 94cd3a171a
2 changed files with 41 additions and 1 deletions

View File

@ -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"`

View File

@ -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