whatsapp-store-db 1.3.46 → 1.3.47
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/handlers/chat.js +55 -0
- package/package.json +1 -1
package/dist/handlers/chat.js
CHANGED
|
@@ -8,6 +8,45 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
8
8
|
const prisma = (0, shared_1.usePrisma)();
|
|
9
9
|
const logger = (0, shared_1.useLogger)();
|
|
10
10
|
let listening = false;
|
|
11
|
+
/**
|
|
12
|
+
* Updates Contact records that have LID as their ID when we discover the phone number mapping.
|
|
13
|
+
* This handles the case where Contact events arrive before Chat events.
|
|
14
|
+
*/
|
|
15
|
+
const updateContactWithLidMapping = async (lidJid, phoneNumberId) => {
|
|
16
|
+
try {
|
|
17
|
+
// Check if there's a Contact with the LID as ID
|
|
18
|
+
const contactWithLid = await prisma.contact.findFirst({
|
|
19
|
+
select: { pkId: true },
|
|
20
|
+
where: { sessionId, id: lidJid },
|
|
21
|
+
});
|
|
22
|
+
if (contactWithLid) {
|
|
23
|
+
// Check if a contact with the phone number already exists
|
|
24
|
+
const existingContact = await prisma.contact.findFirst({
|
|
25
|
+
select: { pkId: true },
|
|
26
|
+
where: { sessionId, id: phoneNumberId },
|
|
27
|
+
});
|
|
28
|
+
if (existingContact) {
|
|
29
|
+
// Phone number contact exists, delete the LID contact (it's a duplicate)
|
|
30
|
+
await prisma.contact.delete({
|
|
31
|
+
where: { pkId: contactWithLid.pkId },
|
|
32
|
+
});
|
|
33
|
+
logger.info({ lidJid, phoneNumberId }, 'Removed duplicate LID contact (phone number contact exists)');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Update the LID contact to use the phone number
|
|
37
|
+
await prisma.contact.update({
|
|
38
|
+
where: { pkId: contactWithLid.pkId },
|
|
39
|
+
data: { id: phoneNumberId },
|
|
40
|
+
});
|
|
41
|
+
logger.info({ lidJid, phoneNumberId }, 'Updated Contact LID to phone number');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
// Silently ignore errors - this is a best-effort cleanup
|
|
47
|
+
logger.debug({ lidJid, phoneNumberId, error: e }, 'Failed to update Contact with LID mapping');
|
|
48
|
+
}
|
|
49
|
+
};
|
|
11
50
|
/**
|
|
12
51
|
* Resolves a chat ID, converting LID format to phone number format when possible.
|
|
13
52
|
* Returns an object with both the resolved ID and the original LID (if conversion happened).
|
|
@@ -73,6 +112,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
73
112
|
logger.error({ id, sessionId }, 'Failed to persist chat record after repeated retries');
|
|
74
113
|
};
|
|
75
114
|
const set = async ({ chats }) => {
|
|
115
|
+
const lidMappings = [];
|
|
76
116
|
try {
|
|
77
117
|
await prisma.$transaction(async (tx) => {
|
|
78
118
|
// Process chats in batches to avoid timeout
|
|
@@ -84,6 +124,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
84
124
|
// Set lidJid if we resolved from LID to phone number
|
|
85
125
|
if (lidJid) {
|
|
86
126
|
validatedData.lidJid = lidJid;
|
|
127
|
+
lidMappings.push({ lidJid, phoneNumberId: resolvedId });
|
|
87
128
|
}
|
|
88
129
|
return Object.assign(Object.assign({}, validatedData), { id: resolvedId });
|
|
89
130
|
});
|
|
@@ -104,6 +145,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
104
145
|
}, {
|
|
105
146
|
timeout: 30000, // 30 seconds for this specific transaction
|
|
106
147
|
});
|
|
148
|
+
// Update any Contact records that have LID as their ID (outside transaction)
|
|
149
|
+
for (const { lidJid, phoneNumberId } of lidMappings) {
|
|
150
|
+
await updateContactWithLidMapping(lidJid, phoneNumberId);
|
|
151
|
+
}
|
|
107
152
|
}
|
|
108
153
|
catch (e) {
|
|
109
154
|
logger.error(e, 'An error occured during chats set');
|
|
@@ -113,6 +158,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
113
158
|
try {
|
|
114
159
|
// Normalize and de-duplicate by resolved id (keep the last occurrence)
|
|
115
160
|
const dedupedById = new Map();
|
|
161
|
+
const lidMappings = [];
|
|
116
162
|
for (const c of chats) {
|
|
117
163
|
const { resolvedId, lidJid } = resolveChatId(c.id, c);
|
|
118
164
|
const transformedData = (0, utils_1.transformPrisma)(c);
|
|
@@ -120,6 +166,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
120
166
|
// Set lidJid if we resolved from LID to phone number
|
|
121
167
|
if (lidJid) {
|
|
122
168
|
validatedData.lidJid = lidJid;
|
|
169
|
+
lidMappings.push({ lidJid, phoneNumberId: resolvedId });
|
|
123
170
|
}
|
|
124
171
|
dedupedById.set(resolvedId, Object.assign(Object.assign({}, validatedData), { id: resolvedId }));
|
|
125
172
|
}
|
|
@@ -129,6 +176,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
129
176
|
createData: Object.assign({}, data),
|
|
130
177
|
updateData: Object.assign(Object.assign({}, data), { id: undefined }),
|
|
131
178
|
})));
|
|
179
|
+
// Update any Contact records that have LID as their ID
|
|
180
|
+
for (const { lidJid, phoneNumberId } of lidMappings) {
|
|
181
|
+
await updateContactWithLidMapping(lidJid, phoneNumberId);
|
|
182
|
+
}
|
|
132
183
|
}
|
|
133
184
|
catch (e) {
|
|
134
185
|
logger.error(e, 'An error occured during chats upsert');
|
|
@@ -157,6 +208,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
157
208
|
: { set: validatedData.unreadCount }
|
|
158
209
|
: undefined }),
|
|
159
210
|
});
|
|
211
|
+
// Update any Contact records that have LID as their ID
|
|
212
|
+
if (lidJid) {
|
|
213
|
+
await updateContactWithLidMapping(lidJid, chatId);
|
|
214
|
+
}
|
|
160
215
|
}
|
|
161
216
|
catch (e) {
|
|
162
217
|
logger.error(e, 'An error occured during chat update');
|