Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
139 lines
3.9 KiB
Go
139 lines
3.9 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/api"
|
|
"github.com/cortezaproject/corteza/server/pkg/filter"
|
|
"github.com/cortezaproject/corteza/server/pkg/payload"
|
|
"github.com/cortezaproject/corteza/server/system/rest/request"
|
|
"github.com/cortezaproject/corteza/server/system/service"
|
|
"github.com/cortezaproject/corteza/server/system/types"
|
|
)
|
|
|
|
type (
|
|
Notification struct {
|
|
notification service.NotificationService
|
|
}
|
|
|
|
notificationSetPayload struct {
|
|
Filter types.NotificationFilter `json:"filter"`
|
|
Set types.NotificationSet `json:"set,omitempty"`
|
|
}
|
|
)
|
|
|
|
func (Notification) New() *Notification {
|
|
ctrl := &Notification{}
|
|
ctrl.notification = service.DefaultNotification
|
|
return ctrl
|
|
}
|
|
|
|
func (ctrl *Notification) List(ctx context.Context, r *request.NotificationList) (interface{}, error) {
|
|
var (
|
|
err error
|
|
f = types.NotificationFilter{
|
|
Kind: []types.NotificationKind{types.NotificationKind(r.Kind)},
|
|
Read: filter.State(r.Read),
|
|
Deleted: filter.State(r.Deleted),
|
|
}
|
|
)
|
|
|
|
if f.Paging, err = filter.NewPaging(r.Limit, r.PageCursor); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
f.IncTotal = r.IncTotal
|
|
|
|
if f.Sorting, err = filter.NewSorting(r.Sort); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(r.NotificationID) > 0 {
|
|
f.NotificationID = payload.ParseUint64s(r.NotificationID)
|
|
}
|
|
|
|
set, f, err := ctrl.notification.Find(ctx, f)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Ensure we have a valid empty set rather than nil
|
|
if set == nil {
|
|
set = make(types.NotificationSet, 0)
|
|
}
|
|
|
|
return ctrl.makeFilterPayload(ctx, set, f, nil)
|
|
}
|
|
|
|
func (ctrl *Notification) Create(ctx context.Context, r *request.NotificationCreate) (interface{}, error) {
|
|
ntf := &types.Notification{
|
|
Kind: types.NotificationKind(r.Kind),
|
|
Config: types.NotificationConfig{},
|
|
Recipient: r.Recipient,
|
|
}
|
|
|
|
// Convert sqlxTypes.JSONText to NotificationConfig
|
|
if len(r.Config) > 0 {
|
|
if err := r.Config.Unmarshal(&ntf.Config); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return ctrl.notification.Create(ctx, ntf)
|
|
}
|
|
|
|
func (ctrl *Notification) Update(ctx context.Context, r *request.NotificationUpdate) (interface{}, error) {
|
|
ntf := &types.Notification{
|
|
ID: r.NotificationID,
|
|
Kind: types.NotificationKind(r.Kind),
|
|
Config: types.NotificationConfig{},
|
|
Recipient: r.Recipient,
|
|
}
|
|
|
|
// Convert sqlxTypes.JSONText to NotificationConfig
|
|
if len(r.Config) > 0 {
|
|
if err := r.Config.Unmarshal(&ntf.Config); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return ctrl.notification.Update(ctx, ntf)
|
|
}
|
|
|
|
func (ctrl *Notification) Read(ctx context.Context, r *request.NotificationRead) (interface{}, error) {
|
|
return ctrl.notification.FindByID(ctx, r.NotificationID)
|
|
}
|
|
|
|
func (ctrl *Notification) Delete(ctx context.Context, r *request.NotificationDelete) (interface{}, error) {
|
|
return api.OK(), ctrl.notification.Delete(ctx, r.NotificationID)
|
|
}
|
|
|
|
func (ctrl *Notification) MarkAsRead(ctx context.Context, r *request.NotificationMarkAsRead) (interface{}, error) {
|
|
return api.OK(), ctrl.notification.MarkAsRead(ctx, r.NotificationID)
|
|
}
|
|
|
|
func (ctrl *Notification) MarkAsUnread(ctx context.Context, r *request.NotificationMarkAsUnread) (interface{}, error) {
|
|
return api.OK(), ctrl.notification.MarkAsUnread(ctx, r.NotificationID)
|
|
}
|
|
|
|
func (ctrl *Notification) MarkAllAsRead(ctx context.Context, r *request.NotificationMarkAllAsRead) (interface{}, error) {
|
|
return api.OK(), ctrl.notification.MarkAllAsRead(ctx)
|
|
}
|
|
|
|
func (ctrl *Notification) MarkAllAsUnread(ctx context.Context, r *request.NotificationMarkAllAsUnread) (interface{}, error) {
|
|
return api.OK(), ctrl.notification.MarkAllAsUnread(ctx)
|
|
}
|
|
|
|
func (ctrl *Notification) makeFilterPayload(ctx context.Context, nn types.NotificationSet, f types.NotificationFilter, err error) (*notificationSetPayload, error) {
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Ensure we never return nil, but an empty slice instead
|
|
if nn == nil {
|
|
nn = make(types.NotificationSet, 0)
|
|
}
|
|
|
|
return ¬ificationSetPayload{Filter: f, Set: nn}, nil
|
|
}
|