Enhance Notification Request Structure and Service Interface
- Updated the NotificationRequest struct to include new fields: Title, Type, Priority, and ScheduledAt, improving the flexibility and usability of notification requests. - Implemented validation for the new ScheduledAt field to ensure it is a future timestamp or zero for immediate delivery. - Refactored the NotificationService interface and SDK methods to accept a new Model struct, streamlining the parameters for sending notifications. - Enhanced the builder pattern for constructing notification requests, adding methods for setting Title, Type, Priority, and ScheduledAt, promoting a fluent API design. - Updated service methods for sending notifications (Email, SMS, Push, OTP) to utilize the new Model struct, ensuring consistency across notification types.
This commit is contained in:
@@ -24,10 +24,14 @@ type NotificationMethods struct {
|
|||||||
|
|
||||||
// NotificationRequest represents the request structure for sending notifications
|
// NotificationRequest represents the request structure for sending notifications
|
||||||
type NotificationRequest struct {
|
type NotificationRequest struct {
|
||||||
EventType EventType `json:"event_type" valid:"required,in(EMAIL|SMS|PUSH|OTP)"`
|
UserID string `json:"user_id,omitempty" valid:"optional"`
|
||||||
Message string `json:"message" valid:"required,length(1|1000)"`
|
Title string `json:"title" valid:"required"`
|
||||||
Methods NotificationMethods `json:"methods" valid:"required"`
|
Message string `json:"message" valid:"required,length(1|1000)"`
|
||||||
UserID string `json:"user_id,omitempty" valid:"optional"`
|
Type string `json:"type" valid:"required,in(info|warning|alert|reject)"`
|
||||||
|
EventType EventType `json:"event_type" valid:"required,in(EMAIL|SMS|PUSH|OTP)"`
|
||||||
|
Priority string `json:"priority" valid:"required,in(low|medium|important)"`
|
||||||
|
ScheduledAt int64 `json:"scheduled_at"`
|
||||||
|
Methods NotificationMethods `json:"methods" valid:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotificationResponse represents the successful response from notification service
|
// NotificationResponse represents the successful response from notification service
|
||||||
@@ -67,6 +71,11 @@ func (nr *NotificationRequest) Validate() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate scheduled_at: 0 means immediate delivery, otherwise must be future timestamp
|
||||||
|
if nr.ScheduledAt < 0 {
|
||||||
|
return ErrValidation{Field: "scheduled_at", Message: "scheduled_at cannot be negative"}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,3 +110,8 @@ func (nm *NotificationMethods) GetMethodValue(eventType EventType) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsImmediate returns true if the notification should be sent immediately (scheduled_at = 0)
|
||||||
|
func (nr *NotificationRequest) IsImmediate() bool {
|
||||||
|
return nr.ScheduledAt == 0
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,19 +10,22 @@ type NotificationService interface {
|
|||||||
SendNotification(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error)
|
SendNotification(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error)
|
||||||
|
|
||||||
// SendEmail sends an email notification
|
// SendEmail sends an email notification
|
||||||
SendEmail(ctx context.Context, email, message, userID string) (*NotificationResponse, error)
|
SendEmail(ctx context.Context, model Model) (*NotificationResponse, error)
|
||||||
|
|
||||||
// SendSMS sends an SMS notification
|
// SendSMS sends an SMS notification
|
||||||
SendSMS(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error)
|
SendSMS(ctx context.Context, model Model) (*NotificationResponse, error)
|
||||||
|
|
||||||
// SendPush sends a push notification
|
// SendPush sends a push notification
|
||||||
SendPush(ctx context.Context, deviceToken, message, userID string) (*NotificationResponse, error)
|
SendPush(ctx context.Context, model Model) (*NotificationResponse, error)
|
||||||
|
|
||||||
// SendOTP sends an OTP notification
|
// SendOTP sends an OTP notification
|
||||||
SendOTP(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error)
|
SendOTP(ctx context.Context, model Model) (*NotificationResponse, error)
|
||||||
|
|
||||||
// Health checks if the notification service is available
|
// Health checks if the notification service is available
|
||||||
Health(ctx context.Context) error
|
Health(ctx context.Context) error
|
||||||
|
|
||||||
|
// NewBuilder creates a new notification builder
|
||||||
|
NewBuilder() Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
// Builder provides a fluent interface for building notification requests
|
// Builder provides a fluent interface for building notification requests
|
||||||
@@ -48,6 +51,18 @@ type Builder interface {
|
|||||||
// SetOTP sets the OTP phone number
|
// SetOTP sets the OTP phone number
|
||||||
SetOTP(phoneNumber string) Builder
|
SetOTP(phoneNumber string) Builder
|
||||||
|
|
||||||
|
// SetTitle sets the notification title
|
||||||
|
SetTitle(title string) Builder
|
||||||
|
|
||||||
|
// SetType sets the notification type
|
||||||
|
SetType(notificationType string) Builder
|
||||||
|
|
||||||
|
// SetPriority sets the notification priority
|
||||||
|
SetPriority(priority string) Builder
|
||||||
|
|
||||||
|
// SetScheduledAt sets the scheduled delivery time (0 for immediate)
|
||||||
|
SetScheduledAt(scheduledAt int64) Builder
|
||||||
|
|
||||||
// Build creates the notification request
|
// Build creates the notification request
|
||||||
Build() (*NotificationRequest, error)
|
Build() (*NotificationRequest, error)
|
||||||
|
|
||||||
|
|||||||
+16
-16
@@ -55,23 +55,23 @@ func (s *SDK) SendNotification(ctx context.Context, req *NotificationRequest) (*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendEmail sends an email notification
|
// SendEmail sends an email notification
|
||||||
func (s *SDK) SendEmail(ctx context.Context, email, message, userID string) (*NotificationResponse, error) {
|
func (s *SDK) SendEmail(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
return s.service.SendEmail(ctx, email, message, userID)
|
return s.service.SendEmail(ctx, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSMS sends an SMS notification
|
// SendSMS sends an SMS notification
|
||||||
func (s *SDK) SendSMS(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
|
func (s *SDK) SendSMS(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
return s.service.SendSMS(ctx, phoneNumber, message, userID)
|
return s.service.SendSMS(ctx, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendPush sends a push notification
|
// SendPush sends a push notification
|
||||||
func (s *SDK) SendPush(ctx context.Context, deviceToken, message, userID string) (*NotificationResponse, error) {
|
func (s *SDK) SendPush(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
return s.service.SendPush(ctx, deviceToken, message, userID)
|
return s.service.SendPush(ctx, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendOTP sends an OTP notification
|
// SendOTP sends an OTP notification
|
||||||
func (s *SDK) SendOTP(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
|
func (s *SDK) SendOTP(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
return s.service.SendOTP(ctx, phoneNumber, message, userID)
|
return s.service.SendOTP(ctx, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder creates a new notification builder for fluent API usage
|
// NewBuilder creates a new notification builder for fluent API usage
|
||||||
@@ -92,26 +92,26 @@ func (s *SDK) GetConfig() *Config {
|
|||||||
// Convenience functions for quick notifications
|
// Convenience functions for quick notifications
|
||||||
|
|
||||||
// QuickEmail sends a simple email notification
|
// QuickEmail sends a simple email notification
|
||||||
func (s *SDK) QuickEmail(ctx context.Context, email, message string) error {
|
func (s *SDK) QuickEmail(ctx context.Context, model Model) error {
|
||||||
_, err := s.SendEmail(ctx, email, message, "")
|
_, err := s.SendEmail(ctx, model)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickSMS sends a simple SMS notification
|
// QuickSMS sends a simple SMS notification
|
||||||
func (s *SDK) QuickSMS(ctx context.Context, phoneNumber, message string) error {
|
func (s *SDK) QuickSMS(ctx context.Context, model Model) error {
|
||||||
_, err := s.SendSMS(ctx, phoneNumber, message, "")
|
_, err := s.SendSMS(ctx, model)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickPush sends a simple push notification
|
// QuickPush sends a simple push notification
|
||||||
func (s *SDK) QuickPush(ctx context.Context, deviceToken, message string) error {
|
func (s *SDK) QuickPush(ctx context.Context, model Model) error {
|
||||||
_, err := s.SendPush(ctx, deviceToken, message, "")
|
_, err := s.SendPush(ctx, model)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickOTP sends a simple OTP notification
|
// QuickOTP sends a simple OTP notification
|
||||||
func (s *SDK) QuickOTP(ctx context.Context, phoneNumber, message string) error {
|
func (s *SDK) QuickOTP(ctx context.Context, model Model) error {
|
||||||
_, err := s.SendOTP(ctx, phoneNumber, message, "")
|
_, err := s.SendOTP(ctx, model)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+76
-22
@@ -10,6 +10,16 @@ type Service struct {
|
|||||||
client *Client
|
client *Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
Message string
|
||||||
|
Title string
|
||||||
|
Type string
|
||||||
|
Priority string
|
||||||
|
ScheduledAt int64
|
||||||
|
Recipient string
|
||||||
|
UserID string
|
||||||
|
}
|
||||||
|
|
||||||
// NewService creates a new notification service
|
// NewService creates a new notification service
|
||||||
func NewService(client *Client) NotificationService {
|
func NewService(client *Client) NotificationService {
|
||||||
return &Service{
|
return &Service{
|
||||||
@@ -23,56 +33,72 @@ func (s *Service) SendNotification(ctx context.Context, req *NotificationRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendEmail sends an email notification
|
// SendEmail sends an email notification
|
||||||
func (s *Service) SendEmail(ctx context.Context, email, message, userID string) (*NotificationResponse, error) {
|
func (s *Service) SendEmail(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
req := &NotificationRequest{
|
req := &NotificationRequest{
|
||||||
EventType: EventTypeEmail,
|
EventType: EventTypeEmail,
|
||||||
Message: message,
|
Message: model.Message,
|
||||||
|
Title: model.Title,
|
||||||
|
Type: model.Type,
|
||||||
|
Priority: model.Priority,
|
||||||
|
ScheduledAt: model.ScheduledAt,
|
||||||
Methods: NotificationMethods{
|
Methods: NotificationMethods{
|
||||||
Email: email,
|
Email: model.Recipient,
|
||||||
},
|
},
|
||||||
UserID: userID,
|
UserID: model.UserID,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.client.SendNotification(ctx, req)
|
return s.client.SendNotification(ctx, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSMS sends an SMS notification
|
// SendSMS sends an SMS notification
|
||||||
func (s *Service) SendSMS(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
|
func (s *Service) SendSMS(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
req := &NotificationRequest{
|
req := &NotificationRequest{
|
||||||
EventType: EventTypeSMS,
|
EventType: EventTypeSMS,
|
||||||
Message: message,
|
Message: model.Message,
|
||||||
|
Title: model.Title,
|
||||||
|
Type: model.Type,
|
||||||
|
Priority: model.Priority,
|
||||||
|
ScheduledAt: model.ScheduledAt,
|
||||||
Methods: NotificationMethods{
|
Methods: NotificationMethods{
|
||||||
SMS: phoneNumber,
|
SMS: model.Recipient,
|
||||||
},
|
},
|
||||||
UserID: userID,
|
UserID: model.UserID,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.client.SendNotification(ctx, req)
|
return s.client.SendNotification(ctx, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendPush sends a push notification
|
// SendPush sends a push notification
|
||||||
func (s *Service) SendPush(ctx context.Context, deviceToken, message, userID string) (*NotificationResponse, error) {
|
func (s *Service) SendPush(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
req := &NotificationRequest{
|
req := &NotificationRequest{
|
||||||
EventType: EventTypePush,
|
EventType: EventTypePush,
|
||||||
Message: message,
|
Message: model.Message,
|
||||||
|
Title: model.Title,
|
||||||
|
Type: model.Type,
|
||||||
|
Priority: model.Priority,
|
||||||
|
ScheduledAt: model.ScheduledAt,
|
||||||
Methods: NotificationMethods{
|
Methods: NotificationMethods{
|
||||||
Push: deviceToken,
|
Push: model.Recipient,
|
||||||
},
|
},
|
||||||
UserID: userID,
|
UserID: model.UserID,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.client.SendNotification(ctx, req)
|
return s.client.SendNotification(ctx, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendOTP sends an OTP notification
|
// SendOTP sends an OTP notification
|
||||||
func (s *Service) SendOTP(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
|
func (s *Service) SendOTP(ctx context.Context, model Model) (*NotificationResponse, error) {
|
||||||
req := &NotificationRequest{
|
req := &NotificationRequest{
|
||||||
EventType: EventTypeOTP,
|
EventType: EventTypeOTP,
|
||||||
Message: message,
|
Message: model.Message,
|
||||||
|
Title: model.Title,
|
||||||
|
Type: model.Type,
|
||||||
|
Priority: model.Priority,
|
||||||
|
ScheduledAt: model.ScheduledAt,
|
||||||
Methods: NotificationMethods{
|
Methods: NotificationMethods{
|
||||||
OTP: phoneNumber,
|
OTP: model.Recipient,
|
||||||
},
|
},
|
||||||
UserID: userID,
|
UserID: model.UserID,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.client.SendNotification(ctx, req)
|
return s.client.SendNotification(ctx, req)
|
||||||
@@ -82,8 +108,12 @@ func (s *Service) SendOTP(ctx context.Context, phoneNumber, message, userID stri
|
|||||||
func (s *Service) Health(ctx context.Context) error {
|
func (s *Service) Health(ctx context.Context) error {
|
||||||
// Create a simple test request to check service availability
|
// Create a simple test request to check service availability
|
||||||
testReq := &NotificationRequest{
|
testReq := &NotificationRequest{
|
||||||
EventType: EventTypeEmail,
|
EventType: EventTypeEmail,
|
||||||
Message: "health check",
|
Message: "health check",
|
||||||
|
Title: "Health Check",
|
||||||
|
Type: "info",
|
||||||
|
Priority: "low",
|
||||||
|
ScheduledAt: 0,
|
||||||
Methods: NotificationMethods{
|
Methods: NotificationMethods{
|
||||||
Email: "test@example.com",
|
Email: "test@example.com",
|
||||||
},
|
},
|
||||||
@@ -157,6 +187,30 @@ func (b *notificationBuilder) SetOTP(phoneNumber string) Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTitle sets the notification title
|
||||||
|
func (b *notificationBuilder) SetTitle(title string) Builder {
|
||||||
|
b.request.Title = title
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetType sets the notification type
|
||||||
|
func (b *notificationBuilder) SetType(notificationType string) Builder {
|
||||||
|
b.request.Type = notificationType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPriority sets the notification priority
|
||||||
|
func (b *notificationBuilder) SetPriority(priority string) Builder {
|
||||||
|
b.request.Priority = priority
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetScheduledAt sets the scheduled delivery time (0 for immediate)
|
||||||
|
func (b *notificationBuilder) SetScheduledAt(scheduledAt int64) Builder {
|
||||||
|
b.request.ScheduledAt = scheduledAt
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
// Build creates the notification request
|
// Build creates the notification request
|
||||||
func (b *notificationBuilder) Build() (*NotificationRequest, error) {
|
func (b *notificationBuilder) Build() (*NotificationRequest, error) {
|
||||||
if err := b.request.Validate(); err != nil {
|
if err := b.request.Validate(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user