src/notification/notification.controller.ts
notifications
Methods |
|
| Async delete | |||||||||
delete(id: number, request)
|
|||||||||
Decorators :
@Delete(':id')
|
|||||||||
|
Parameters :
Returns :
Promise<Notification>
|
| Async findAllForUser | |||||||||
findAllForUser(request, queryOptions: literal type)
|
|||||||||
Decorators :
@Get()
|
|||||||||
|
Parameters :
Returns :
Promise<Notification[]>
|
| Async update | ||||||||||||
update(id: number, updateData: UpdateNotificationDto, request)
|
||||||||||||
Decorators :
@Patch(':id')
|
||||||||||||
|
Parameters :
Returns :
Promise<Notification>
|
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);
}
}