forked from ebhomengo/niki
Merge pull request 'feature/notification' (#263) from feature/notification into develop
Reviewed-on: ebhomengo/niki#263 Reviewed-by: hossein <h.nazari1990@gmail.com>
This commit is contained in:
commit
2dfbeaf58e
|
|
@ -0,0 +1,8 @@
|
|||
package entity
|
||||
|
||||
type Channel struct {
|
||||
ID int8
|
||||
Type NotificationType
|
||||
Provider string
|
||||
Config string
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package entity
|
||||
|
||||
type Notification struct {
|
||||
ID int8
|
||||
Type NotificationType
|
||||
Recipinet string
|
||||
Body string
|
||||
Status NotificationStatus
|
||||
}
|
||||
|
||||
type NotificationType uint8
|
||||
|
||||
const (
|
||||
Email NotificationType = iota + 1
|
||||
SMS
|
||||
Push
|
||||
)
|
||||
|
||||
func (t NotificationType) String() string {
|
||||
switch t {
|
||||
case Email:
|
||||
return "Email"
|
||||
case SMS:
|
||||
return "SMS"
|
||||
case Push:
|
||||
return "Push"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
type NotificationStatus uint8
|
||||
|
||||
const (
|
||||
Pending NotificationStatus = iota + 1
|
||||
Success
|
||||
Failed
|
||||
)
|
||||
|
||||
func (t NotificationStatus) String() string {
|
||||
switch t {
|
||||
case Pending:
|
||||
return "Pending"
|
||||
case Success:
|
||||
return "Success"
|
||||
case Failed:
|
||||
return "Failed"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package messagebroker
|
||||
|
||||
import "git.gocasts.ir/ebhomengo/niki/domain/notification/entity"
|
||||
|
||||
type redis struct {
|
||||
}
|
||||
|
||||
func (r *redis) AddItem(notification entity.Notification) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *redis) RemoveItem(notification entity.Notification) error {
|
||||
return nil
|
||||
}
|
||||
func (r *redis) HealthCheck() error {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
_ "time"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/domain/notification/entity"
|
||||
)
|
||||
|
||||
type Notifservice struct {
|
||||
MessageBroker MessageBroker
|
||||
Repository Repository
|
||||
Channel Channel
|
||||
}
|
||||
|
||||
type MessageBroker interface {
|
||||
AddItem(notification entity.Notification) error
|
||||
RemoveItem(notification entity.Notification) error
|
||||
HealthCheck() error
|
||||
}
|
||||
|
||||
type Repository interface {
|
||||
AddItem(notification entity.Notification) error
|
||||
}
|
||||
|
||||
type Channel interface {
|
||||
SendMessage(notification entity.Notification) error
|
||||
}
|
||||
type NotificationServiceRequest struct {
|
||||
Body string
|
||||
Type entity.NotificationType
|
||||
Recipinet string
|
||||
}
|
||||
|
||||
func (n *Notifservice) NewNotification(r NotificationServiceRequest) *entity.Notification {
|
||||
// TODO add validation of notification properties
|
||||
return &entity.Notification{
|
||||
Type: r.Type,
|
||||
Recipinet: r.Recipinet,
|
||||
Body: r.Body,
|
||||
Status: entity.Pending,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifservice) Send(notification entity.Notification) error {
|
||||
if err := n.Channel.SendMessage(notification); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notifservice) Add(notification *entity.Notification) error {
|
||||
err := n.MessageBroker.AddItem(*notification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notifservice) Archive(notification *entity.Notification) error {
|
||||
if err := n.MessageBroker.RemoveItem(*notification); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := n.Repository.AddItem(*notification); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package service
|
||||
|
||||
import "git.gocasts.ir/ebhomengo/niki/domain/notification/entity"
|
||||
|
||||
type sender interface {
|
||||
Send(notification entity.Notification) error
|
||||
}
|
||||
Loading…
Reference in New Issue