wexa-chat 0.3.3 → 0.3.4

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
@@ -820,121 +820,152 @@ function createMessagesService(models, hooks = {}) {
820
820
  return message;
821
821
  },
822
822
  /**
823
- * Receive a new message by whatsappChatId
823
+ * Receive a new message by whatsappChatId — delivers to ALL conversations
824
+ * that share this chatId (across organizations).
824
825
  */
825
826
  async receiveWhatsappMessage(args) {
826
- const { whatsappChatId, message, messageId, senderName } = args;
827
- const conversation = await Conversation.findOne({ whatsappChatId }).lean();
828
- if (!conversation) {
827
+ const { whatsappChatId, message, messageId, senderName, senderPhone } = args;
828
+ const conversations = await Conversation.find({ whatsappChatId }).lean();
829
+ if (!conversations.length) {
829
830
  throw new Error("Conversation not found for given whatsappChatId");
830
831
  }
831
- if (messageId) {
832
- const existingMessage = await Message.findOne({
833
- organizationId: conversation.organizationId,
834
- conversationId: String(conversation._id),
835
- externalMessageId: messageId
836
- }).lean();
837
- if (existingMessage) {
838
- console.log(`Duplicate WhatsApp message detected: ${messageId}`);
839
- return existingMessage;
832
+ let lastSavedMessage = null;
833
+ for (const conversation of conversations) {
834
+ if (messageId) {
835
+ const existingMessage = await Message.findOne({
836
+ organizationId: conversation.organizationId,
837
+ conversationId: String(conversation._id),
838
+ externalMessageId: messageId
839
+ }).lean();
840
+ if (existingMessage) {
841
+ console.log(`Duplicate WhatsApp message detected for conversation ${conversation._id}: ${messageId}`);
842
+ lastSavedMessage = existingMessage;
843
+ continue;
844
+ }
840
845
  }
841
- }
842
- const applicationParticipant = conversation.participants.find(
843
- (p) => p.entityModel === "Application"
844
- );
845
- if (!applicationParticipant) {
846
- throw new Error("Application participant not found in conversation");
847
- }
848
- const orgId = conversation.organizationId;
849
- const senderModel = "Application";
850
- const senderId = applicationParticipant.entityId;
851
- const msgDoc = new Message({
852
- organizationId: orgId,
853
- conversationId: String(conversation._id),
854
- senderModel,
855
- senderId,
856
- text: message,
857
- kind: "text",
858
- source: ["whatsapp"],
859
- externalMessageId: messageId,
860
- parentMessageId: void 0,
861
- rootThreadId: void 0,
862
- metadata: senderName ? {
863
- whatsapp: {
864
- senderName
846
+ const applicationParticipant = conversation.participants.find(
847
+ (p) => p.entityModel === "Application"
848
+ );
849
+ if (!applicationParticipant) {
850
+ console.log(`Application participant not found in conversation ${conversation._id}, skipping`);
851
+ continue;
852
+ }
853
+ const orgId = conversation.organizationId;
854
+ const senderModel = "Application";
855
+ const senderId = applicationParticipant.entityId;
856
+ const msgDoc = new Message({
857
+ organizationId: orgId,
858
+ conversationId: String(conversation._id),
859
+ senderModel,
860
+ senderId,
861
+ text: message,
862
+ kind: "text",
863
+ source: ["whatsapp"],
864
+ externalMessageId: messageId,
865
+ parentMessageId: void 0,
866
+ rootThreadId: void 0,
867
+ metadata: senderName || senderPhone ? {
868
+ whatsapp: {
869
+ senderName,
870
+ senderPhone
871
+ }
872
+ } : void 0
873
+ });
874
+ try {
875
+ await msgDoc.save();
876
+ } catch (saveError) {
877
+ if ((saveError == null ? void 0 : saveError.code) === 11e3) {
878
+ console.log(`Duplicate key for conversation ${conversation._id}, already saved by concurrent request`);
879
+ continue;
865
880
  }
866
- } : void 0
867
- });
868
- await msgDoc.save();
869
- await Conversation.findByIdAndUpdate(conversation._id, {
870
- $set: {
871
- lastMessageAt: msgDoc.createdAt,
872
- lastMessagePreview: message.substring(0, 100),
873
- lastMessageSenderId: senderId,
874
- lastMessageSenderModel: senderModel
881
+ throw saveError;
875
882
  }
876
- });
877
- if (onMessageCreated) onMessageCreated(msgDoc);
878
- return msgDoc;
883
+ await Conversation.findByIdAndUpdate(conversation._id, {
884
+ $set: {
885
+ lastMessageAt: msgDoc.createdAt,
886
+ lastMessagePreview: message.substring(0, 100),
887
+ lastMessageSenderId: senderId,
888
+ lastMessageSenderModel: senderModel
889
+ }
890
+ });
891
+ if (onMessageCreated) onMessageCreated(msgDoc);
892
+ lastSavedMessage = msgDoc;
893
+ }
894
+ return lastSavedMessage;
879
895
  },
880
896
  /**
881
- * Receive a new message by linkedinChatId
897
+ * Receive a new message by linkedinChatId — delivers to ALL conversations
898
+ * that share this chatId (across organizations).
882
899
  */
883
900
  async receiveLinkedinMessage(args) {
884
901
  const { linkedinChatId, message, messageId, senderName, senderProfileUrl } = args;
885
- const conversation = await Conversation.findOne({ linkedinChatId }).sort({ lastMessageAt: -1 }).lean();
886
- if (!conversation) {
902
+ const conversations = await Conversation.find({ linkedinChatId }).lean();
903
+ if (!conversations.length) {
887
904
  throw new Error("Conversation not found for given linkedinChatId");
888
905
  }
889
- if (messageId) {
890
- const existingMessage = await Message.findOne({
891
- organizationId: conversation.organizationId,
892
- conversationId: String(conversation._id),
893
- externalMessageId: messageId
894
- }).lean();
895
- if (existingMessage) {
896
- console.log(`Duplicate LinkedIn message detected: ${messageId}`);
897
- return existingMessage;
906
+ let lastSavedMessage = null;
907
+ for (const conversation of conversations) {
908
+ if (messageId) {
909
+ const existingMessage = await Message.findOne({
910
+ organizationId: conversation.organizationId,
911
+ conversationId: String(conversation._id),
912
+ externalMessageId: messageId
913
+ }).lean();
914
+ if (existingMessage) {
915
+ console.log(`Duplicate LinkedIn message detected for conversation ${conversation._id}: ${messageId}`);
916
+ lastSavedMessage = existingMessage;
917
+ continue;
918
+ }
898
919
  }
899
- }
900
- const applicationParticipant = conversation.participants.find(
901
- (p) => p.entityModel === "Application"
902
- );
903
- if (!applicationParticipant) {
904
- throw new Error("Application participant not found in conversation");
905
- }
906
- const orgId = conversation.organizationId;
907
- const senderModel = "Application";
908
- const senderId = applicationParticipant.entityId;
909
- const msgDoc = new Message({
910
- organizationId: orgId,
911
- conversationId: String(conversation._id),
912
- senderModel,
913
- senderId,
914
- text: message,
915
- kind: "text",
916
- source: ["linkedin"],
917
- externalMessageId: messageId,
918
- parentMessageId: void 0,
919
- rootThreadId: void 0,
920
- metadata: senderName || senderProfileUrl ? {
921
- linkedin: {
922
- senderName,
923
- senderProfileUrl
920
+ const applicationParticipant = conversation.participants.find(
921
+ (p) => p.entityModel === "Application"
922
+ );
923
+ if (!applicationParticipant) {
924
+ console.log(`Application participant not found in conversation ${conversation._id}, skipping`);
925
+ continue;
926
+ }
927
+ const orgId = conversation.organizationId;
928
+ const senderModel = "Application";
929
+ const senderId = applicationParticipant.entityId;
930
+ const msgDoc = new Message({
931
+ organizationId: orgId,
932
+ conversationId: String(conversation._id),
933
+ senderModel,
934
+ senderId,
935
+ text: message,
936
+ kind: "text",
937
+ source: ["linkedin"],
938
+ externalMessageId: messageId,
939
+ parentMessageId: void 0,
940
+ rootThreadId: void 0,
941
+ metadata: senderName || senderProfileUrl ? {
942
+ linkedin: {
943
+ senderName,
944
+ senderProfileUrl
945
+ }
946
+ } : void 0
947
+ });
948
+ try {
949
+ await msgDoc.save();
950
+ } catch (saveError) {
951
+ if ((saveError == null ? void 0 : saveError.code) === 11e3) {
952
+ console.log(`Duplicate key for conversation ${conversation._id}, already saved by concurrent request`);
953
+ continue;
924
954
  }
925
- } : void 0
926
- });
927
- await msgDoc.save();
928
- await Conversation.findByIdAndUpdate(conversation._id, {
929
- $set: {
930
- lastMessageAt: msgDoc.createdAt,
931
- lastMessagePreview: message.substring(0, 100),
932
- lastMessageSenderId: senderId,
933
- lastMessageSenderModel: senderModel
955
+ throw saveError;
934
956
  }
935
- });
936
- if (onMessageCreated) onMessageCreated(msgDoc);
937
- return msgDoc;
957
+ await Conversation.findByIdAndUpdate(conversation._id, {
958
+ $set: {
959
+ lastMessageAt: msgDoc.createdAt,
960
+ lastMessagePreview: message.substring(0, 100),
961
+ lastMessageSenderId: senderId,
962
+ lastMessageSenderModel: senderModel
963
+ }
964
+ });
965
+ if (onMessageCreated) onMessageCreated(msgDoc);
966
+ lastSavedMessage = msgDoc;
967
+ }
968
+ return lastSavedMessage;
938
969
  },
939
970
  /**
940
971
  * Receive a new message by emailChatId with full email metadata