All files / libs/kernel/user-notifications/in-app/core/src/lib notification.controller.ts

97.05% Statements 66/68
27.27% Branches 3/11
100% Functions 3/3
97.05% Lines 66/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x  
import { BadRequestException, Body, Controller, Get, Inject, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company, User } from '@amalia/core/models';
import { Notification as NotificationDto } from '@amalia/core/types';
import { PaginatedResponse } from '@amalia/core-http-types';
import { NumberPipe } from '@amalia/kernel/api';
import { AmaliaAuthGuard, CurrentUser, CurrentUserCompany, PoliciesGuard } from '@amalia/kernel/auth/core';
 
import { formatNotifications } from './dto/notification.dto';
import { NotificationService } from './notification.service';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('notifications')
@Controller('notifications')
export class NotificationController {
  public constructor(
    @Inject(NotificationService)
    private readonly notificationService: NotificationService,
  ) {}
 
  /**
   * Find all notifications for a user and company.
   * @param user
   * @param company
   * @param page
   * @param limit
   */
  @Get()
  public async findMyNotifications(
    @CurrentUser() user: User,
    @CurrentUserCompany() company: Company,
    @Query('page', new NumberPipe({ optional: true })) page: number = 1,
    @Query('limit', new NumberPipe({ optional: true })) limit: number = 10,
  ): Promise<PaginatedResponse<NotificationDto> & { unreadCount: number }> {
    const [{ items, ...rest }, unreadCount] = await Promise.all([
      this.notificationService.listUserNotifications(user, company, { limit: Math.min(limit, 50), page: page || 1 }),
      this.notificationService.countUnreadNotifications(user, company),
    ]);
 
    return {
      items: items.map(formatNotifications),
      unreadCount,
      ...rest,
    };
  }
 
  /**
   * Mark all notifications as read.
   * @param user
   * @param company
   * @param body
   */
  @Post('/read-notifications')
  public async markNotificationsAsRead(
    @CurrentUser() user: User,
    @CurrentUserCompany() company: Company,
    @Body() body: { lastCheck: string },
  ) {
    const { lastCheck } = body;
    if (!lastCheck) {
      throw new BadRequestException('Missing lastCheck');
    }
 
    await this.notificationService.markAllNotificationsAsRead(user, company, new Date(lastCheck));
  }
}