import { HttpException , Injectable } from '@nestjs/common' ; import { Client , ClientProxy , Transport } from '@nestjs/microservices' ; import { IPagination } from '../common/entities/pagination.entity' ; import { CreateMessageDto , CreateThreadDto , FindOtherParticipantsInThreadDto , FindParticipantInThreadDto , } from './message.dto' ; import { IMessage , IParticipant , IThread } from './message.interface' ; @ Injectable ( ) export class MessageService { @ Client ( { options : { host : 'message' , port : 3000 } , transport : Transport . TCP , } ) private client : ClientProxy ; public async findAllThreads ( userId ) { return this . client . send < IThread [ ] > ( { cmd : 'getAllThreads' } , userId ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async findThreadFromParticipantsIds ( ids ) { return this . client . send < IThread > ( { cmd : 'getThreadFromParticipantsIds' } , ids ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async createThread ( createThreadDto ) { return this . client . send < IThread > ( { cmd : 'createThread' } , createThreadDto ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async findAllThreadMessages ( threadId , pagination ) { return this . client . send < IMessage [ ] > ( { cmd : 'getAllThreadMessages' } , { threadId , pagination } ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async findLatestThreadMessage ( threadId ) { return this . client . send < IMessage [ ] > ( { cmd : 'getLatestThreadMessage' } , threadId ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async checkIfThreadHasNewMessage ( threadId , userId ) { return this . client . send < boolean > ( { cmd : 'checkIfThreadHasNewMessage' } , { threadId , userId } ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async findParticipantInThread ( findParticipantInThreadDto ) { return this . client . send < IParticipant > ( { cmd : 'findParticipantInThread' } , findParticipantInThreadDto ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async findOtherParticipantsInThread ( findOtherParticipantsInThreadDto ) { return this . client . send < IParticipant [ ] > ( { cmd : 'findOtherParticipantsInThread' } , findOtherParticipantsInThreadDto ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async createMessage ( createMessageDto ) { return this . client . send < IMessage > ( { cmd : 'createMessage' } , createMessageDto ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } public async unNewThreadMessages ( threadId ) { return this . client . send < any > ( { cmd : 'unNewThreadMessages' } , threadId ) . toPromise ( ) . catch ( error => { throw new HttpException ( error , error . status ) ; } ) ; } }