Skip to content

Commit 64ed0fa

Browse files
committed
refactor: integrations router folder structure
1 parent d68d42b commit 64ed0fa

9 files changed

Lines changed: 167 additions & 79 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { PrismaRepository } from '@api/repository/repository.service';
2+
import { websocketController } from '@api/server.module';
3+
import { WAMonitoringService } from '@api/services/monitor.service';
4+
import { Server } from 'http';
5+
6+
export class EventController {
7+
public prismaRepository: PrismaRepository;
8+
public waMonitor: WAMonitoringService;
9+
10+
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
11+
this.prisma = prismaRepository;
12+
this.monitor = waMonitor;
13+
}
14+
15+
public set prisma(prisma: PrismaRepository) {
16+
this.prismaRepository = prisma;
17+
}
18+
19+
public get prisma() {
20+
return this.prismaRepository;
21+
}
22+
23+
public set monitor(waMonitor: WAMonitoringService) {
24+
this.waMonitor = waMonitor;
25+
}
26+
27+
public get monitor() {
28+
return this.waMonitor;
29+
}
30+
31+
public readonly events = [
32+
'APPLICATION_STARTUP',
33+
'QRCODE_UPDATED',
34+
'MESSAGES_SET',
35+
'MESSAGES_UPSERT',
36+
'MESSAGES_EDITED',
37+
'MESSAGES_UPDATE',
38+
'MESSAGES_DELETE',
39+
'SEND_MESSAGE',
40+
'CONTACTS_SET',
41+
'CONTACTS_UPSERT',
42+
'CONTACTS_UPDATE',
43+
'PRESENCE_UPDATE',
44+
'CHATS_SET',
45+
'CHATS_UPSERT',
46+
'CHATS_UPDATE',
47+
'CHATS_DELETE',
48+
'GROUPS_UPSERT',
49+
'GROUP_UPDATE',
50+
'GROUP_PARTICIPANTS_UPDATE',
51+
'CONNECTION_UPDATE',
52+
'LABELS_EDIT',
53+
'LABELS_ASSOCIATION',
54+
'CALL',
55+
'TYPEBOT_START',
56+
'TYPEBOT_CHANGE_STATUS',
57+
'REMOVE_INSTANCE',
58+
'LOGOUT_INSTANCE',
59+
];
60+
61+
public init(httpServer: Server): void {
62+
// websocket
63+
websocketController.init(httpServer);
64+
}
65+
66+
public async emit({
67+
instanceName,
68+
origin,
69+
event,
70+
data,
71+
}: {
72+
instanceName: string;
73+
origin: string;
74+
event: string;
75+
data: Object;
76+
}): Promise<void> {
77+
// websocket
78+
await websocketController.emit({
79+
instanceName,
80+
origin,
81+
event,
82+
data,
83+
});
84+
}
85+
86+
public async set(instanceName: string, data: any): Promise<any> {
87+
// websocket
88+
await websocketController.set(instanceName, data);
89+
}
90+
91+
public async get(instanceName: string): Promise<any> {
92+
// websocket
93+
await websocketController.get(instanceName);
94+
}
95+
}

src/api/integrations/event/websocket/controllers/websocket.controller.ts

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventController } from '@api/controllers/event.controller';
12
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
23
import { PrismaRepository } from '@api/repository/repository.service';
34
import { WAMonitoringService } from '@api/services/monitor.service';
@@ -8,45 +9,13 @@ import { NotFoundException } from '@exceptions';
89
import { Server } from 'http';
910
import { Server as SocketIO } from 'socket.io';
1011

11-
export class WebsocketController {
12+
export class WebsocketController extends EventController {
1213
private io: SocketIO;
13-
private prismaRepository: PrismaRepository;
14-
private waMonitor: WAMonitoringService;
1514
private corsConfig: Array<any>;
16-
private readonly logger = new Logger('SocketStartupService');
17-
public readonly events = [
18-
'APPLICATION_STARTUP',
19-
'QRCODE_UPDATED',
20-
'MESSAGES_SET',
21-
'MESSAGES_UPSERT',
22-
'MESSAGES_EDITED',
23-
'MESSAGES_UPDATE',
24-
'MESSAGES_DELETE',
25-
'SEND_MESSAGE',
26-
'CONTACTS_SET',
27-
'CONTACTS_UPSERT',
28-
'CONTACTS_UPDATE',
29-
'PRESENCE_UPDATE',
30-
'CHATS_SET',
31-
'CHATS_UPSERT',
32-
'CHATS_UPDATE',
33-
'CHATS_DELETE',
34-
'GROUPS_UPSERT',
35-
'GROUP_UPDATE',
36-
'GROUP_PARTICIPANTS_UPDATE',
37-
'CONNECTION_UPDATE',
38-
'LABELS_EDIT',
39-
'LABELS_ASSOCIATION',
40-
'CALL',
41-
'TYPEBOT_START',
42-
'TYPEBOT_CHANGE_STATUS',
43-
'REMOVE_INSTANCE',
44-
'LOGOUT_INSTANCE',
45-
];
15+
private readonly logger = new Logger(WebsocketController.name);
4616

4717
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
48-
this.prisma = prismaRepository;
49-
this.monitor = waMonitor;
18+
super(prismaRepository, waMonitor);
5019
this.cors = configService.get<Cors>('CORS').ORIGIN;
5120
}
5221

@@ -72,22 +41,6 @@ export class WebsocketController {
7241
this.logger.info('Socket.io initialized');
7342
}
7443

75-
private set prisma(prisma: PrismaRepository) {
76-
this.prismaRepository = prisma;
77-
}
78-
79-
private get prisma() {
80-
return this.prismaRepository;
81-
}
82-
83-
private set monitor(waMonitor: WAMonitoringService) {
84-
this.waMonitor = waMonitor;
85-
}
86-
87-
private get monitor() {
88-
return this.waMonitor;
89-
}
90-
9144
private set cors(cors: Array<any>) {
9245
this.corsConfig = cors;
9346
}

src/api/routes/chatbot.router.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ChatwootRouter } from '@api/integrations/chatbot/chatwoot/routes/chatwoot.router';
2+
import { DifyRouter } from '@api/integrations/chatbot/dify/routes/dify.router';
3+
import { OpenaiRouter } from '@api/integrations/chatbot/openai/routes/openai.router';
4+
import { TypebotRouter } from '@api/integrations/chatbot/typebot/routes/typebot.router';
5+
import { Router } from 'express';
6+
7+
export class ChatbotRouter {
8+
public readonly router: Router;
9+
10+
constructor(...guards: any[]) {
11+
this.router = Router();
12+
13+
this.router.use('/chatwoot', new ChatwootRouter(...guards).router);
14+
this.router.use('/typebot', new TypebotRouter(...guards).router);
15+
this.router.use('/openai', new OpenaiRouter(...guards).router);
16+
this.router.use('/dify', new DifyRouter(...guards).router);
17+
}
18+
}

src/api/routes/event.router.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router';
2+
import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router';
3+
import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router';
4+
import { Router } from 'express';
5+
6+
import { WebhookRouter } from './webhook.router';
7+
8+
export class EventRouter {
9+
public readonly router: Router;
10+
11+
constructor(configService: any, ...guards: any[]) {
12+
this.router = Router();
13+
14+
this.router.use('/webhook', new WebhookRouter(configService, ...guards).router);
15+
this.router.use('/websocket', new WebsocketRouter(...guards).router);
16+
this.router.use('/rabbitmq', new RabbitmqRouter(...guards).router);
17+
this.router.use('/sqs', new SqsRouter(...guards).router);
18+
}
19+
}

src/api/routes/index.router.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { authGuard } from '@api/guards/auth.guard';
22
import { instanceExistsGuard, instanceLoggedGuard } from '@api/guards/instance.guard';
33
import Telemetry from '@api/guards/telemetry.guard';
4-
import { ChatwootRouter } from '@api/integrations/chatbot/chatwoot/routes/chatwoot.router';
5-
import { DifyRouter } from '@api/integrations/chatbot/dify/routes/dify.router';
6-
import { OpenaiRouter } from '@api/integrations/chatbot/openai/routes/openai.router';
7-
import { TypebotRouter } from '@api/integrations/chatbot/typebot/routes/typebot.router';
8-
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router';
9-
import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router';
10-
import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router';
11-
import { S3Router } from '@api/integrations/storage/s3/routes/s3.router';
124
import { webhookController } from '@api/server.module';
135
import { configService, WaBusiness } from '@config/env.config';
146
import { Router } from 'express';
@@ -17,15 +9,17 @@ import mime from 'mime';
179
import path from 'path';
1810

1911
import { ChatRouter } from './chat.router';
12+
import { ChatbotRouter } from './chatbot.router';
13+
import { EventRouter } from './event.router';
2014
import { GroupRouter } from './group.router';
2115
import { InstanceRouter } from './instance.router';
2216
import { LabelRouter } from './label.router';
2317
import { ProxyRouter } from './proxy.router';
2418
import { MessageRouter } from './sendMessage.router';
2519
import { SettingsRouter } from './settings.router';
20+
import { StorageRouter } from './storage.router';
2621
import { TemplateRouter } from './template.router';
2722
import { ViewsRouter } from './view.router';
28-
import { WebhookRouter } from './webhook.router';
2923

3024
enum HttpStatus {
3125
OK = 200,
@@ -87,19 +81,10 @@ router
8781
.use('/message', new MessageRouter(...guards).router)
8882
.use('/chat', new ChatRouter(...guards).router)
8983
.use('/group', new GroupRouter(...guards).router)
90-
.use('/webhook', new WebhookRouter(configService, ...guards).router)
9184
.use('/template', new TemplateRouter(configService, ...guards).router)
92-
.use('/chatwoot', new ChatwootRouter(...guards).router)
9385
.use('/settings', new SettingsRouter(...guards).router)
94-
.use('/websocket', new WebsocketRouter(...guards).router)
95-
.use('/rabbitmq', new RabbitmqRouter(...guards).router)
96-
.use('/sqs', new SqsRouter(...guards).router)
97-
.use('/typebot', new TypebotRouter(...guards).router)
9886
.use('/proxy', new ProxyRouter(...guards).router)
9987
.use('/label', new LabelRouter(...guards).router)
100-
.use('/s3', new S3Router(...guards).router)
101-
.use('/openai', new OpenaiRouter(...guards).router)
102-
.use('/dify', new DifyRouter(...guards).router)
10388
.get('/webhook/meta', async (req, res) => {
10489
if (req.query['hub.verify_token'] === configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
10590
res.send(req.query['hub.challenge']);
@@ -110,6 +95,9 @@ router
11095
const response = await webhookController.receiveWebhook(body);
11196

11297
return res.status(200).json(response);
113-
});
98+
})
99+
.use('', new EventRouter(configService, ...guards).router)
100+
.use('', new ChatbotRouter(...guards).router)
101+
.use('', new StorageRouter(...guards).router);
114102

115103
export { HttpStatus, router };

src/api/routes/storage.router.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { S3Router } from '@api/integrations/storage/s3/routes/s3.router';
2+
import { Router } from 'express';
3+
4+
export class StorageRouter {
5+
public readonly router: Router;
6+
7+
constructor(...guards: any[]) {
8+
this.router = Router();
9+
10+
this.router.use('/s3', new S3Router(...guards).router);
11+
}
12+
}

src/api/server.module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { eventEmitter } from '@config/event.config';
44
import { Logger } from '@config/logger.config';
55

66
import { ChatController } from './controllers/chat.controller';
7+
import { EventController } from './controllers/event.controller';
78
import { GroupController } from './controllers/group.controller';
89
import { InstanceController } from './controllers/instance.controller';
910
import { LabelController } from './controllers/label.controller';
@@ -18,15 +19,15 @@ import { DifyController } from './integrations/chatbot/dify/controllers/dify.con
1819
import { DifyService } from './integrations/chatbot/dify/services/dify.service';
1920
import { OpenaiController } from './integrations/chatbot/openai/controllers/openai.controller';
2021
import { OpenaiService } from './integrations/chatbot/openai/services/openai.service';
22+
import { TypebotController } from './integrations/chatbot/typebot/controllers/typebot.controller';
23+
import { TypebotService } from './integrations/chatbot/typebot/services/typebot.service';
2124
import { RabbitmqController } from './integrations/event/rabbitmq/controllers/rabbitmq.controller';
2225
import { RabbitmqService } from './integrations/event/rabbitmq/services/rabbitmq.service';
23-
import { S3Controller } from './integrations/storage/s3/controllers/s3.controller';
24-
import { S3Service } from './integrations/storage/s3/services/s3.service';
2526
import { SqsController } from './integrations/event/sqs/controllers/sqs.controller';
2627
import { SqsService } from './integrations/event/sqs/services/sqs.service';
27-
import { TypebotController } from './integrations/chatbot/typebot/controllers/typebot.controller';
28-
import { TypebotService } from './integrations/chatbot/typebot/services/typebot.service';
2928
import { WebsocketController } from './integrations/event/websocket/controllers/websocket.controller';
29+
import { S3Controller } from './integrations/storage/s3/controllers/s3.controller';
30+
import { S3Service } from './integrations/storage/s3/services/s3.service';
3031
import { ProviderFiles } from './provider/sessions';
3132
import { PrismaRepository } from './repository/repository.service';
3233
import { AuthService } from './services/auth.service';
@@ -84,6 +85,8 @@ export const webhookController = new WebhookController(webhookService, waMonitor
8485
const templateService = new TemplateService(waMonitor, prismaRepository, configService);
8586
export const templateController = new TemplateController(templateService);
8687

88+
export const eventController = new EventController(prismaRepository, waMonitor);
89+
8790
export const websocketController = new WebsocketController(prismaRepository, waMonitor);
8891

8992
const proxyService = new ProxyService(waMonitor);

src/api/services/channel.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto
66
import { ChatwootService } from '@api/integrations/chatbot/chatwoot/services/chatwoot.service';
77
import { DifyService } from '@api/integrations/chatbot/dify/services/dify.service';
88
import { OpenaiService } from '@api/integrations/chatbot/openai/services/openai.service';
9+
import { TypebotService } from '@api/integrations/chatbot/typebot/services/typebot.service';
910
import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
1011
import { getAMQP, removeQueues } from '@api/integrations/event/rabbitmq/libs/amqp.server';
1112
import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto';
1213
import { getSQS, removeQueues as removeQueuesSQS } from '@api/integrations/event/sqs/libs/sqs.server';
13-
import { TypebotService } from '@api/integrations/chatbot/typebot/services/typebot.service';
1414
import { PrismaRepository, Query } from '@api/repository/repository.service';
15-
import { waMonitor, websocketController } from '@api/server.module';
15+
import { eventController, waMonitor } from '@api/server.module';
1616
import { Events, wa } from '@api/types/wa.types';
1717
import { Auth, Chatwoot, ConfigService, HttpServer, Log, Rabbitmq, Sqs, Webhook } from '@config/env.config';
1818
import { Logger } from '@config/logger.config';
@@ -821,7 +821,7 @@ export class ChannelStartupService {
821821
}
822822
}
823823

824-
await websocketController.emit({
824+
await eventController.emit({
825825
instanceName: this.instance.name,
826826
origin: ChannelStartupService.name,
827827
event,

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { initSQS } from '@api/integrations/event/sqs/libs/sqs.server';
33
import { ProviderFiles } from '@api/provider/sessions';
44
import { PrismaRepository } from '@api/repository/repository.service';
55
import { HttpStatus, router } from '@api/routes/index.router';
6-
import { waMonitor, websocketController } from '@api/server.module';
6+
import { eventController, waMonitor } from '@api/server.module';
77
import { Auth, configService, Cors, HttpServer, ProviderSession, Rabbitmq, Sqs, Webhook } from '@config/env.config';
88
import { onUnexpectedError } from '@config/error.config';
99
import { Logger } from '@config/logger.config';
@@ -141,7 +141,7 @@ async function bootstrap() {
141141
ServerUP.app = app;
142142
const server = ServerUP[httpServer.TYPE];
143143

144-
websocketController.init(server);
144+
eventController.init(server);
145145

146146
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
147147

0 commit comments

Comments
 (0)