init notification

This commit is contained in:
AmirHossein Mahmoodi
2023-10-24 10:55:50 +03:30
parent b95ed3b449
commit 416e05c31e
28 changed files with 8181 additions and 1701 deletions

21
src/app.module.ts Normal file
View File

@@ -0,0 +1,21 @@
import { Module } from "@nestjs/common";
import { NotificationModule } from "./notifications/notification.mudule";
import { SequelizeModule } from "@nestjs/sequelize";
@Module({
imports: [
SequelizeModule.forRoot({
dialect: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "",
database: "notification_db",
autoLoadModels: true,
synchronize: true,
}),
NotificationModule,
],
})
export class AppModule {
}

13
src/main.ts Normal file
View File

@@ -0,0 +1,13 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ValidationPipe } from "@nestjs/common";
import { IoAdapter } from "@nestjs/platform-socket.io";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
app.useWebSocketAdapter(new IoAdapter(app));
await app.listen(3001);
}
bootstrap();

View File

@@ -0,0 +1,5 @@
export class NotificationInterface {
call_id: number;
telephone_id: string;
phone_number: string;
}

View File

@@ -0,0 +1,18 @@
import { Body, Controller, Post } from "@nestjs/common";
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,
) {
}
@Post("send")
async sendNotification(@Body() notificationData: SendNotificationDto) {
return await this.socketGateway.handleSendNotification(notificationData);
}
}

View File

@@ -0,0 +1,15 @@
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 {
}

View File

@@ -0,0 +1,5 @@
import { Injectable } from "@nestjs/common";
@Injectable()
export class NotificationService {
}

View File

@@ -0,0 +1,15 @@
import { IsInt, IsNotEmpty, IsString } from "class-validator";
export class SendNotificationDto {
@IsNotEmpty()
@IsInt()
call_id: number;
@IsNotEmpty()
@IsString()
telephone_id: string;
@IsNotEmpty()
@IsString()
phone_number: string;
}

View File

@@ -0,0 +1,24 @@
import { Column, DataType, Model, Table } from "sequelize-typescript";
@Table({ updatedAt: false, tableName: "notifications" })
export class SocketModel extends Model {
@Column({
type: DataType.BIGINT,
allowNull: false,
autoIncrement: true,
primaryKey: true,
})
id: number;
@Column({ allowNull: false })
client_id: string;
@Column({ allowNull: false })
socket_id: string;
@Column({ allowNull: false })
event: string;
@Column
description: string;
}

View File

@@ -0,0 +1,61 @@
import { OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
import { Server, Socket } from "socket.io";
import { NotificationInterface } from "../notifications/interfaces/notification.interface";
import { SocketService } from "./socket.service";
@WebSocketGateway()
export class SocketGateway implements OnGatewayConnection {
@WebSocketServer() server: Server;
constructor(
private socketService: SocketService,
) {
}
handleConnection(client: Socket) {
this.socketService.ClientConnection({
client_id: client.handshake.auth.token,
socket_id: client.id,
event: "connected",
});
client.on("disconnect", (reason) => {
this.socketService.ClientConnection({
client_id: client.handshake.auth.token,
socket_id: client.id,
event: "disconnected",
description: reason,
});
});
}
@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" });
}
});
}
}

View File

@@ -0,0 +1,16 @@
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/sequelize";
import { SocketModel } from "./models/socket.model";
@Injectable()
export class SocketService {
constructor(
@InjectModel(SocketModel)
private socketModel: typeof SocketModel,
) {
}
ClientConnection(data) {
this.socketModel.create(data);
}
}