Skip to content

Commit

Permalink
Merge pull request #146 from modern-agile-team/feature/notice
Browse files Browse the repository at this point in the history
Modify(hobiJeong/notice): Use Interceptors for response type
  • Loading branch information
hobiJeong authored Nov 22, 2023
2 parents ce2b5fc + f7b4d62 commit ed42d59
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/boards/controllers/boards-like.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@ import {
ValidationPipe,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { BoardsLikeService } from '../services/boards-like.service';
import { ApiTags } from '@nestjs/swagger';
import { ApiAddBoardLike } from '../swagger-decorators/add-board-like.decorator';
import { ApiGetBoardLikeCount } from '../swagger-decorators/get-board-like-count.decorator';
import { ApiDeleteBoardLike } from '../swagger-decorators/delete-board-like.decorator';
import { TokenService } from 'src/auth/services/token.service';
import { GetUserId } from 'src/common/decorators/get-userId.decorator';
import { JwtAccessTokenGuard } from 'src/config/guards/jwt-access-token.guard';
import { JwtOptionalGuard } from 'src/config/guards/jwt-optional.guard';
import { SuccessResponseInterceptor } from 'src/common/interceptors/success-response.interceptor';

@ApiTags('BOARDS-LIKE')
@UsePipes(ValidationPipe)
@UseInterceptors(SuccessResponseInterceptor)
@Controller('boards')
export class BoardsLikeController {
constructor(
private tokenService: TokenService,
private boardsLikeService: BoardsLikeService,
) {}
constructor(private boardsLikeService: BoardsLikeService) {}

@ApiAddBoardLike()
@Post('like/:boardId')
Expand Down
4 changes: 2 additions & 2 deletions src/chat/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ApiGetChatNotificationSse } from '../swagger-decorators/get-chat-notifi
import { TokenService } from 'src/auth/services/token.service';
import { GetUserId } from 'src/common/decorators/get-userId.decorator';
import { JwtAccessTokenGuard } from 'src/config/guards/jwt-access-token.guard';
import { GetNotificationsResponseDto } from '../dto/get-notifications-response.dto';
import { GetNotificationsResponseFromChatDto } from '../dto/get-notifications-response-from-chat.dto';
import { ApiGetChatNotifications } from '../swagger-decorators/get-chat-notifications.decorator';
import { ApiCreateChatImage } from '../swagger-decorators/create-chat-image.decorators';
import { SuccessResponseInterceptor } from 'src/common/interceptors/success-response.interceptor';
Expand Down Expand Up @@ -119,7 +119,7 @@ export class ChatController {
@Get('chat/notice')
async getChatNotifications(
@GetUserId() userId: number,
): Promise<GetNotificationsResponseDto[]> {
): Promise<GetNotificationsResponseFromChatDto[]> {
return this.chatService.getChatNotifications(userId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';

export class GetNotificationsResponseDto {
export class GetNotificationsResponseFromChatDto {
_id: mongoose.Types.ObjectId;

chatroom_id: mongoose.Types.ObjectId;
Expand Down
4 changes: 2 additions & 2 deletions src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Subject, catchError, map } from 'rxjs';
import { Chat } from '../schemas/chat.schemas';
import { User } from 'src/users/entities/user.entity';
import { EntityManager } from 'typeorm';
import { GetNotificationsResponseDto } from '../dto/get-notifications-response.dto';
import { GetNotificationsResponseFromChatDto } from '../dto/get-notifications-response-from-chat.dto';

@Injectable()
export class ChatService {
Expand Down Expand Up @@ -202,7 +202,7 @@ export class ChatService {

async getChatNotifications(
userId: number,
): Promise<GetNotificationsResponseDto[]> {
): Promise<GetNotificationsResponseFromChatDto[]> {
const isUser = await this.entityManager.findOne(User, {
where: { id: userId },
});
Expand Down
7 changes: 7 additions & 0 deletions src/common/dto/get-notifications-response-from-board.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OmitType } from '@nestjs/swagger';
import { BoardNotification } from '../notice/entities/board-notice.entity';

export class GetNotificationsResponseFromBoardDto extends OmitType(
BoardNotification,
['board', 'receiver', 'sender'],
) {}
14 changes: 11 additions & 3 deletions src/common/notice/controllers/notice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {
Controller,
Delete,
Get,
Headers,
Param,
ParseIntPipe,
Patch,
UseGuards,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
Expand All @@ -15,9 +16,14 @@ import { ApiTags } from '@nestjs/swagger';
import { ApiGetAllNotifications } from '../swagger-decorators/get-all-notifications.decorator';
import { ApiUpdateUnSeenNotification } from '../swagger-decorators/update-un-seen-notification.decorator';
import { ApiHardDeleteNotificatons } from '../swagger-decorators/hard-delete-notifications.decorator';
import { SuccessResponseInterceptor } from 'src/common/interceptors/success-response.interceptor';
import { GetNotificationsResponseFromBoardDto } from 'src/common/dto/get-notifications-response-from-board.dto';
import { JwtAccessTokenGuard } from 'src/config/guards/jwt-access-token.guard';
import { GetUserId } from 'src/common/decorators/get-userId.decorator';

@ApiTags('BOARD-NOTICE')
@UsePipes(ValidationPipe)
@UseInterceptors(SuccessResponseInterceptor)
@Controller('notice')
export class NoticeController {
constructor(
Expand All @@ -26,9 +32,11 @@ export class NoticeController {
) {}

@ApiGetAllNotifications()
@UseGuards(JwtAccessTokenGuard)
@Get()
async getAllNotifications(@Headers('access_token') accessToken: string) {
const userId = await this.tokenService.decodeToken(accessToken);
async getAllNotifications(
@GetUserId() userId: number,
): Promise<GetNotificationsResponseFromBoardDto[]> {
return this.noticeService.getAllNotifications(userId);
}

Expand Down
5 changes: 4 additions & 1 deletion src/common/notice/repositories/notice.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { EntityManager, LessThan } from 'typeorm';
import { BoardNotification, Separator } from '../entities/board-notice.entity';
import { GetNotificationsResponseFromBoardDto } from 'src/common/dto/get-notifications-response-from-board.dto';

@Injectable()
export class NoticeRepository {
constructor(private entityManager: EntityManager) {}

async getAllNotifications(userId: number) {
async getAllNotifications(
userId: number,
): Promise<GetNotificationsResponseFromBoardDto[]> {
const notifications = await this.entityManager.find(BoardNotification, {
skip: 1,
take: 10,
Expand Down
20 changes: 16 additions & 4 deletions src/common/notice/services/notice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { NoticeRepository } from '../repositories/notice.repository';
import { EntityManager } from 'typeorm';
import { User } from 'src/users/entities/user.entity';
import { GetNotificationsResponseFromBoardDto } from 'src/common/dto/get-notifications-response-from-board.dto';

@Injectable()
export class NoticeService {
Expand All @@ -10,7 +11,9 @@ export class NoticeService {
private entityManager: EntityManager,
) {}

async getAllNotifications(userId: number) {
async getAllNotifications(
userId: number,
): Promise<GetNotificationsResponseFromBoardDto[]> {
const isUser = await this.entityManager.findOne(User, {
where: { id: userId },
});
Expand All @@ -24,7 +27,10 @@ export class NoticeService {
boardId: number,
senderId: number,
receiverId: number,
) {
): Promise<void | GetNotificationsResponseFromBoardDto> {
if (senderId === receiverId) {
return;
}
return this.noticeRepository.createBoardNoticeFromComment(
boardId,
senderId,
Expand All @@ -36,7 +42,10 @@ export class NoticeService {
boardId: number,
senderId: number,
receiverId: number,
) {
): Promise<void | GetNotificationsResponseFromBoardDto> {
if (senderId === receiverId) {
return;
}
return this.noticeRepository.createCommentNoticeFromReComment(
boardId,
senderId,
Expand All @@ -48,7 +57,10 @@ export class NoticeService {
boardId: number,
senderId: number,
receiverId: number,
) {
): Promise<void | GetNotificationsResponseFromBoardDto> {
if (senderId === receiverId) {
return;
}
return this.noticeRepository.createBoardNoticeFromLike(
boardId,
senderId,
Expand Down

0 comments on commit ed42d59

Please sign in to comment.