Files
tm_back/pkg/notification/USAGE.md
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

2.1 KiB

Notification SDK - Quick Usage Guide

Installation

The SDK is already part of your tm module. Import it as:

import "tm/pkg/notification"

Basic Setup

// 1. Configuration
config := notification.DefaultConfig()
config.BaseURL = "http://127.0.0.1:9095" // Your notification service URL

// 2. Logger (using project's logger)
loggerConfig := &logger.Config{Level: "info", Format: "json", Output: "stdout"}
projectLogger := logger.NewLogger(loggerConfig)
loggerAdapter := &notification.LoggerAdapter{Logger: projectLogger}

// 3. Create SDK
sdk, err := notification.NewSDK(config, loggerAdapter)

Environment Variables

export NOTIFICATION_BASE_URL="http://127.0.0.1:9095"
export NOTIFICATION_TIMEOUT="30s"
export NOTIFICATION_RETRY_ATTEMPTS="3"
export NOTIFICATION_RETRY_DELAY="2s"
export NOTIFICATION_ENABLE_LOGGING="true"

Quick Examples

Simple Email

err := sdk.QuickEmail(ctx, "user@example.com", "Hello!")

Custom Notification

request := &notification.NotificationRequest{
    EventType: notification.EventTypeEmail,
    Message:   "Hello Nima",
    Methods: notification.NotificationMethods{
        OTP:   "09143257416",
        SMS:   "09143257416", 
        Push:  "devicefsdgsdgsg6766sdgdsg",
        Email: "nakhostin.nima1998@gmail.com",
    },
    UserID: "",
}
response, err := sdk.SendNotification(ctx, request)

Fluent Builder

response, err := sdk.NewBuilder().
    SetEventType(notification.EventTypeEmail).
    SetMessage("Hello Nima").
    SetEmail("nakhostin.nima1998@gmail.com").
    Send(ctx)

Batch Operations

requests := []*notification.NotificationRequest{...}
batchResponse, err := sdk.SendBatch(ctx, requests)

API Response

  • Success: {"status": "Notification sent"}
  • Error: {"error": "error message"}

Error Handling

if err != nil {
    switch e := err.(type) {
    case notification.ErrHTTP:
        // HTTP error (400, 500, etc.)
    case notification.ErrValidation:
        // Validation error
    case notification.ErrRetryExhausted:
        // All retry attempts failed
    }
}