-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaccountRouter.js
More file actions
533 lines (502 loc) · 24.9 KB
/
accountRouter.js
File metadata and controls
533 lines (502 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
const express = require('express');
const multer = require('multer');
const { connectToDB } = require('./db');
const MediaHandler = require('./mediaHandler');
const audioConverter = require('./audioConverter');
const { appLogger } = require('./logger');
const { isValidJID, isValidMessageLength, isValidJIDArray } = require('./validators');
// Configure multer for memory storage
const upload = multer({
storage: multer.memoryStorage(),
limits: { fileSize: 50 * 1024 * 1024 },
});
/**
* Create an Express router exposing REST endpoints to manage and interact with a single account's messaging socket and stored messages.
* @param {string} accountId - Account identifier used to scope socket operations, logging, and database queries.
* @param {Object} socketManager - Manager providing socket lifecycle and access methods (e.g., `getSocket`, `createSocket`, `removeSocket`).
* @returns {import('express').Router} An Express router with routes for sending text and media messages, replying, reacting, deleting, retrieving messages/media/chats, and controlling the account connection.
*/
function createAccountRouter(accountId, socketManager) {
const router = express.Router();
let db = null;
let mediaHandler = null;
const log = appLogger(`account:${accountId}`);
(async () => {
try {
db = await connectToDB();
mediaHandler = new MediaHandler(db);
log.info('media_handler_initialized');
} catch (e) {
log.error('media_handler_init_failed', { error: e.message });
}
})();
/**
* Load a quoted message by ID, preferring the live socket store and falling back to the database.
*
* If the message is not available in the live socket store, attempts a database lookup and may
* initialize the module database connection to retrieve and normalize a stored message shape.
* @param {Object} socketInfo - Object containing the `socket` instance used for the live-store lookup.
* @param {string} quotedMessageId - The message ID of the quoted message to load.
* @param {string} chatId - The chat JID associated with the quoted message (used for live lookup and DB query).
* @returns {Object|null} The quoted message formatted with `key`, `message`, and `messageTimestamp` when found; `null` otherwise.
*/
async function loadQuotedMessage(socketInfo, quotedMessageId, chatId) {
try {
let quotedMsg = null;
try {
quotedMsg = await socketInfo.socket.loadMessage(chatId, quotedMessageId);
} catch (socketErr) {
log.warn('quote_load_socket_failed', { error: socketErr.message });
}
if (quotedMsg) {
log.info('quote_loaded_socket_store', { quotedMessageId });
return quotedMsg;
}
log.info('quote_socket_store_miss', { quotedMessageId });
if (!db) {
log.warn('quote_db_uninitialized');
db = await connectToDB();
}
const messagesCollection = db.collection('messages');
const dbMsg = await messagesCollection.findOne({ messageId: quotedMessageId, accountId });
if (!dbMsg) {
log.warn('quote_not_found_db', { quotedMessageId });
return null;
}
if (dbMsg.rawMessage) {
const participant = dbMsg.rawMessage.key.participant && dbMsg.rawMessage.key.participant.trim()
? dbMsg.rawMessage.key.participant
: undefined;
const sanitizedQuote = {
key: {
remoteJid: dbMsg.rawMessage.key.remoteJid,
id: dbMsg.rawMessage.key.id,
fromMe: dbMsg.rawMessage.key.fromMe,
participant,
},
message: dbMsg.rawMessage.message,
messageTimestamp: dbMsg.rawMessage.messageTimestamp,
};
log.info('quote_using_sanitized_rawMessage', { quotedMessageId });
return sanitizedQuote;
}
log.info('quote_constructing_from_db_fields', { quotedMessageId });
return {
key: {
remoteJid: dbMsg.chatId,
id: dbMsg.messageId,
fromMe: dbMsg.fromMe,
participant: dbMsg.fromMe ? undefined : dbMsg.from,
},
message: { conversation: dbMsg.text || '' },
messageTimestamp: dbMsg.timestamp,
};
} catch (err) {
log.error('quote_load_error', { quotedMessageId, error: err.message });
return null;
}
}
/**
* Persist a sent message record for the current account in the messages collection.
*
* @param {object} sentMsg - Raw message object returned by the socket; must include `key.id`, `key.remoteJid`, and optionally `messageTimestamp`.
* @param {'text'|'image'|'video'|'audio'|'document'} messageType - High-level message type to record.
* @param {object} [additionalData] - Optional metadata for the stored message.
* @param {string} [additionalData.text] - Text content for text messages.
* @param {string} [additionalData.caption] - Caption for media messages.
* @param {object} [additionalData.quotedMessage] - Quoted message object to attach to the stored record.
* @param {Array<string>} [additionalData.mentions] - Array of mentioned JIDs.
* @returns {boolean} `true` if the message document was inserted successfully, `false` otherwise.
*/
async function storeSentMessage(sentMsg, messageType, additionalData = {}) {
try {
if (!db) db = await connectToDB();
const messagesCollection = db.collection('messages');
const messageId = sentMsg.key.id;
const chatId = sentMsg.key.remoteJid;
const timestamp = sentMsg.messageTimestamp?.low || Math.floor(Date.now() / 1000);
let textContent = null;
let hasMedia = false;
let mediaUrl = null;
let mediaType = null;
let caption = null;
if (messageType === 'text') {
textContent = additionalData.text || null;
} else if (['image', 'video', 'audio', 'document'].includes(messageType)) {
hasMedia = true;
mediaType = messageType;
caption = additionalData.caption || null;
}
const messageDoc = {
accountId,
messageId,
chatId,
from: chatId,
fromMe: true,
timestamp,
type: messageType,
text: textContent,
caption,
hasMedia,
mediaUrl,
mediaType,
quotedMessage: additionalData.quotedMessage || null,
mentions: additionalData.mentions || [],
rawMessage: sentMsg,
createdAt: new Date(),
updatedAt: new Date(),
};
await messagesCollection.insertOne(messageDoc);
log.info('sent_message_stored', { messageType, messageId });
return true;
} catch (err) {
log.error('sent_message_store_failed', { error: err.message });
return false;
}
}
// Reply to message
router.post('/message/reply', async (req, res) => {
const { to, message, quotedMessageId, mentions } = req.body || {};
if (!to || !message || !quotedMessageId) {
return res.status(400).json({ ok: false, error: 'to, message, and quotedMessageId are required' });
}
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!isValidMessageLength(message)) {
return res.status(400).json({ ok: false, error: 'Message length invalid. Must be between 1 and 65536 characters' });
}
if (mentions && !isValidJIDArray(mentions)) {
return res.status(400).json({ ok: false, error: 'Invalid JID format in mentions array' });
}
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo) return res.status(404).json({ ok: false, error: 'Account not found' });
if (socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo.status });
}
try {
const quotedMsg = await loadQuotedMessage(socketInfo, quotedMessageId, to);
if (!quotedMsg) return res.status(404).json({ ok: false, error: 'Quoted message not found' });
const messageContent = { text: message };
if (mentions && Array.isArray(mentions) && mentions.length > 0) messageContent.mentions = mentions;
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent, { quoted: quotedMsg });
await storeSentMessage(sentMsg, 'text', { text: message, mentions, quotedMessage: quotedMessageId });
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('reply_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Send text
router.post('/message/send', async (req, res) => {
const { to, message, mentions } = req.body || {};
if (!to || !message) return res.status(400).json({ ok: false, error: 'to and message are required' });
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!isValidMessageLength(message)) {
return res.status(400).json({ ok: false, error: 'Message length invalid. Must be between 1 and 65536 characters' });
}
if (mentions && !isValidJIDArray(mentions)) {
return res.status(400).json({ ok: false, error: 'Invalid JID format in mentions array' });
}
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo) return res.status(404).json({ ok: false, error: 'Account not found' });
if (socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo.status });
}
try {
const messageContent = { text: message };
if (mentions && Array.isArray(mentions) && mentions.length > 0) messageContent.mentions = mentions;
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent);
await storeSentMessage(sentMsg, 'text', { text: message, mentions });
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('text_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Image
router.post('/message/send/image', upload.single('image'), async (req, res) => {
const { to, caption, quotedMessageId } = req.body || {};
if (!to) return res.status(400).json({ ok: false, error: 'to field is required' });
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!req.file) return res.status(400).json({ ok: false, error: 'image file is required' });
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
const messageContent = { image: req.file.buffer, caption: caption || undefined, mimetype: req.file.mimetype };
const sendOptions = {};
if (quotedMessageId) {
try {
const quotedMsg = await loadQuotedMessage(socketInfo, quotedMessageId, to);
if (quotedMsg) sendOptions.quoted = quotedMsg;
} catch (quoteErr) {
log.error('image_quote_load_error', { error: quoteErr.message });
}
}
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent, sendOptions);
await storeSentMessage(sentMsg, 'image', { caption });
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('image_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Video
router.post('/message/send/video', upload.single('video'), async (req, res) => {
const { to, caption, gifPlayback, quotedMessageId } = req.body || {};
if (!to) return res.status(400).json({ ok: false, error: 'to field is required' });
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!req.file) return res.status(400).json({ ok: false, error: 'video file is required' });
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
const messageContent = {
video: req.file.buffer,
caption: caption || undefined,
mimetype: req.file.mimetype,
gifPlayback: gifPlayback === 'true' || gifPlayback === true,
};
const sendOptions = {};
if (quotedMessageId) {
try {
const quotedMsg = await loadQuotedMessage(socketInfo, quotedMessageId, to);
if (quotedMsg) sendOptions.quoted = quotedMsg;
} catch (quoteErr) {
log.error('video_quote_load_error', { error: quoteErr.message });
}
}
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent, sendOptions);
await storeSentMessage(sentMsg, 'video', { caption });
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('video_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Audio
router.post('/message/send/audio', upload.single('audio'), async (req, res) => {
const { to, ptt, quotedMessageId } = req.body || {};
if (!to) return res.status(400).json({ ok: false, error: 'to field is required' });
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!req.file) return res.status(400).json({ ok: false, error: 'audio file is required' });
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
let audioBuffer = req.file.buffer;
let mimetype = 'audio/ogg; codecs=opus';
const isPtt = ptt === 'true' || ptt === true;
if (audioConverter.needsConversion(req.file.mimetype)) {
try {
audioBuffer = await audioConverter.convertToOggOpus(req.file.buffer, req.file.mimetype, isPtt);
} catch (conversionErr) {
log.error('audio_conversion_failed', { error: conversionErr.message });
audioBuffer = req.file.buffer;
mimetype = req.file.mimetype;
}
}
const messageContent = { audio: audioBuffer, mimetype, ptt: isPtt };
const sendOptions = {};
if (quotedMessageId) {
try {
const quotedMsg = await loadQuotedMessage(socketInfo, quotedMessageId, to);
if (quotedMsg) sendOptions.quoted = quotedMsg;
} catch (quoteErr) {
log.error('audio_quote_load_error', { error: quoteErr.message });
}
}
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent, sendOptions);
await storeSentMessage(sentMsg, 'audio', {});
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('audio_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Document
router.post('/message/send/document', upload.single('document'), async (req, res) => {
const { to, caption, fileName, quotedMessageId } = req.body || {};
if (!to) return res.status(400).json({ ok: false, error: 'to field is required' });
if (!isValidJID(to)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "to" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
if (!req.file) return res.status(400).json({ ok: false, error: 'document file is required' });
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
const messageContent = { document: req.file.buffer, caption: caption || undefined, mimetype: req.file.mimetype, fileName: fileName || req.file.originalname };
const sendOptions = {};
if (quotedMessageId) {
try {
const quotedMsg = await loadQuotedMessage(socketInfo, quotedMessageId, to);
if (quotedMsg) sendOptions.quoted = quotedMsg;
} catch (quoteErr) {
log.error('document_quote_load_error', { error: quoteErr.message });
}
}
const sentMsg = await socketInfo.socket.sendMessage(to, messageContent, sendOptions);
await storeSentMessage(sentMsg, 'document', { caption });
return res.status(200).json({ ok: true, messageId: sentMsg.key.id, timestamp: sentMsg.messageTimestamp });
} catch (err) {
log.error('document_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Reaction
router.post('/message/react', async (req, res) => {
const { chatId, messageId, emoji } = req.body || {};
if (!chatId || !messageId) return res.status(400).json({ ok: false, error: 'chatId and messageId are required' });
if (!isValidJID(chatId)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "chatId" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
const messagesCollection = db.collection('messages');
const originalMsg = await messagesCollection.findOne({ messageId, chatId, accountId });
if (!originalMsg) return res.status(404).json({ ok: false, error: 'Message not found in database' });
const reactionMessage = { react: { text: emoji || '', key: { remoteJid: chatId, id: messageId, fromMe: originalMsg.fromMe } } };
const sentMsg = await socketInfo.socket.sendMessage(chatId, reactionMessage);
await storeSentMessage(sentMsg, 'text', {});
return res.status(200).json({ ok: true, messageId: sentMsg.key.id });
} catch (err) {
log.error('reaction_send_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Delete message
router.post('/message/delete', async (req, res) => {
const { chatId, messageId } = req.body || {};
if (!chatId || !messageId) return res.status(400).json({ ok: false, error: 'chatId and messageId are required' });
if (!isValidJID(chatId)) {
return res.status(400).json({ ok: false, error: 'Invalid WhatsApp JID format for "chatId" field. Expected format: 1234567890@s.whatsapp.net or 123456789@g.us' });
}
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo || socketInfo.status !== 'connected') {
return res.status(400).json({ ok: false, error: 'Account not connected', status: socketInfo?.status });
}
try {
await socketInfo.socket.sendMessage(chatId, { delete: { remoteJid: chatId, id: messageId, fromMe: true } });
return res.status(200).json({ ok: true });
} catch (err) {
log.error('message_delete_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Get media
router.get('/messages/:messageId/media', async (req, res) => {
try {
const { messageId } = req.params;
const messagesCollection = db.collection('messages');
const message = await messagesCollection.findOne({ messageId, accountId });
if (!message) return res.status(404).json({ ok: false, error: 'Message not found' });
if (!message.hasMedia || !message.mediaGridFsId) return res.status(404).json({ ok: false, error: 'Message has no media' });
const { stream, metadata } = await mediaHandler.getMedia(message.mediaGridFsId);
res.setHeader('Content-Type', metadata.contentType || 'application/octet-stream');
res.setHeader('Content-Length', metadata.length);
if (metadata.fileName) res.setHeader('Content-Disposition', `inline; filename="${metadata.fileName}"`);
stream.pipe(res);
} catch (err) {
log.error('media_retrieval_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Get single message
router.get('/messages/:messageId', async (req, res) => {
try {
const { messageId } = req.params;
const messagesCollection = db.collection('messages');
const message = await messagesCollection.findOne({ messageId, accountId });
if (!message) return res.status(404).json({ ok: false, error: 'Message not found' });
delete message._id;
return res.status(200).json({ ok: true, message });
} catch (err) {
log.error('message_fetch_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Get chat messages
router.get('/chats/:chatId/messages', async (req, res) => {
try {
const { chatId } = req.params;
const { limit = 50, offset = 0, before, after } = req.query;
const messagesCollection = db.collection('messages');
const query = { accountId, chatId };
if (before) query.timestamp = { $lt: parseInt(before) };
if (after) query.timestamp = { ...query.timestamp, $gt: parseInt(after) };
const messages = await messagesCollection.find(query).sort({ timestamp: -1 }).skip(parseInt(offset)).limit(parseInt(limit)).toArray();
const total = await messagesCollection.countDocuments(query);
messages.forEach(m => delete m._id);
return res.status(200).json({ ok: true, messages, pagination: { total, limit: parseInt(limit), offset: parseInt(offset), hasMore: total > (parseInt(offset) + messages.length) } });
} catch (err) {
log.error('chat_messages_fetch_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// List chats
router.get('/chats', async (req, res) => {
try {
const messagesCollection = db.collection('messages');
const chats = await messagesCollection.aggregate([
{ $match: { accountId } },
{ $sort: { timestamp: -1 } },
{ $group: { _id: '$chatId', lastMessage: { $first: '$$ROOT' }, messageCount: { $sum: 1 }, lastTimestamp: { $first: '$timestamp' } } },
{ $sort: { lastTimestamp: -1 } },
]).toArray();
const formattedChats = chats.map(chat => ({
chatId: chat._id,
messageCount: chat.messageCount,
lastMessage: { messageId: chat.lastMessage.messageId, text: chat.lastMessage.text, type: chat.lastMessage.type, timestamp: chat.lastMessage.timestamp, fromMe: chat.lastMessage.fromMe },
}));
return res.status(200).json({ ok: true, chats: formattedChats });
} catch (err) {
log.error('chats_list_error', { error: err.message });
return res.status(500).json({ ok: false, error: err.message || 'internal_error' });
}
});
// Account status
router.get('/status', (req, res) => {
const socketInfo = socketManager.getSocket(accountId);
if (!socketInfo) return res.status(404).json({ ok: false, error: 'Account not found or not started' });
return res.status(200).json({ ok: true, accountId, status: socketInfo.status, qr: socketInfo.qr, collection: socketInfo.collection });
});
// Connect
router.post('/connect', async (req, res) => {
try {
const socketInfo = await socketManager.createSocket(accountId);
return res.status(200).json({ ok: true, accountId, status: socketInfo.status, message: 'Connection initiated. Check /accounts/' + accountId + '/status for QR code' });
} catch (err) {
log.error('connect_error', { error: err.message });
return res.status(500).json({ ok: false, error: 'Failed to connect' });
}
});
// Disconnect
router.post('/disconnect', async (req, res) => {
try {
await socketManager.removeSocket(accountId);
return res.status(200).json({ ok: true, message: 'Account disconnected' });
} catch (err) {
log.error('disconnect_error', { error: err.message });
return res.status(500).json({ ok: false, error: 'Failed to disconnect' });
}
});
return router;
}
module.exports = { createAccountRouter };