From 67bb743a9ed273be6d43e71322d706ca2b31ff76 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Sun, 29 Oct 2023 14:02:33 +0330 Subject: [PATCH] implementation of module and service socket --- src/app.module.ts | 4 +- src/notifications/notification.controller.ts | 14 +++---- ...otification.dto.ts => notification.dto.ts} | 2 +- .../notification.interface.ts | 0 src/notifications/notification.module.ts | 12 ++++++ src/notifications/notification.mudule.ts | 15 ------- src/notifications/notification.service.ts | 10 +++++ src/socket/socket.gateway.ts | 30 +------------- src/socket/{models => }/socket.model.ts | 0 src/socket/socket.module.ts | 13 ++++++ src/socket/socket.service.ts | 41 ++++++++++++++++++- 11 files changed, 86 insertions(+), 55 deletions(-) rename src/notifications/{validations/sendNotification.dto.ts => notification.dto.ts} (86%) rename src/notifications/{interfaces => }/notification.interface.ts (100%) create mode 100644 src/notifications/notification.module.ts delete mode 100644 src/notifications/notification.mudule.ts rename src/socket/{models => }/socket.model.ts (100%) create mode 100644 src/socket/socket.module.ts diff --git a/src/app.module.ts b/src/app.module.ts index e63453d..dc8126b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,8 @@ import { Module } from "@nestjs/common"; -import { NotificationModule } from "./notifications/notification.mudule"; +import { NotificationModule } from "./notifications/notification.module"; import { SequelizeModule } from "@nestjs/sequelize"; import { ConfigModule } from "@nestjs/config"; +import { SocketModule } from "./socket/socket.module"; @Module({ imports: [ @@ -17,6 +18,7 @@ import { ConfigModule } from "@nestjs/config"; synchronize: true, }), NotificationModule, + SocketModule, ], }) export class AppModule { diff --git a/src/notifications/notification.controller.ts b/src/notifications/notification.controller.ts index e5ed102..9a57106 100644 --- a/src/notifications/notification.controller.ts +++ b/src/notifications/notification.controller.ts @@ -1,18 +1,16 @@ import { Body, Controller, Post } from "@nestjs/common"; +import { NotificationDto } from "./notification.dto"; import { NotificationService } from "./notification.service"; -import { SendNotificationDto } from "./validations/sendNotification.dto"; -import { SocketGateway } from "../socket/socket.gateway"; @Controller("notifications") export class NotificationController { - constructor( - private readonly notificationService: NotificationService, - private socketGateway: SocketGateway, - ) { + + constructor(private notificationService: NotificationService) { } + @Post("send") - async sendNotification(@Body() notificationData: SendNotificationDto) { - return await this.socketGateway.handleSendNotification(notificationData); + async sendNotification(@Body() notificationData: NotificationDto) { + return await this.notificationService.sendNotification(notificationData); } } \ No newline at end of file diff --git a/src/notifications/validations/sendNotification.dto.ts b/src/notifications/notification.dto.ts similarity index 86% rename from src/notifications/validations/sendNotification.dto.ts rename to src/notifications/notification.dto.ts index d49cfff..5545ad1 100644 --- a/src/notifications/validations/sendNotification.dto.ts +++ b/src/notifications/notification.dto.ts @@ -1,6 +1,6 @@ import { IsInt, IsNotEmpty, IsString } from "class-validator"; -export class SendNotificationDto { +export class NotificationDto { @IsNotEmpty() @IsInt() call_id: number; diff --git a/src/notifications/interfaces/notification.interface.ts b/src/notifications/notification.interface.ts similarity index 100% rename from src/notifications/interfaces/notification.interface.ts rename to src/notifications/notification.interface.ts diff --git a/src/notifications/notification.module.ts b/src/notifications/notification.module.ts new file mode 100644 index 0000000..bf548e3 --- /dev/null +++ b/src/notifications/notification.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { NotificationController } from "./notification.controller"; +import { NotificationService } from "./notification.service"; +import { SocketModule } from "../socket/socket.module"; + +@Module({ + imports: [SocketModule], + controllers: [NotificationController], + providers: [NotificationService], +}) +export class NotificationModule { +} \ No newline at end of file diff --git a/src/notifications/notification.mudule.ts b/src/notifications/notification.mudule.ts deleted file mode 100644 index a5c5dba..0000000 --- a/src/notifications/notification.mudule.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Module } from "@nestjs/common"; -import { NotificationController } from "./notification.controller"; -import { NotificationService } from "./notification.service"; -import { SocketGateway } from "../socket/socket.gateway"; -import { SocketService } from "../socket/socket.service"; -import { SequelizeModule } from "@nestjs/sequelize"; -import { SocketModel } from "../socket/models/socket.model"; - -@Module({ - imports: [SequelizeModule.forFeature([SocketModel])], - controllers: [NotificationController], - providers: [NotificationService, SocketGateway, SocketService], -}) -export class NotificationModule { -} \ No newline at end of file diff --git a/src/notifications/notification.service.ts b/src/notifications/notification.service.ts index bb78ba3..aa8d59e 100644 --- a/src/notifications/notification.service.ts +++ b/src/notifications/notification.service.ts @@ -1,5 +1,15 @@ import { Injectable } from "@nestjs/common"; +import { SocketGateway } from "../socket/socket.gateway"; +import { NotificationInterface } from "./notification.interface"; @Injectable() export class NotificationService { + constructor( + private socketGateway: SocketGateway, + ) { + } + + async sendNotification(notificationData: NotificationInterface) { + return await this.socketGateway.handleSendNotification(notificationData); + } } \ No newline at end of file diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index 1610784..18a0d69 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -1,6 +1,6 @@ import { OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets"; import { Server, Socket } from "socket.io"; -import { NotificationInterface } from "../notifications/interfaces/notification.interface"; +import { NotificationInterface } from "../notifications/notification.interface"; import { SocketService } from "./socket.service"; @WebSocketGateway() @@ -30,32 +30,6 @@ export class SocketGateway implements OnGatewayConnection { @SubscribeMessage("sendNotificationToClients") handleSendNotification(notificationData: NotificationInterface) { - return new Promise(async (resolve) => { - const connectedClients = this.server.sockets.sockets; - - if (connectedClients.size === 0) resolve({ status: "failed", message: "Clients not found!" }); - - let sent: any = false; - - for (const connectedClient of connectedClients) { - if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) { - sent = await new Promise((resolve) => { - connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => { - if (err) { - resolve(false); - } else { - resolve(true); - } - }); - }); - } - } - - if (sent) { - resolve({ status: "success", message: "Sent to clients" }); - } else { - resolve({ status: "failed", message: "Not sent to clients" }); - } - }); + return this.socketService.sendNotificationToClients(this.server, notificationData); } } \ No newline at end of file diff --git a/src/socket/models/socket.model.ts b/src/socket/socket.model.ts similarity index 100% rename from src/socket/models/socket.model.ts rename to src/socket/socket.model.ts diff --git a/src/socket/socket.module.ts b/src/socket/socket.module.ts new file mode 100644 index 0000000..b44fce1 --- /dev/null +++ b/src/socket/socket.module.ts @@ -0,0 +1,13 @@ +import { Module } from "@nestjs/common"; +import { SocketGateway } from "./socket.gateway"; +import { SocketService } from "./socket.service"; +import { SequelizeModule } from "@nestjs/sequelize"; +import { SocketModel } from "./socket.model"; + +@Module({ + imports: [SequelizeModule.forFeature([SocketModel])], + providers: [SocketGateway, SocketService], + exports: [SocketGateway], +}) +export class SocketModule { +} diff --git a/src/socket/socket.service.ts b/src/socket/socket.service.ts index c13903f..bde948d 100644 --- a/src/socket/socket.service.ts +++ b/src/socket/socket.service.ts @@ -1,6 +1,8 @@ import { Injectable } from "@nestjs/common"; import { InjectModel } from "@nestjs/sequelize"; -import { SocketModel } from "./models/socket.model"; +import { SocketModel } from "./socket.model"; +import { Server } from "socket.io"; +import { NotificationInterface } from "../notifications/notification.interface"; @Injectable() export class SocketService { @@ -10,7 +12,42 @@ export class SocketService { ) { } - ClientConnection(data) { + ClientConnection(data: any) { this.socketModel.create(data); } + + sendNotificationToClients(server: Server, notificationData: NotificationInterface) { + return new Promise(async (resolveHandlerSend) => { + const connectedClients = server.sockets.sockets; + + if (connectedClients.size === 0) { + resolveHandlerSend({ status: 404, message: "Failed to find the clients!" }); + return; + } + + let sent: any = false; + + for (const connectedClient of connectedClients) { + if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) { + sent = await new Promise((resolveSend) => { + connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => { + if (err) { + resolveSend(false); + } else { + resolveSend(true); + } + }); + }); + } + } + + if (!sent) { + resolveHandlerSend({ status: 404, message: "Failed to send to the clients!" }); + return; + } + + resolveHandlerSend({ status: 200, message: "Success in sending to the clients!" }); + }); + } + }