diff --git a/src/notifications/notification.controller.ts b/src/notifications/notification.controller.ts index 7d18fea..6306fdd 100644 --- a/src/notifications/notification.controller.ts +++ b/src/notifications/notification.controller.ts @@ -4,10 +4,12 @@ import { NotificationService } from './notification.service'; @Controller('notifications') export class NotificationController { - constructor(private notificationService: NotificationService) {} + constructor(private readonly notificationService: NotificationService) {} @Post('send') - async sendNotification(@Body() notificationData: NotificationDto) { + async sendNotification( + @Body() notificationData: NotificationDto, + ): Promise<{ status: number; message: string }> { return await this.notificationService.sendNotification(notificationData); } } diff --git a/src/notifications/notification.service.ts b/src/notifications/notification.service.ts index 046d215..f690502 100644 --- a/src/notifications/notification.service.ts +++ b/src/notifications/notification.service.ts @@ -4,13 +4,13 @@ import { NotificationDto } from './notification.dto'; @Injectable() export class NotificationService { - constructor(private socketGateway: SocketGateway) {} + constructor(private readonly socketGateway: SocketGateway) {} - async sendNotification(notificationData: NotificationDto) { - const auth: string[] = []; - auth.push(notificationData.telephone_id); + async sendNotification( + notificationData: NotificationDto, + ): Promise<{ status: number; message: string }> { return await this.socketGateway.handleSendMessage({ - auth, + auths: [notificationData.telephone_id], data: notificationData, event: 'answer', }); diff --git a/src/socket/dto/sendMessage.dto.ts b/src/socket/dto/sendMessage.dto.ts index 0324a8f..7eedca5 100644 --- a/src/socket/dto/sendMessage.dto.ts +++ b/src/socket/dto/sendMessage.dto.ts @@ -1,5 +1,5 @@ export class SendMessageDto { - auth: string[]; + auths: string[]; data: any; event: string; } diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index efb704d..47770af 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -15,13 +15,13 @@ export class SocketGateway implements OnGatewayConnection { constructor(private socketService: SocketService) {} async handleConnection(client: Socket) { - await this.socketService.ClientConnection({ + await this.socketService.clientConnection({ client_id: client.handshake.auth.token, socket_id: client.id, event: 'connected', }); client.on('disconnect', async (reason) => { - await this.socketService.ClientConnection({ + await this.socketService.clientConnection({ client_id: client.handshake.auth.token, socket_id: client.id, event: 'disconnected', @@ -31,28 +31,31 @@ export class SocketGateway implements OnGatewayConnection { } @SubscribeMessage('sendMessageToClients') - handleSendMessage(sendMessageDto: SendMessageDto) { - const clients = this.getConnectedClients(sendMessageDto.auth); + async handleSendMessage(sendMessageDto: SendMessageDto) { + const clients = this.getConnectedClients(sendMessageDto.auths); if (clients.length === 0) { return { status: 0, message: 'Failed to find the clients!', }; } - return this.socketService.sendMessageToClients( + + const result = await this.socketService.sendMessageToClients( clients, sendMessageDto.data, sendMessageDto.event, ); + + return result; } - getConnectedClients(auth: string[]): Socket[] { + getConnectedClients(auths: string[]): Socket[] { const connectedClients = this.server.sockets.sockets; + const clients: Socket[] = []; - const clients = []; - for (const connectedClient of connectedClients) { - if (auth.includes(connectedClient[1].handshake.auth.token)) { - clients.push(connectedClient[1]); + for (const client of connectedClients.values()) { + if (auths.includes(client.handshake.auth.token)) { + clients.push(client); } } diff --git a/src/socket/socket.service.ts b/src/socket/socket.service.ts index 1af0444..a690fcf 100644 --- a/src/socket/socket.service.ts +++ b/src/socket/socket.service.ts @@ -12,49 +12,38 @@ export class SocketService { private socketConnectionRepository: Repository, ) {} - async ClientConnection(data: ClientConnectionDto) { + async clientConnection(data: ClientConnectionDto) { const client = this.socketConnectionRepository.create(data); await this.socketConnectionRepository.save(client); } - sendMessageToClients(clients: Socket[], data: any, event: string) { - return new Promise(async (resolveHandlerSend) => { - let sent: boolean = false; - - try { - for (const client of clients) { - sent = await new Promise((resolveSend) => { - client - .timeout(2000) - .emit(event, JSON.stringify(data), (err: any) => { - if (err) { - resolveSend(false); - } else { - resolveSend(true); - } - }); + async sendMessageToClients( + clients: Socket[], + data: any, + event: string, + ): Promise<{ status: number; message: string }> { + const promises = clients.map( + (client) => + new Promise((resolve) => { + client.timeout(2000).emit(event, JSON.stringify(data), (err: any) => { + resolve(!err); }); - } - } catch (error) { - resolveHandlerSend({ - status: 0, - message: 'Failed to send to the clients!', - }); - return; - } + }), + ); - if (!sent) { - resolveHandlerSend({ - status: 0, - message: 'Failed to send to the clients!', - }); - return; - } + const results = await Promise.all(promises); + const allSent = results.every((result) => result); - resolveHandlerSend({ - status: 1, - message: 'Success in sending to the clients!', - }); - }); + if (!allSent) { + return { + status: 0, + message: 'Failed to send to all clients!', + }; + } + + return { + status: 1, + message: 'Success in sending to all clients!', + }; } }