init notification
This commit is contained in:
21
src/app.module.ts
Normal file
21
src/app.module.ts
Normal 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
13
src/main.ts
Normal 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();
|
||||
5
src/notifications/interfaces/notification.interface.ts
Normal file
5
src/notifications/interfaces/notification.interface.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export class NotificationInterface {
|
||||
call_id: number;
|
||||
telephone_id: string;
|
||||
phone_number: string;
|
||||
}
|
||||
18
src/notifications/notification.controller.ts
Normal file
18
src/notifications/notification.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
15
src/notifications/notification.mudule.ts
Normal file
15
src/notifications/notification.mudule.ts
Normal 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 {
|
||||
}
|
||||
5
src/notifications/notification.service.ts
Normal file
5
src/notifications/notification.service.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class NotificationService {
|
||||
}
|
||||
15
src/notifications/validations/sendNotification.dto.ts
Normal file
15
src/notifications/validations/sendNotification.dto.ts
Normal 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;
|
||||
}
|
||||
24
src/socket/models/socket.model.ts
Normal file
24
src/socket/models/socket.model.ts
Normal 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;
|
||||
}
|
||||
61
src/socket/socket.gateway.ts
Normal file
61
src/socket/socket.gateway.ts
Normal 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" });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
16
src/socket/socket.service.ts
Normal file
16
src/socket/socket.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user