You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/wai-server/api/channels/channel.controller.js

42 lines
1.3 KiB
JavaScript

'use strict';
const { sessionStore } = require('../../core'); // Import from core/index.js
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
const { getConnection } = require('../../services/connections.service');
const { isEmpty } = require('../../utils/commons.util');
const { domain } = require('../../config').server;
const newConnect = async ctx => {
const { phone } = ctx.query;
const existsSession = sessionStore.getSession(phone);
if (!isEmpty(existsSession)) {
ctx.assert(null, 400, `WhatsApp ${phone} 已连接`);
// return { phone, session_id: phone, ...existsSession, stauts: 'open', wai_server: domain };
}
try {
const whatsApp1 = await createWhatsApp(phone);
ctx.assert(whatsApp1, 503, 'No available connections');
whatsApp1.start();
sessionStore.createSession(phone, whatsApp1);
return { phone, session_id: phone, wai_server: domain, ...whatsApp1 };
} catch (error) {
console.error('create connection error', error);
ctx.assert(null, 500, 'Failed to create connection or generate QR code.');
}
};
const getAll = async () => {
const findConnection = await getConnection({});
return findConnection;
};
const getSessions = async () => {
return Array.from(sessionStore.sessions);
};
module.exports = {
newConnect,
getAll,
getSessions,
};