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:
n.nakhostin
2025-09-20 15:29:00 +03:30
parent c6801ef2f3
commit 0857314906
4 changed files with 129 additions and 46 deletions
+76 -22
View File
@@ -10,6 +10,16 @@ type Service struct {
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
func NewService(client *Client) NotificationService {
return &Service{
@@ -23,56 +33,72 @@ func (s *Service) SendNotification(ctx context.Context, req *NotificationRequest
}
// 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{
EventType: EventTypeEmail,
Message: message,
EventType: EventTypeEmail,
Message: model.Message,
Title: model.Title,
Type: model.Type,
Priority: model.Priority,
ScheduledAt: model.ScheduledAt,
Methods: NotificationMethods{
Email: email,
Email: model.Recipient,
},
UserID: userID,
UserID: model.UserID,
}
return s.client.SendNotification(ctx, req)
}
// 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{
EventType: EventTypeSMS,
Message: message,
EventType: EventTypeSMS,
Message: model.Message,
Title: model.Title,
Type: model.Type,
Priority: model.Priority,
ScheduledAt: model.ScheduledAt,
Methods: NotificationMethods{
SMS: phoneNumber,
SMS: model.Recipient,
},
UserID: userID,
UserID: model.UserID,
}
return s.client.SendNotification(ctx, req)
}
// 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{
EventType: EventTypePush,
Message: message,
EventType: EventTypePush,
Message: model.Message,
Title: model.Title,
Type: model.Type,
Priority: model.Priority,
ScheduledAt: model.ScheduledAt,
Methods: NotificationMethods{
Push: deviceToken,
Push: model.Recipient,
},
UserID: userID,
UserID: model.UserID,
}
return s.client.SendNotification(ctx, req)
}
// 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{
EventType: EventTypeOTP,
Message: message,
EventType: EventTypeOTP,
Message: model.Message,
Title: model.Title,
Type: model.Type,
Priority: model.Priority,
ScheduledAt: model.ScheduledAt,
Methods: NotificationMethods{
OTP: phoneNumber,
OTP: model.Recipient,
},
UserID: userID,
UserID: model.UserID,
}
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 {
// Create a simple test request to check service availability
testReq := &NotificationRequest{
EventType: EventTypeEmail,
Message: "health check",
EventType: EventTypeEmail,
Message: "health check",
Title: "Health Check",
Type: "info",
Priority: "low",
ScheduledAt: 0,
Methods: NotificationMethods{
Email: "test@example.com",
},
@@ -157,6 +187,30 @@ func (b *notificationBuilder) SetOTP(phoneNumber string) Builder {
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
func (b *notificationBuilder) Build() (*NotificationRequest, error) {
if err := b.request.Validate(); err != nil {