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 +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +129 -98
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +129 -98
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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
|
|
828
|
-
if (!
|
|
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
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
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
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
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
|
-
|
|
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
|
-
|
|
878
|
-
|
|
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
|
|
886
|
-
if (!
|
|
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
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
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
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
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
|
-
|
|
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
|
-
|
|
937
|
-
|
|
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
|