All files / services/whatsapp WebMessageSender.js

0% Statements 0/16
0% Branches 0/22
0% Functions 0/2
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                     
class WebMessageSender {
  constructor(whatsappService) {
    this.whatsappService = whatsappService;
  }
 
  async send({ tenantId, item, metadata }) {
    if (!this.whatsappService) {
      return { success: false, error: 'WhatsApp service not available' };
    }
    if (item.message_type === 'raw' && metadata.payload?.raw) {
      const instance = this.whatsappService.getInstance?.(tenantId);
      const sock = instance?.connection?.getSocket?.(tenantId);
      if (!sock) {
        return { success: false, error: 'WhatsApp not connected' };
      }
      const jid = metadata.payload?.jid || item.phone_number;
      await sock.sendMessage(jid, metadata.payload.raw);
      return { success: true, messageId: null };
    }
    const result = await this.whatsappService.sendMessage(
      tenantId,
      item.phone_number,
      item.content || '',
      metadata.conversationId || null,
      metadata.options || {}
    );
    if (!result?.success) {
      return { success: false, error: result?.error || 'Failed to send message' };
    }
    return { success: true, messageId: result.whatsappMessageId || null };
  }
}
 
module.exports = { WebMessageSender };