Files
tm_back/pkg/notification/sdk.go
T
n.nakhostin 108629278a Integrate Notification Service into Tender Management System
- 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.
2025-09-16 15:35:36 +03:30

179 lines
4.9 KiB
Go

package notification
import (
"context"
"fmt"
)
// SDK provides a high-level interface for the notification service
type SDK struct {
service NotificationService
config *Config
}
// NewSDK creates a new notification SDK with the provided configuration and logger
func New(config *Config, logger Logger) (*SDK, error) {
if config == nil {
config = DefaultConfig()
}
client, err := NewClient(config, logger)
if err != nil {
return nil, fmt.Errorf("failed to create notification client: %w", err)
}
service := NewService(client)
return &SDK{
service: service,
config: config,
}, nil
}
// NewSDKWithClient creates a new notification SDK with a custom HTTP client
func NewSDKWithClient(config *Config, httpClient HTTPClient, logger Logger) (*SDK, error) {
if config == nil {
config = DefaultConfig()
}
client, err := NewClientWithHTTPClient(config, httpClient, logger)
if err != nil {
return nil, fmt.Errorf("failed to create notification client: %w", err)
}
service := NewService(client)
return &SDK{
service: service,
config: config,
}, nil
}
// SendNotification sends a custom notification request
func (s *SDK) SendNotification(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error) {
return s.service.SendNotification(ctx, req)
}
// SendEmail sends an email notification
func (s *SDK) SendEmail(ctx context.Context, email, message, userID string) (*NotificationResponse, error) {
return s.service.SendEmail(ctx, email, message, userID)
}
// SendSMS sends an SMS notification
func (s *SDK) SendSMS(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
return s.service.SendSMS(ctx, phoneNumber, message, userID)
}
// SendPush sends a push notification
func (s *SDK) SendPush(ctx context.Context, deviceToken, message, userID string) (*NotificationResponse, error) {
return s.service.SendPush(ctx, deviceToken, message, userID)
}
// SendOTP sends an OTP notification
func (s *SDK) SendOTP(ctx context.Context, phoneNumber, message, userID string) (*NotificationResponse, error) {
return s.service.SendOTP(ctx, phoneNumber, message, userID)
}
// NewBuilder creates a new notification builder for fluent API usage
func (s *SDK) NewBuilder() Builder {
return s.service.(*Service).NewBuilder()
}
// Health checks if the notification service is available
func (s *SDK) Health(ctx context.Context) error {
return s.service.Health(ctx)
}
// GetConfig returns the current configuration
func (s *SDK) GetConfig() *Config {
return s.config
}
// Convenience functions for quick notifications
// QuickEmail sends a simple email notification
func (s *SDK) QuickEmail(ctx context.Context, email, message string) error {
_, err := s.SendEmail(ctx, email, message, "")
return err
}
// QuickSMS sends a simple SMS notification
func (s *SDK) QuickSMS(ctx context.Context, phoneNumber, message string) error {
_, err := s.SendSMS(ctx, phoneNumber, message, "")
return err
}
// QuickPush sends a simple push notification
func (s *SDK) QuickPush(ctx context.Context, deviceToken, message string) error {
_, err := s.SendPush(ctx, deviceToken, message, "")
return err
}
// QuickOTP sends a simple OTP notification
func (s *SDK) QuickOTP(ctx context.Context, phoneNumber, message string) error {
_, err := s.SendOTP(ctx, phoneNumber, message, "")
return err
}
// Batch operations for sending multiple notifications
// BatchRequest represents a batch notification request
type BatchRequest struct {
Requests []*NotificationRequest `json:"requests"`
}
// BatchResponse represents the response from batch operations
type BatchResponse struct {
Responses []BatchItemResponse `json:"responses"`
Success int `json:"success"`
Failed int `json:"failed"`
}
// BatchItemResponse represents individual response in batch
type BatchItemResponse struct {
Index int `json:"index"`
Success bool `json:"success"`
Response *NotificationResponse `json:"response,omitempty"`
Error string `json:"error,omitempty"`
}
// SendBatch sends multiple notifications in batch (sequential for now)
func (s *SDK) SendBatch(ctx context.Context, requests []*NotificationRequest) (*BatchResponse, error) {
if len(requests) == 0 {
return &BatchResponse{
Responses: []BatchItemResponse{},
Success: 0,
Failed: 0,
}, nil
}
responses := make([]BatchItemResponse, len(requests))
successCount := 0
failedCount := 0
for i, req := range requests {
resp, err := s.service.SendNotification(ctx, req)
if err != nil {
responses[i] = BatchItemResponse{
Index: i,
Success: false,
Error: err.Error(),
}
failedCount++
} else {
responses[i] = BatchItemResponse{
Index: i,
Success: true,
Response: resp,
}
successCount++
}
}
return &BatchResponse{
Responses: responses,
Success: successCount,
Failed: failedCount,
}, nil
}