108629278a
- Added a new notification service to handle various notification types (email, SMS, push, OTP). - Implemented configuration for the notification service, allowing customization via environment variables. - Updated user and customer services to utilize the notification service for sending welcome emails and status updates. - Introduced a builder pattern for constructing notification requests, enhancing usability and flexibility. - Enhanced error handling and logging for notification operations, ensuring robust service integration. - Updated documentation to include usage examples and configuration details for the new notification SDK.
178 lines
4.4 KiB
Go
178 lines
4.4 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// Service implements the NotificationService interface
|
|
type Service struct {
|
|
client *Client
|
|
}
|
|
|
|
// NewService creates a new notification service
|
|
func NewService(client *Client) NotificationService {
|
|
return &Service{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// SendNotification sends a notification using the configured client
|
|
func (s *Service) SendNotification(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error) {
|
|
return s.client.SendNotification(ctx, req)
|
|
}
|
|
|
|
// SendEmail sends an email notification
|
|
func (s *Service) SendEmail(ctx context.Context, email, message, userID string) (*NotificationResponse, error) {
|
|
req := &NotificationRequest{
|
|
EventType: EventTypeEmail,
|
|
Message: message,
|
|
Methods: NotificationMethods{
|
|
Email: email,
|
|
},
|
|
UserID: 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) {
|
|
req := &NotificationRequest{
|
|
EventType: EventTypeSMS,
|
|
Message: message,
|
|
Methods: NotificationMethods{
|
|
SMS: phoneNumber,
|
|
},
|
|
UserID: 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) {
|
|
req := &NotificationRequest{
|
|
EventType: EventTypePush,
|
|
Message: message,
|
|
Methods: NotificationMethods{
|
|
Push: deviceToken,
|
|
},
|
|
UserID: 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) {
|
|
req := &NotificationRequest{
|
|
EventType: EventTypeOTP,
|
|
Message: message,
|
|
Methods: NotificationMethods{
|
|
OTP: phoneNumber,
|
|
},
|
|
UserID: userID,
|
|
}
|
|
|
|
return s.client.SendNotification(ctx, req)
|
|
}
|
|
|
|
// Health checks if the notification service is available
|
|
func (s *Service) Health(ctx context.Context) error {
|
|
// Create a simple test request to check service availability
|
|
testReq := &NotificationRequest{
|
|
EventType: EventTypeEmail,
|
|
Message: "health check",
|
|
Methods: NotificationMethods{
|
|
Email: "test@example.com",
|
|
},
|
|
}
|
|
|
|
// We don't actually send this, just validate the client can create a request
|
|
if err := testReq.Validate(); err != nil {
|
|
return fmt.Errorf("service health check failed: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// notificationBuilder implements the Builder interface
|
|
type notificationBuilder struct {
|
|
service *Service
|
|
request *NotificationRequest
|
|
hasErrors []error
|
|
}
|
|
|
|
// NewBuilder creates a new notification builder
|
|
func (s *Service) NewBuilder() Builder {
|
|
return ¬ificationBuilder{
|
|
service: s,
|
|
request: &NotificationRequest{
|
|
Methods: NotificationMethods{},
|
|
},
|
|
hasErrors: make([]error, 0),
|
|
}
|
|
}
|
|
|
|
// SetEventType sets the event type
|
|
func (b *notificationBuilder) SetEventType(eventType EventType) Builder {
|
|
b.request.EventType = eventType
|
|
return b
|
|
}
|
|
|
|
// SetMessage sets the notification message
|
|
func (b *notificationBuilder) SetMessage(message string) Builder {
|
|
b.request.Message = message
|
|
return b
|
|
}
|
|
|
|
// SetUserID sets the user ID
|
|
func (b *notificationBuilder) SetUserID(userID string) Builder {
|
|
b.request.UserID = userID
|
|
return b
|
|
}
|
|
|
|
// SetEmail sets the email address
|
|
func (b *notificationBuilder) SetEmail(email string) Builder {
|
|
b.request.Methods.Email = email
|
|
return b
|
|
}
|
|
|
|
// SetSMS sets the SMS phone number
|
|
func (b *notificationBuilder) SetSMS(phoneNumber string) Builder {
|
|
b.request.Methods.SMS = phoneNumber
|
|
return b
|
|
}
|
|
|
|
// SetPush sets the push device token
|
|
func (b *notificationBuilder) SetPush(deviceToken string) Builder {
|
|
b.request.Methods.Push = deviceToken
|
|
return b
|
|
}
|
|
|
|
// SetOTP sets the OTP phone number
|
|
func (b *notificationBuilder) SetOTP(phoneNumber string) Builder {
|
|
b.request.Methods.OTP = phoneNumber
|
|
return b
|
|
}
|
|
|
|
// Build creates the notification request
|
|
func (b *notificationBuilder) Build() (*NotificationRequest, error) {
|
|
if err := b.request.Validate(); err != nil {
|
|
return nil, fmt.Errorf("failed to build notification request: %w", err)
|
|
}
|
|
|
|
return b.request, nil
|
|
}
|
|
|
|
// Send builds and sends the notification
|
|
func (b *notificationBuilder) Send(ctx context.Context) (*NotificationResponse, error) {
|
|
req, err := b.Build()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.service.SendNotification(ctx, req)
|
|
}
|