import { Injectable } from '@nestjs/common' ; import { Repository } from 'typeorm' ; import { InjectRepository } from '@nestjs/typeorm' ; import { User } from '../auth/user.entity' ; import { Chatroom } from './chatroom.entity' ; import { Message } from './message.entity' ; import { SecurityService } from '../common/security/security.service' ; import { ChatCache } from './chat.cache' ; import { UserJWT } from '../auth/interfaces/user-JWT.interface' ; @ Injectable ( ) export class ChatService { constructor ( @ InjectRepository ( Chatroom ) private readonly chatRepository , @ InjectRepository ( Message ) private readonly messageRepository , @ InjectRepository ( User ) private readonly userRepository , private readonly securityService , private readonly chatCache ) { } async findChatroomByName ( roomName ) { return this . chatRepository . findOne ( { where : { name : roomName } } ) ; } async createChatroom ( roomName , userJWT ) { const user = await this . userRepository . findOne ( userJWT . sub ) ; const chatroom = new Chatroom ( ) ; chatroom . name = roomName ; chatroom . ownedBy = user ; chatroom . populatedBy = [ user ] ; this . chatRepository . save ( chatroom ) ; return chatroom ; } async addMessage ( messageData , userJWT ) { const user = await this . userRepository . findOne ( userJWT . sub ) ; const room = await this . findChatroomByName ( messageData . roomName ) ; const message = new Message ( ) ; message . chatroom = room ; message . createdDate = new Date ( ) ; message . message = messageData . message ; message . user = user ; this . messageRepository . save ( message ) ; return message ; } }