'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 { upsert: upsertAgentSession } = require('../../services/agent_sessions.service'); const { isEmpty } = require('../../utils/commons.util'); const { getUserLogger } = require('../../utils/logger.util'); const { domain } = require('../../config').server; const waEmitter = require('../../core/emitter'); 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 { getUserLogger(phone).info(`WhatsApp ${phone} 正在连接`); 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); }; const offline = async ctx => { const { phone } = ctx.query; const existsSession = sessionStore.getSession(phone); ctx.assert(existsSession, 400, `WhatsApp ${phone} 已离线`); try { getUserLogger(phone).info(`WhatsApp ${phone} 准备离线`); // existsSession.stop(); // todo: // sessionStore.removeSession(existsSession.channelId); waEmitter.emit('connection:close', { phone, whatsAppNo: phone, status: 'offline', channelId: existsSession.channelId }); return ''; // { wsToSend, ret: 'Message sent successfully' }; } catch (error) { console.error('set offline error', error); ctx.assert(null, 500, 'Failed to set offline'); } }; const hosted = async ctx => { const { phone, contact, hosted } = ctx.query; ctx.assert(phone, 400, 'phone is required'); const existsSession = sessionStore.getSession(phone); ctx.assert(existsSession, 400, `WhatsApp ${phone} 已离线`); try { // hosting an USER if (isEmpty(contact)) { waEmitter.emit('connection:update', { phone, whatsAppNo: phone, status: hosted ? 'hosted' : 'open', channelId: existsSession.channelId }); return ''; } // hosting a session await upsertAgentSession({ hosted: phone, contact, }); return ''; } catch (error) { console.error('set hosted error', error); ctx.assert(null, 500, 'Failed to set hosted'); } }; /** * @deprecated */ const setStatus = async ctx => { const { phone, status } = ctx.request.body; const existsSession = sessionStore.getSession(phone); ctx.assert(existsSession, 400, `WhatsApp ${phone} 已离线`); try { // existsSession.stop(); // todo: waEmitter.emit('connection:close', { phone, status: 'offline', channelId: existsSession.channelId }); } catch (error) { console.error('close connection error', error); ctx.assert(null, 500, 'Failed to close connection'); } }; const test = async () => {}; module.exports = { newConnect, getAll, getSessions, offline, hosted, test, };