连接事件

dev/supplier-email-drawer
Lei OT 9 months ago
parent 3fb6f10cd9
commit 3df7ff4b80

@ -1,7 +1,8 @@
const { sessionService } = require('../../core'); // Import from core/index.js
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
const { getConnection } = require('../../services/connections.service');
const { objectMapper } = require('../../utils/commons.util');
const { objectMapper, isEmpty } = require('../../utils/commons.util');
const { domain } = require('../../config').server;
const waInstance = {
wa: null,
@ -9,9 +10,14 @@ const waInstance = {
exports.newConnect = async ctx => {
try {
const { phone } = ctx.query;
const findSession = await getConnection({ sesson_id: phone, status: 'open' });
if (findSession) {
return findSession;
const findSession = await getConnection({ sesson_id: phone, connect_domain: domain });
// todo: 只有一条
if (!isEmpty(findSession)) {
const { sesson_id: sessonId, status } = findSession[0];
if (['open', 'connecting'].includes(status)) {
const sock = sessionService.getSession(sessonId);
return sock;
}
}
const whatsApp1 = await createWhatsApp(phone);
const qr = await whatsApp1.start();
@ -25,40 +31,7 @@ exports.newConnect = async ctx => {
}
};
exports.testSend = async ctx => {
const { to, content } = ctx.request.body;
waInstance.wa.sendTextMessage(to, content);
return { waInstance, ret: 'Message sent successfully' };
};
exports.getAll = async () => {
const sessions = sessionService.sessions;
return Array.from(sessions);
};
/**
* @deprecated
*/
exports.getIn = async () => {
// Wait for at least one connection to be established (or handle the case where no connections are available)
// await new Promise(resolve => {
// const checkConnections = () => {
// if (websocketManager.getConnections().length > 0) {
// resolve();
// } else {
// setTimeout(checkConnections, 100); // Check again after 100ms
// }
// };
// checkConnections();
// });
// const availableWs = websocketManager.getAvailableConnection();
// if (!availableWs) {
// // ctx.status = 503;
// // ctx.body = { message: 'No available connections' };
// ctx.assert(availableWs, 503, 'No available connections');
// return;
// }
// // return availableWs;
// const { sessionId, url } = websocketService.createSession(availableWs);
// return { sessionId, url, message: 'Connection established' }; // availableWs
};

@ -6,7 +6,7 @@ const { objectMapper } = require('../../utils/commons.util');
const logger = console;
const connectionEventNames = ['connection:added', 'connection:updated', 'connection:removed'];
const connectionEventNames = ['connection:open', 'connection:update', 'connection:close'];
const messageEventNames = ['message:received', 'message:updated'];
const eeventTypeMapped = {
@ -28,8 +28,8 @@ const webhookBodyBuilder = (messageData, messageType) => {
const setupConnectionHandler = () => {
// connectionEventNames.forEach(eventName => {
whatsappEvents.on('connection:added', async connectionData => {
logger.info(`Setting up event ${'connection:added'}`);
whatsappEvents.on('connection:open', async connectionData => {
logger.info(`Setting up event ${'connection:open'}`);
try {
await addConnection({
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id', createTimestamp: 'createtime' }),
@ -40,7 +40,7 @@ const setupConnectionHandler = () => {
logger.error({ connectionData, error }, 'error add connection');
}
});
whatsappEvents.on('connection:updated', async connectionData => {
whatsappEvents.on('connection:update', async connectionData => {
logger.info(`Setting up event ${'connection:updated'}`);
try {
await updateConnection({

@ -8,7 +8,7 @@ module.exports = async (ctx, next) => {
console.log('forward start ---------------');
// console.log(/^\/wai-server\/v\d{1}\/(?!channels|messages)/.test(ctx.path));
const { waisession } = ctx.headers;
const findSession = await getConnection({ sesson_id: waisession, status: 'open' });
const findSession = await getConnection({ sesson_id: waisession, connect_domain: domain });
if (!isEmpty(findSession) && findSession.connect_domain === domain) {
await next();
}

@ -27,7 +27,7 @@ const updateConnection = async data => {
};
const getConnection = async data => {
const r = await ConnectionsModel.findOne({ where: data });
const r = await ConnectionsModel.findAll({ where: data });
return r;
};

Loading…
Cancel
Save