diff --git a/Dockerfile b/Dockerfile index 0e51328..0721e79 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,6 @@ FROM builder WORKDIR /app COPY package*.json ./ -ENV NODE_ENV=Production RUN npm install COPY --from=builder /app/dist ./dist EXPOSE ${PORT} diff --git a/src/chat/chat.controller.ts b/src/chat/chat.controller.ts index 14d480a..5a4a786 100644 --- a/src/chat/chat.controller.ts +++ b/src/chat/chat.controller.ts @@ -3,7 +3,11 @@ import { Controller, Post, Body, Get, Query } from '@nestjs/common'; import { ChatService } from './chat.service'; import { CreateChatDto } from './dto/create-chat.dto'; import { Chat } from './entities/chat.entity'; -import { FindAllChatDto, FindChatDto } from './dto/find-chat.dto'; +import { + FindAllChatDto, + FindAllInMinuteChatDto, + FindChatDto, +} from './dto/find-chat.dto'; @Controller('chat') export class ChatController { @@ -19,9 +23,14 @@ export class ChatController { return await this.chatService.findAll(dto); } + @Get('/minute') + async findAllInMinute(@Query() dto: FindAllInMinuteChatDto): Promise { + return await this.chatService.findAllInMinute(dto); + } + @Get() - async findOne(@Query() readChatDto: FindChatDto): Promise { - return await this.chatService.findOne(readChatDto); + async findOne(@Query() dto: FindChatDto): Promise { + return await this.chatService.findOne(dto); } /* diff --git a/src/chat/chat.service.ts b/src/chat/chat.service.ts index 5840ed7..45d83eb 100644 --- a/src/chat/chat.service.ts +++ b/src/chat/chat.service.ts @@ -4,36 +4,63 @@ import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { CreateChatDto } from './dto/create-chat.dto'; - import { Chat } from './entities/chat.entity'; -import { FindAllChatDto, FindChatDto } from './dto/find-chat.dto'; +import { + FindAllChatDto, + FindAllInMinuteChatDto, + FindChatDto, +} from './dto/find-chat.dto'; @Injectable() export class ChatService { constructor(@InjectModel(Chat.name) private chatModel: Model) {} async create(dto: CreateChatDto): Promise { - return await this.chatModel.create(dto); + const result = await this.chatModel.create(dto); + console.log('create', result); + + return result; } async findAll(dto: FindAllChatDto): Promise { - const oneMinute = new Date(Date.now() - 60 * 1000); + const result = await this.chatModel.find({ + senderId: dto.senderId, + receiverId: dto.receiverId, + }); + console.log('findAll', result); - return await this.chatModel.find({ + return result; + } + + async findAllInMinute(dto: FindAllInMinuteChatDto): Promise { + const oneMinuteAgo = new Date( + new Date(dto.createdAt).getTime() - 60 * 1000, + ); + + const result = await this.chatModel.find({ senderId: dto.senderId, receiverId: dto.receiverId, - createdAt: { $gte: oneMinute }, + createdAt: { + $gte: oneMinuteAgo, + $lte: dto.createdAt, + }, }); + console.log('findAllInMinute', result); + + return result; } async findOne(dto: FindChatDto): Promise { - return await this.chatModel + const result = await this.chatModel .findOne({ senderId: dto.senderId, receiverId: dto.receiverId, messageId: dto.messageId, }) .exec(); + console.log('findOne', result); + + return result; } /* diff --git a/src/chat/dto/create-chat.dto.ts b/src/chat/dto/create-chat.dto.ts index 40c771a..acf0e31 100644 --- a/src/chat/dto/create-chat.dto.ts +++ b/src/chat/dto/create-chat.dto.ts @@ -1,4 +1,4 @@ -import { IsDate, IsString } from 'class-validator'; +import { IsDateString, IsString } from 'class-validator'; export class CreateChatDto { @IsString() @@ -13,9 +13,9 @@ export class CreateChatDto { @IsString() message: string; - @IsDate() + @IsDateString() createdAt: Date; - @IsDate() + @IsDateString() updatedAt: Date; } diff --git a/src/chat/dto/find-chat.dto.ts b/src/chat/dto/find-chat.dto.ts index 197e70d..46ef315 100644 --- a/src/chat/dto/find-chat.dto.ts +++ b/src/chat/dto/find-chat.dto.ts @@ -1,14 +1,14 @@ import { IsDateString, IsString } from 'class-validator'; -export class FindChatDto { +export class FindAllInMinuteChatDto { @IsString() senderId: string; @IsString() receiverId: string; - @IsString() - messageId: string; + @IsDateString() + createdAt: Date; } export class FindAllChatDto { @@ -17,7 +17,15 @@ export class FindAllChatDto { @IsString() receiverId: string; +} - @IsDateString() - createdAt: Date; +export class FindChatDto { + @IsString() + senderId: string; + + @IsString() + receiverId: string; + + @IsString() + messageId: string; }