19cd346b1c
- 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.
159 lines
3.8 KiB
Markdown
159 lines
3.8 KiB
Markdown
# Notification SDK - Quick Usage Guide
|
|
|
|
## Installation
|
|
The SDK is already part of your `tm` module. Import it as:
|
|
```go
|
|
import "tm/pkg/notification"
|
|
```
|
|
|
|
## Basic Setup
|
|
```go
|
|
// 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 := ¬ification.LoggerAdapter{Logger: projectLogger}
|
|
|
|
// 3. Create SDK
|
|
sdk, err := notification.NewSDK(config, loggerAdapter)
|
|
```
|
|
|
|
## Environment Variables
|
|
```bash
|
|
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
|
|
```go
|
|
err := sdk.QuickEmail(ctx, "user@example.com", "Hello!")
|
|
```
|
|
|
|
### Custom Notification
|
|
```go
|
|
request := ¬ification.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
|
|
```go
|
|
response, err := sdk.NewBuilder().
|
|
SetEventType(notification.EventTypeEmail).
|
|
SetMessage("Hello Nima").
|
|
SetEmail("nakhostin.nima1998@gmail.com").
|
|
Send(ctx)
|
|
```
|
|
|
|
### Batch Operations
|
|
```go
|
|
requests := []*notification.NotificationRequest{...}
|
|
batchResponse, err := sdk.SendBatch(ctx, requests)
|
|
```
|
|
|
|
### Getting Notifications
|
|
|
|
#### Get All Notifications
|
|
```go
|
|
// Get all notifications
|
|
response, err := sdk.GetNotifications(ctx, ¬ification.GetNotificationsRequest{})
|
|
```
|
|
|
|
#### Get Notifications with Filters
|
|
```go
|
|
// 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 := ¬ification.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
|
|
```go
|
|
// 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
|
|
```json
|
|
{
|
|
"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
|
|
```go
|
|
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
|
|
}
|
|
}
|
|
``` |