Skip to content

NotificationService

reference
1 min readUpdated

Kind: Service

Source: atloria-monorepo/apps/api/src/notification/notification.service.ts

NotificationService manages creation, delivery, retrieval, and read-state updates for user notifications in the API. It centralizes notification flows for mentions, new comments, and suggestions, and provides user-facing queries such as unread notifications and notification lists.

Methods

MethodSignatureReturnsDescription
createNotificationcreateNotification(dto: CreateNotificationDto)unknownCreate a notification for a user
notifyMentionsnotifyMentions(comment: { id: string; content: string; documentId: string }, mentionedUserIds: string[], authorName: string)unknownNotify users who were mentioned in a comment
notifyNewCommentnotifyNewComment(documentId: string, commentId: string, authorName: string, subscribedUserIds: string[])unknownNotify when a new comment is added to a document
notifySuggestionnotifySuggestion(documentId: string, suggestionId: string, authorName: string, documentOwnerId: string)unknownNotify when a suggestion is made
getUnreadNotificationsgetUnreadNotifications(userId: string)unknownGet unread notifications for a user
listForUserlistForUser(userId: string, opts: ListNotificationsOptions)unknownList a user's notification feed for the navbar bell (P1.d reader API): newest first, paginated, optional unread-only filter.
markAsReadmarkAsRead(notificationId: string, userId: string)unknownMark notification as read.
markAllAsReadmarkAllAsRead(userId: string)unknownMark all notifications as read for a user
getUserNotificationsgetUserNotifications(userId: string, limit: unknown)unknownGet all notifications for a user

Dependencies

  • PrismaService

Where it refuses work

  • NotificationService stops the work with NotFoundException when result.count === 0 — “Notification not found”.

When something fails

  • NotificationService handles failure in 1 place: it lets it reach the caller in all 1.

Diagram

mermaid
sequenceDiagram
  participant Client
  participant Feature as Comment/Suggestion Feature
  participant Notifications as NotificationService
  participant Store as Notification Repository

  Feature->>Notifications: notifyMentions() / notifyNewComment() / notifySuggestion()
  Notifications->>Notifications: createNotification()
  Notifications->>Store: Persist notification
  Store-->>Notifications: Created notification

  Client->>Notifications: getUnreadNotifications() / listForUser()
  Notifications->>Store: Query user notifications
  Store-->>Notifications: Notifications and unread count
  Notifications-->>Client: Notification results

  Client->>Notifications: markAsRead() / markAllAsRead()
  Notifications->>Store: Update read state
  Store-->>Notifications: Updated notification(s)

Usage

ts
import { Controller, Get, Param, Patch } from '@nestjs/common';
import { NotificationService } from './notification.service';

@Controller('notifications')
export class NotificationController {
  constructor(
    private readonly notificationService: NotificationService,
  ) {}

  @Get('unread')
  async getUnread() {
    return this.notificationService.getUnreadNotifications();
  }

  @Get()
  async listForCurrentUser() {
    return this.notificationService.getUserNotifications();
  }

  @Patch(':notificationId/read')
  async markRead(@Param('notificationId') notificationId: string) {
    return this.notificationService.markAsRead(notificationId);
  }

  @Patch('read-all')
  async markAllRead() {
    return this.notificationService.markAllAsRead();
  }
}

AI Coding Instructions

  • Use createNotification() as the shared creation path; feature-specific methods such as notifyNewComment() should delegate to it rather than duplicating persistence logic.
  • Trigger notification methods from the relevant domain workflow only after the related comment, mention, or suggestion has been successfully created.
  • Ensure notification queries and read-state updates are scoped to the authenticated user to prevent cross-user notification access.
  • Prefer markAllAsRead() for bulk read-state updates instead of iterating through notifications in application code.
  • Keep notification payloads consistent across mention, comment, and suggestion flows so clients can render them predictably.

Relationships

  • DEPENDS_ON → PrismaService

Referenced By

  • CommentService (DEPENDS_ON)
  • DocAutomationService (DEPENDS_ON)
  • NotificationController (DEPENDS_ON)
  • NotificationModule (MODULE_PROVIDES)
  • NotificationModule (MODULE_EXPORTS)
  • SuggestionService (DEPENDS_ON)
  • TechnicalDocsGenerationService (DEPENDS_ON)

Was this page helpful?

Download as PDF