File

src/notification/notification.controller.ts

Prefix

notifications

Index

Methods

Methods

Async delete
delete(id: number, request)
Decorators :
@Delete(':id')
@ApiOkResponse({type: Notification})
@UsePipes(validationPipe)
Parameters :
Name Type Optional
id number No
request No
Async findAllForUser
findAllForUser(request, queryOptions: literal type)
Decorators :
@Get()
@ApiOkResponse({type: Notification})
Parameters :
Name Type Optional
request No
queryOptions literal type No
Async update
update(id: number, updateData: UpdateNotificationDto, request)
Decorators :
@Patch(':id')
@ApiOkResponse({type: Notification})
@UsePipes(validationPipe)
Parameters :
Name Type Optional
id number No
updateData UpdateNotificationDto No
request No
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Query,
  Request,
  UnauthorizedException,
  UsePipes,
  ValidationPipe,
} from "@nestjs/common";
import { ApiOkResponse } from "@nestjs/swagger";
import { NotificationService } from "./notification.service";
import { Notification } from "./entities/notification.entity";
import { UpdateNotificationDto } from "./dto/update-notification.dto";

const validationPipe = new ValidationPipe({
  whitelist: true,
  forbidNonWhitelisted: true,
});

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

  @Get()
  @ApiOkResponse({ type: Notification })
  async findAllForUser(
    @Request() request,
    @Query()
    queryOptions: {
      pageSize?: number;
      pageOffset?: number;
      startId?: number;
    },
  ): Promise<Notification[]> {
    const userId = request.user.id;
    return this.notificationService.findAllByUserId(userId, queryOptions);
  }

  @Patch(":id")
  @ApiOkResponse({ type: Notification })
  @UsePipes(validationPipe)
  async update(
    @Param("id") id: number,
    @Body() updateData: UpdateNotificationDto,
    @Request() request,
  ): Promise<Notification> {
    const notification = await this.notificationService.getNotification(+id);

    if (!notification) {
      throw new Error("Notification not found");
    }

    if (notification.userId !== request.user.id) {
      throw new UnauthorizedException();
    }

    return this.notificationService.update(+id, updateData);
  }

  @Delete(":id")
  @ApiOkResponse({ type: Notification })
  @UsePipes(validationPipe)
  async delete(
    @Param("id") id: number,
    @Request() request,
  ): Promise<Notification> {
    const notification = await this.notificationService.getNotification(+id);

    if (!notification) {
      throw new Error("Notification not found");
    }

    if (notification.userId !== request.user.id) {
      throw new UnauthorizedException();
    }

    return this.notificationService.delete(+id);
  }
}

results matching ""

    No results matching ""