wexa-chat 0.2.6 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -70,6 +70,8 @@ function createConversationModel(options, conn) {
70
70
  },
71
71
  { timestamps: true }
72
72
  );
73
+ ConversationSchema.index({ linkedinChatId: 1 }, { unique: false, sparse: true });
74
+ ConversationSchema.index({ whatsappChatId: 1 }, { unique: false, sparse: true });
73
75
  const model = conn.model(name, ConversationSchema);
74
76
  modelRegistry.set(registryKey, model);
75
77
  return model;
@@ -547,6 +549,8 @@ function createMessagesService(models, hooks = {}) {
547
549
  const tasks = [];
548
550
  let lastLinkedInId;
549
551
  let lastWhatsAppId;
552
+ let linkedinChatId;
553
+ let whatsappChatId;
550
554
  if (Array.isArray(source) && source.includes("linkedin")) {
551
555
  const LinkedinConnector = connectorIds && connectorIds.linkedin;
552
556
  lastLinkedInId = LinkedinConnector == null ? void 0 : LinkedinConnector.connectorId;
@@ -555,10 +559,18 @@ function createMessagesService(models, hooks = {}) {
555
559
  name: "linkedin",
556
560
  run: async () => {
557
561
  const path = paths.linkedin.startChat(String(LinkedinConnector.connectorId));
558
- await getAxios().post(path, {
559
- linkedin_url: LinkedinConnector.contactId,
560
- text
561
- });
562
+ try {
563
+ const res = await getAxios().post(path, {
564
+ linkedin_url: LinkedinConnector.contactId,
565
+ text
566
+ });
567
+ const data = res.data;
568
+ console.log("linkedinChatId", data);
569
+ linkedinChatId = data.chat_data.chat_id;
570
+ return { status: "fulfilled", value: data };
571
+ } catch (error) {
572
+ return { status: "rejected", reason: error };
573
+ }
562
574
  }
563
575
  });
564
576
  } else {
@@ -579,7 +591,14 @@ function createMessagesService(models, hooks = {}) {
579
591
  phone_numbers: sanitize(raw),
580
592
  text
581
593
  };
582
- await getAxios().post(path, body);
594
+ try {
595
+ const res = await getAxios().post(path, body);
596
+ const data = res.data;
597
+ whatsappChatId = data.chat_id;
598
+ return { status: "fulfilled", value: data };
599
+ } catch (error) {
600
+ return { status: "rejected", reason: error };
601
+ }
583
602
  }
584
603
  });
585
604
  } else {
@@ -588,22 +607,21 @@ function createMessagesService(models, hooks = {}) {
588
607
  }
589
608
  if (tasks.length > 0) {
590
609
  const results = await Promise.allSettled(tasks.map((t) => t.run()));
591
- results.forEach((res, idx) => {
610
+ results.forEach((result, idx) => {
611
+ var _a, _b, _c, _d, _e, _f;
592
612
  const name = tasks[idx].name;
593
- if (res.status === "fulfilled") {
613
+ if (result.status === "fulfilled") {
594
614
  success_source.push(name);
595
615
  } else {
596
- const err = success_source;
597
- res.reason.response.data.detail || res.reason.response.data || "Unknown error";
616
+ const error = ((_c = (_b = (_a = result.reason) == null ? void 0 : _a.response) == null ? void 0 : _b.data) == null ? void 0 : _c.detail) || ((_e = (_d = result.reason) == null ? void 0 : _d.response) == null ? void 0 : _e.data) || ((_f = result.reason) == null ? void 0 : _f.message) || "Unknown error";
598
617
  failed_source.push({
599
618
  source: name,
600
- message: typeof err === "string" ? err : JSON.stringify(err)
619
+ message: typeof error === "string" ? error : JSON.stringify(error)
601
620
  });
621
+ console.error(`${name} failed:`, error);
602
622
  }
603
623
  });
604
- console.log("results", results);
605
624
  }
606
- console.log("success_source", success_source);
607
625
  const message = new Message({
608
626
  organizationId,
609
627
  conversationId,
@@ -624,6 +642,8 @@ function createMessagesService(models, hooks = {}) {
624
642
  };
625
643
  if (lastLinkedInId) conversationUpdate.lastLinkedInId = lastLinkedInId;
626
644
  if (lastWhatsAppId) conversationUpdate.lastWhatsAppId = lastWhatsAppId;
645
+ if (linkedinChatId) conversationUpdate.linkedinChatId = linkedinChatId;
646
+ if (whatsappChatId) conversationUpdate.whatsappChatId = whatsappChatId;
627
647
  await Conversation.findByIdAndUpdate(conversationId, {
628
648
  $set: conversationUpdate
629
649
  });
@@ -633,6 +653,86 @@ function createMessagesService(models, hooks = {}) {
633
653
  message.failed_source = failed_source;
634
654
  return message;
635
655
  },
656
+ /**
657
+ * Receive a new message by whatsappChatId
658
+ */
659
+ async receiveWhatsappMessage({ whatsappChatId, message }) {
660
+ const conversation = await Conversation.findOne({ whatsappChatId }).lean();
661
+ if (!conversation) {
662
+ throw new Error("Conversation not found for given whatsappChatId");
663
+ }
664
+ const applicationParticipant = conversation.participants.find(
665
+ (p) => p.entityModel === "Application"
666
+ );
667
+ if (!applicationParticipant) {
668
+ throw new Error("Application participant not found in conversation");
669
+ }
670
+ const orgId = conversation.organizationId;
671
+ const senderModel = "Application";
672
+ const senderId = applicationParticipant.entityId;
673
+ const msgDoc = new Message({
674
+ organizationId: orgId,
675
+ conversationId: String(conversation._id),
676
+ senderModel,
677
+ senderId,
678
+ text: message,
679
+ kind: "text",
680
+ source: ["whatsapp"],
681
+ parentMessageId: void 0,
682
+ rootThreadId: void 0
683
+ });
684
+ await msgDoc.save();
685
+ await Conversation.findByIdAndUpdate(conversation._id, {
686
+ $set: {
687
+ lastMessageAt: msgDoc.createdAt,
688
+ lastMessagePreview: message.substring(0, 100),
689
+ lastMessageSenderId: senderId,
690
+ lastMessageSenderModel: senderModel
691
+ }
692
+ });
693
+ if (onMessageCreated) onMessageCreated(msgDoc);
694
+ return msgDoc;
695
+ },
696
+ /**
697
+ * Receive a new message by linkedinChatId
698
+ */
699
+ async receiveLinkedinMessage({ linkedinChatId, message }) {
700
+ const conversation = await Conversation.findOne({ linkedinChatId }).lean();
701
+ if (!conversation) {
702
+ throw new Error("Conversation not found for given linkedinChatId");
703
+ }
704
+ const applicationParticipant = conversation.participants.find(
705
+ (p) => p.entityModel === "Application"
706
+ );
707
+ if (!applicationParticipant) {
708
+ throw new Error("Application participant not found in conversation");
709
+ }
710
+ const orgId = conversation.organizationId;
711
+ const senderModel = "Application";
712
+ const senderId = applicationParticipant.entityId;
713
+ const msgDoc = new Message({
714
+ organizationId: orgId,
715
+ conversationId: String(conversation._id),
716
+ senderModel,
717
+ senderId,
718
+ text: message,
719
+ kind: "text",
720
+ source: ["linkedin"],
721
+ parentMessageId: void 0,
722
+ rootThreadId: void 0
723
+ });
724
+ await msgDoc.save();
725
+ await Conversation.findByIdAndUpdate(conversation._id, {
726
+ $set: {
727
+ lastMessageAt: msgDoc.createdAt,
728
+ lastMessagePreview: message.substring(0, 100),
729
+ lastMessageSenderId: senderId,
730
+ lastMessageSenderModel: senderModel
731
+ }
732
+ });
733
+ if (onMessageCreated) onMessageCreated(msgDoc);
734
+ return msgDoc;
735
+ },
636
736
  /**
637
737
  * List messages for a conversation with pagination
638
738
  * @param args Message listing arguments