Files
tm_back/pkg/notification/USAGE.md
T
n.nakhostin 19cd346b1c Enhance Notification Management with New Endpoints and Response Structures
- Added new endpoints for retrieving notifications for both admins and users, improving the flexibility of the notification system.
- Implemented query parameters for filtering notifications by status, method, event type, type, and recipient, enhancing usability.
- Introduced new response structures for notifications, including pagination information, to provide better data handling in API responses.
- Updated API documentation with Swagger comments for the new endpoints and response formats, ensuring clarity for API consumers.
- Refactored notification handling logic to support the new features, promoting a more robust notification management system.
2025-09-20 17:41:21 +03:30

3.8 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)

Getting Notifications

Get All Notifications

// Get all notifications
response, err := sdk.GetNotifications(ctx, &notification.GetNotificationsRequest{})

Get Notifications with Filters

// Get notifications for specific user
response, err := sdk.GetNotificationsByUser(ctx, "user_id_here", 1, 10)

// Get notifications by status
response, err := sdk.GetNotificationsByStatus(ctx, "sent", 1, 10)

// Get notifications with custom filters
req := &notification.GetNotificationsRequest{
    Status:    "sent",
    Method:    "email",
    EventType: "EMAIL",
    Type:      "info",
    UserID:    "user_id_here",
    Page:      1,
    PerPage:   10,
}
response, err := sdk.GetNotifications(ctx, req)

Quick Get Notifications

// Quick method for basic filtering
response, err := sdk.QuickGetNotifications(ctx, "user_id_here", "sent")

API Response

Send Notification Response

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

Get Notifications Response

{
    "data": [
        {
            "id": "68ceaaec99c78402b5fec555",
            "user_id": "689a0aca36bf9aae890c30c1",
            "title": "CHECK",
            "message": "Check Send Notification Channels",
            "link": "",
            "image": "",
            "type": "info",
            "priority": "low",
            "event_type": "EMAIL",
            "created_at": "2025-09-20T13:23:56.664Z",
            "updated_at": "0001-01-01T00:00:00Z",
            "methods": {
                "email": "nakhostin.nima1998@gmail.com"
            },
            "metadata": null,
            "status": "sent",
            "scheduled_at": 0,
            "is_scheduled": false
        }
    ],
    "pagination": {
        "total": 3,
        "count": 3,
        "per_page": 10,
        "current_page": 1,
        "total_pages": 1
    }
}

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
    }
}