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.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
|
|
836
|
-
if (!
|
|
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
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
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
|
-
|
|
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
|
-
|
|
886
|
-
|
|
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
|
|
894
|
-
if (!
|
|
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
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
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
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
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
|
-
|
|
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
|
-
|
|
945
|
-
|
|
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
|