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