Files
sendico/api/pkg/model/notificationevent.go
Stephan D 29f5a56f21
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
fixed notifications dispatch
2025-11-19 20:15:36 +01:00

97 lines
2.3 KiB
Go

package model
import (
"strings"
"github.com/tech/sendico/pkg/merrors"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
)
type NotificationEvent interface {
GetType() mservice.Type
GetAction() nm.NotificationAction
ToString() string
StringType() string
StringAction() string
}
type NotificationEventImp struct {
nType mservice.Type
nAction nm.NotificationAction
}
func (ne *NotificationEventImp) GetType() mservice.Type {
return ne.nType
}
func (ne *NotificationEventImp) GetAction() nm.NotificationAction {
return ne.nAction
}
const messageDelimiter string = "_"
func (ne *NotificationEventImp) Equals(other *NotificationEventImp) bool {
return (other != nil) && (ne.nType == other.nType) && (ne.nAction == other.nAction)
}
func (ne *NotificationEventImp) ToString() string {
return ne.StringType() + messageDelimiter + ne.StringAction()
}
func (ne *NotificationEventImp) StringType() string {
return string(ne.nType)
}
func (ne *NotificationEventImp) StringAction() string {
return string(ne.nAction)
}
func NewNotification(t mservice.Type, a nm.NotificationAction) NotificationEvent {
return &NotificationEventImp{nType: t, nAction: a}
}
func FromString(s string) (*NotificationEventImp, error) {
parts := strings.Split(s, messageDelimiter)
if len(parts) != 2 {
return nil, merrors.Internal("invalid_notification_event_format")
}
res := &NotificationEventImp{}
var err error
if res.nType, err = mservice.StringToSType(parts[0]); err != nil {
return nil, err
}
if res.nAction, err = StringToNotificationAction(parts[1]); err != nil {
return nil, err
}
return res, nil
}
func StringToNotificationAction(s string) (nm.NotificationAction, error) {
switch nm.NotificationAction(s) {
case nm.NACreated,
nm.NAPending,
nm.NAUpdated,
nm.NADeleted,
nm.NAAssigned,
nm.NAPasswordReset:
return nm.NotificationAction(s), nil
default:
return "", merrors.DataConflict("invalid Notification action: " + s)
}
}
func StringToNotificationEvent(eventType, eventAction string) (NotificationEvent, error) {
et, err := mservice.StringToSType(eventType)
if err != nil {
return nil, err
}
ea, err := StringToNotificationAction(eventAction)
if err != nil {
return nil, err
}
return NewNotification(et, ea), nil
}