|
|
|
@ -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
|
|
|
|
|
};
|
|
|
|
|