Skip to content

Commit

Permalink
Changed notifications & user.login as main unique id for users
Browse files Browse the repository at this point in the history
  • Loading branch information
SchawnnDev committed Dec 5, 2023
1 parent 0ec576e commit b80260b
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 30 deletions.
5 changes: 3 additions & 2 deletions internals/handlers/notifier_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"github.com/google/uuid"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/export"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/notifier/notification"
"net/http"
"time"

Expand Down Expand Up @@ -45,14 +46,14 @@ func NotificationsWSRegister(w http.ResponseWriter, r *http.Request) {
zap.L().Error("Add new WS Client to manager", zap.Error(err))
return
}
go func(client *notifier.WebsocketClient) {
go func(client *notifier.WebsocketClient) { // temporary for tests
zap.L().Info("starting notifier")
ticker := time.NewTicker(1 * time.Second)
after := time.After(30 * time.Second)
for {
select {
case <-ticker.C:
notifier.C().SendToUsers(ExportNotification{Status: export.StatusPending, Export: export.WrapperItem{Id: uuid.New().String(), FileName: "test.bla"}}, []uuid.UUID{user.ID})
notifier.C().SendToUsers(notification.ExportNotification{Status: export.StatusPending, Export: export.WrapperItem{Id: uuid.New().String(), FileName: "test.bla"}}, []users.UserWithPermissions{user})
zap.L().Info("send notification")
case <-after:
return
Expand Down
22 changes: 21 additions & 1 deletion internals/notifier/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Notification interface {
ToBytes() ([]byte, error)
NewInstance(id int64, data []byte, isRead bool) (Notification, error)
Equals(notification Notification) bool
}

// BaseNotification data structure represents a basic notification and her current state
Expand All @@ -28,13 +29,32 @@ func (n BaseNotification) NewInstance(id int64, data []byte, isRead bool) (Notif
notification.Id = id
notification.IsRead = isRead
notification.Notification = notification
return &notification, nil
return notification, nil
}

// ToBytes convert a notification in a json byte slice to be sent though any required channel
func (n BaseNotification) ToBytes() ([]byte, error) {
b, err := json.Marshal(n)
if err != nil {
return nil, err
}
return b, nil
}

// Equals returns true if the two notifications are equals
func (n BaseNotification) Equals(notification Notification) bool {
notif, ok := notification.(BaseNotification)
if !ok {
return ok
}
if n.Id != notif.Id {
return false
}
if n.IsRead != notif.IsRead {
return false
}
if n.Type != notif.Type {
return false
}
return true
}
21 changes: 20 additions & 1 deletion internals/notifier/notification/notification_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notification
import (
"encoding/json"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/export"
"reflect"
)

type ExportNotification struct {
Expand Down Expand Up @@ -40,5 +41,23 @@ func (e ExportNotification) NewInstance(id int64, data []byte, isRead bool) (Not
notification.Id = id
notification.IsRead = isRead
notification.Notification = notification
return &notification, nil
return notification, nil
}

// Equals returns true if the two notifications are equals
func (e ExportNotification) Equals(notification Notification) bool {
notif, ok := notification.(ExportNotification)
if !ok {
return ok
}
if !notif.BaseNotification.Equals(e.BaseNotification) {
return false
}
if !reflect.DeepEqual(notif.Export, e.Export) {
return false
}
if notif.Status != e.Status {
return false
}
return true
}
81 changes: 71 additions & 10 deletions internals/notifier/notification/notification_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
// MockNotification is an implementation of a notification main type
type MockNotification struct {
BaseNotification
ID int64 `json:"id"`
Type string `json:"type"`
CreationDate time.Time `json:"creationDate"`
Groups []int64 `json:"groups"`
Level string `json:"level"`
Expand All @@ -20,18 +18,21 @@ type MockNotification struct {
}

// NewMockNotification renders a new MockNotification instance
func NewMockNotification(level string, title string, subTitle string, description string, creationDate time.Time,
func NewMockNotification(id int64, level string, title string, subTitle string, description string, creationDate time.Time,
groups []int64, context map[string]interface{}) *MockNotification {

return &MockNotification{
Type: "mock",
BaseNotification: BaseNotification{
Id: id,
Type: "MockNotification",
},
CreationDate: creationDate,
// Groups: groups,
Level: level,
Title: title,
SubTitle: subTitle,
Description: description,
Context: context,
Groups: groups,
Level: level,
Title: title,
SubTitle: subTitle,
Description: description,
Context: context,
}
}

Expand All @@ -43,3 +44,63 @@ func (n MockNotification) ToBytes() ([]byte, error) {
}
return b, nil
}

// NewInstance returns a new instance of a MockNotification
func (n MockNotification) NewInstance(id int64, data []byte, isRead bool) (Notification, error) {
var notification MockNotification
err := json.Unmarshal(data, &notification)
if err != nil {
return nil, err
}
notification.Id = id
notification.IsRead = isRead
notification.Notification = notification
return notification, nil
}

// Equals returns true if the two notifications are equals
func (n MockNotification) Equals(notification Notification) bool {
notif, ok := notification.(MockNotification)
if !ok {
return ok
}
if !notif.BaseNotification.Equals(n.BaseNotification) {
return false
}
if notif.CreationDate != n.CreationDate {
return false
}
if notif.Level != n.Level {
return false
}
if notif.Title != n.Title {
return false
}
if notif.SubTitle != n.SubTitle {
return false
}
if notif.Description != n.Description {
return false
}
if notif.Context != nil && n.Context != nil {
if len(notif.Context) != len(n.Context) {
return false
}
for k, v := range notif.Context {
if n.Context[k] != v {
return false
}
}
} else if notif.Context != nil || n.Context != nil {
return false
}
if len(notif.Groups) != len(n.Groups) {
return false
}
for i, v := range notif.Groups {
if n.Groups[i] != v {
return false
}
}
return true
}
Loading

0 comments on commit b80260b

Please sign in to comment.