wexa-chat 0.2.9 → 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;
@@ -651,6 +653,86 @@ function createMessagesService(models, hooks = {}) {
651
653
  message.failed_source = failed_source;
652
654
  return message;
653
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
+ },
654
736
  /**
655
737
  * List messages for a conversation with pagination
656
738
  * @param args Message listing arguments