diff --git a/docs/notifications-service/1_about.md b/docs/notifications-service/1_about.md index 50d69b1..a04c9e9 100644 --- a/docs/notifications-service/1_about.md +++ b/docs/notifications-service/1_about.md @@ -4,5 +4,5 @@ sidebar_position: 1 # About -Notification Service is designed to deliver messages to users swiftly and effectively. By supporting a range of notification types like SMS, push notifications and email, this service ensures that user will receive timely updates through their preferred communication channels. +Notification Service is designed to deliver messages to users swiftly and effectively. By supporting a range of notification types like SMS, push notifications, inApp and email, this service ensures that user will receive timely updates through their preferred communication channels. This service is vital for maintaining user engagement and keeping them informed about key events and activities within your application or system. \ No newline at end of file diff --git a/docs/notifications-service/2_features.md b/docs/notifications-service/2_features.md index eb719c7..323f1e2 100644 --- a/docs/notifications-service/2_features.md +++ b/docs/notifications-service/2_features.md @@ -9,7 +9,7 @@ title: Notification Service Features Seamlessly manage and distribute large volumes of notifications using RabbitMQ. ### 2. Multiple Delivery Modes -Choose from various notification methods, including SMS, email, and push notifications. +Choose from various notification methods, including SMS, email, inApp and push notifications. ### 3. Customizable Templates Personalize notifications with adaptable templates to meet specific needs. diff --git a/docs/notifications-service/6_database-schema.md b/docs/notifications-service/6_database-schema.md index 2ce7f25..e2a09fd 100644 --- a/docs/notifications-service/6_database-schema.md +++ b/docs/notifications-service/6_database-schema.md @@ -52,4 +52,24 @@ sidebar_position: 6 | recipient | character varying(255) | Recipient of the notification | | error | character varying(255) | Error message, if any | -![NotificationLogs](assets/notification_db2.png) \ No newline at end of file +![NotificationLogs](assets/notification_db2.png) +#### InAppNotification #### + +| Column Name | Data Type | Description | +|-------------|--------------------------|-----------------------------------------------------------------------------------| +| id | uuid | Unique identifier (Primary Key) | +| userId | uuid | ID of the user who is the recipient of the in-app notification. | +| action_key | character varying (255) | "Key associated with the action (e.g., references NotificationActionTemplates.key)." | +| org_code | character varying(255) | Optional organization code. | +| context | character varying(255) | "Context or purpose of the notification (e.g., 'task_assignment')." | +| title | character varying(255) | Title of the in-app notification. | +| message | text | Full content/body of the in-app notification. | +| link | character varying(500) | Optional URL or link associated with the notification action | +| metadata | jsonb | Optional JSON data for extra context or dynamic content. | +| isRead | boolean | Flag indicating if the user has read the notification (default: false) | +| createdAt | timestamp with time zone | Timestamp for when the notification was created. | +| readAt | timestamp with time zone | Timestamp for when the notification was marked as read. | +| expiresAt | timestamp with time zone | Optional timestamp after which the notification should be considered expired/removed. | + + +![inAppNotification](assets/inappnotification_db.png) diff --git a/docs/notifications-service/7_api-docs.md b/docs/notifications-service/7_api-docs.md index 89e652e..a6cabe2 100644 --- a/docs/notifications-service/7_api-docs.md +++ b/docs/notifications-service/7_api-docs.md @@ -372,3 +372,264 @@ sidebar_position: 7 "name": "projects/PROJECT_ID/messages/0:1618924844212%6e23a5c0f9e1a1e8" } ``` + + +# In-App Notifications - API & Integration Guide + +This document explains how to use the new in-app notifications in the Notification Service. + +## Endpoints + +- Create in-app notification (raw or template-based) + - POST `notification/inApp` +- List notifications with filters and pagination + - GET `notification/inApp?userId={uuid}&status=unread|read|all&page=1&limit=20` +- Count unread (reuse list with `limit=0`) + - GET `notification/inApp?userId={uuid}&status=unread&limit=0` +- Mark read (single or all) + - PATCH `notification/inApp/mark-read` + +All endpoints require `Authorization: Bearer ` header (Swagger auth key `access-token`). + +--- + +## Create (Raw) + +POST `notification/inApp` + +Body: +```json +{ + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "title": "Task Completed", + "message": "Your review task is completed.", + "link": "https://app.example.com/tasks/T-9001", + "metadata": { "taskId": "T-9001" }, + "tenant_code": "TENANT1", + "org_code": "ORG1", + "expiresAt": "2026-01-01T00:00:00.000Z" +} +``` + +Notes: +- `title` is required only when neither `templateId` nor `key` is provided. +- `message` (body) is stored at create time from template and replacements (if provided). List returns stored message directly. +- Optional fields: `link`, `metadata`, `tenant_code`, `org_code`, `expiresAt`. + +--- + +## Create (Template-based) + +POST `notification/inApp` + +Body: +```json +{ + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "templateId": "0d3b4b24-8a00-4f1d-9a1c-2f6f1c5f2b11", + "replacements": { + "userName": "Sachin", + "{taskId}": "T-9001" + }, + "metadata": { "priority": "high" } +} +``` + +Rules: +- The template is taken from `NotificationActionTemplates` with `type='inApp'`. +- Placeholders in the template subject/body/link are replaced by `replacements`. + - Keys can be provided either with braces (`"{userName}"`) or without (`"userName"`). +- If both `templateId` and raw `title/message` are provided, the template takes precedence. + +--- + +## Create (Action-based, consistent with Email/SMS/Push) + +POST `notification/inApp` + +Body: +```json +{ + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "context": "USER", + "key": "OnRegister", + "replacements": { + "userName": "Sachin" + }, + "metadata": { "role": "superuser" } +} +``` + +Rules: +- We resolve `NotificationActions` by `context` and `key`, then pick the `NotificationActionTemplates` with `type='inApp'`. +- Placeholders are replaced using `replacements`. +- We store `context`, `key`, and resolved fields; template is used transiently at create time. + +--- + +## Create (Key-only) + +POST `notification/inApp` + +Body: +```json +{ + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "key": "OnRegister", + "replacements": { "userName": "Sachin" } +} +``` + +Rules: +- If `key` resolves to a single action, we use that. If multiple actions share the same key, provide `context` as well. +- We fetch the in-app template for the resolved action and perform placeholder replacement. + +--- + +## Create (Send-structure, bulk over recipients) + +POST `notification/inApp` + +Curl: +```bash +curl -X POST http://localhost:4000/notification/inApp \ + -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \ + -d '{ + "context":"USER", + "key":"OnRegister", + "replacements":{"userName":"Sachin"}, + "inApp": { "receipients": ["", ""] } + }' +``` + +Behavior: +- For each userId in `inApp.receipients`, the service resolves the action/template and creates an in-app record with stored title/message/link and metadata. +- Response returns `{ inApp: { data: [{recipient, id}, ...] } }`. + +## List + +GET `notification/inApp?userId={uuid}&status=unread|read|all&offset=0&limit=20` + +Examples: +``` +GET notification/inApp?userId=c12f...&status=unread&offset=0&limit=20 +GET notification/inApp?userId=c12f...&status=read +GET notification/inApp?userId=c12f... (defaults to status=all) +``` + +Response: +```json +{ + "id": "api.send.notification", + "responseCode": "OK", + "result": { + "data": [ + { + "id": "7f7e6b10-5bdc-4f8b-bf4b-3e4f11a1b2c3", + "userId": "c12f...", + "title": "Task Completed", + "message": "Your review task is completed.", + "link": "https://app.example.com/tasks/T-9001", + "metadata": { "taskId": "T-9001" }, + "isRead": false, + "createdAt": "2025-12-05T10:30:00.000Z", + "readAt": null, + "expiresAt": null + } + ], + "count": 1, + "offset": 0, + "limit": 20 + } +} +``` + +--- + +## Count Unread + +GET `notification/inApp?userId={uuid}&status=unread&limit=0` + +Response: +```json +{ + "id": "api.send.notification", + "responseCode": "OK", + "result": { "count": 3 } +} +``` + +--- + +## Mark Read + +PATCH `notification/inApp/mark-read` + +- Mark single: +```json +{ "notificationId": "7f7e6b10-5bdc-4f8b-bf4b-3e4f11a1b2c3" } +``` + +- Mark all for a user: +```json +{ "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", "markAll": true } +``` + +Responses: +- Success (single): +```json +{ "updated": 1, "message": "Notification marked as read" } +``` + + +--- + +## Logging + +All in-app operations write to `notificationLogs` for audit: +- Create (raw or template): `type='inApp'`, `action='create'`, `subject=title`, `recipient=userId` +- Mark read (single): `type='inApp'`, `action='mark-read-single'`, body includes notificationId +- Mark read (all): `type='inApp'`, `action='mark-read-all'`, body includes affected count and userId + +Use these logs for tracing, analytics, and support. + +--- + +## Kafka Integration (optional) + +If you publish to Kafka to create in-app notifications, use the unified service topic: +- `notifications` + +Message formats: + +1) Raw create (channel discriminator) +```json +{ + "channel": "inApp", + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "title": "Task Completed", + "message": "Your review task is completed.", + "link": "https://app.example.com/tasks/T-9001", + "metadata": { "taskId": "T-9001" }, + "tenant_code": "TENANT1", + "org_code": "ORG1", + "expiresAt": "2026-01-01T00:00:00.000Z", + "traceId": "d9f7a1a0-6e4f-4a44-a9d0-1234567890ab" +} +``` + +2) Key-based create (channel discriminator) +```json +{ + "channel": "inApp", + "userId": "c12fa913-6fc1-4e90-bd44-2c75724f2b3c", + "key": "OnRegister", + "context": "USER", + "replacements": { + "userName": "Sachin", + "{taskId}": "T-9001" + }, + "metadata": { "priority": "high" }, + "traceId": "d9f7a1a0-6e4f-4a44-a9d0-1234567890ab" +} +``` diff --git a/docs/notifications-service/assets/inappnotification_db.png b/docs/notifications-service/assets/inappnotification_db.png new file mode 100644 index 0000000..16d87c9 Binary files /dev/null and b/docs/notifications-service/assets/inappnotification_db.png differ