whatsapp-store-db 1.3.44 → 1.3.45

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.
@@ -7,14 +7,41 @@ function contactHandler(sessionId, event, getJid = undefined) {
7
7
  const prisma = (0, shared_1.usePrisma)();
8
8
  const logger = (0, shared_1.useLogger)();
9
9
  let listening = false;
10
- const resolveContactId = (id, contact) => {
10
+ /**
11
+ * Resolves a contact ID, converting LID format to phone number format when possible.
12
+ * Checks multiple sources: contact fields, getJid function, and Chat table lookup.
13
+ */
14
+ const resolveContactId = async (id, contact) => {
11
15
  // console.log("contactHandler:contact:", contact);
12
- // Prefer primary number when we get a LID id and contact carries pn/jid
16
+ // Prefer primary number when we get a LID id
13
17
  if (id === null || id === void 0 ? void 0 : id.endsWith('@lid')) {
18
+ // First, check contact-level fields
14
19
  const candidate = ((contact === null || contact === void 0 ? void 0 : contact.senderPn) || (contact === null || contact === void 0 ? void 0 : contact.pnJid) || (contact === null || contact === void 0 ? void 0 : contact.jid));
15
- if (candidate) {
20
+ if (candidate && candidate.includes('@s.whatsapp.net')) {
16
21
  return (0, baileys_1.jidNormalizedUser)(candidate);
17
22
  }
23
+ // Second, try getJid function
24
+ const jidByLid = typeof getJid === 'function' ? getJid(id || '') : undefined;
25
+ if (jidByLid && jidByLid.includes('@s.whatsapp.net')) {
26
+ return (0, baileys_1.jidNormalizedUser)(jidByLid);
27
+ }
28
+ // Third, look up the Chat table for a mapping (lidJid -> id)
29
+ try {
30
+ const chatWithLid = await prisma.chat.findFirst({
31
+ select: { id: true },
32
+ where: {
33
+ sessionId,
34
+ lidJid: id,
35
+ id: { endsWith: '@s.whatsapp.net' }
36
+ },
37
+ });
38
+ if (chatWithLid === null || chatWithLid === void 0 ? void 0 : chatWithLid.id) {
39
+ return (0, baileys_1.jidNormalizedUser)(chatWithLid.id);
40
+ }
41
+ }
42
+ catch (e) {
43
+ // Silently ignore lookup errors, fall through to default
44
+ }
18
45
  }
19
46
  const jidByLid = typeof getJid === 'function' ? getJid(id || '') : undefined;
20
47
  return (0, baileys_1.jidNormalizedUser)(jidByLid !== null && jidByLid !== void 0 ? jidByLid : id);
@@ -31,11 +58,11 @@ function contactHandler(sessionId, event, getJid = undefined) {
31
58
  };
32
59
  const set = async ({ contacts }) => {
33
60
  try {
34
- const normalizedContacts = contacts.map((c) => {
35
- const id = resolveContactId(c.id, c);
61
+ const normalizedContacts = await Promise.all(contacts.map(async (c) => {
62
+ const id = await resolveContactId(c.id, c);
36
63
  const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
37
64
  return Object.assign(Object.assign({}, data), { id });
38
- });
65
+ }));
39
66
  const contactIds = normalizedContacts.map((c) => c.id);
40
67
  const deletedOldContactIds = (await prisma.contact.findMany({
41
68
  select: { id: true },
@@ -60,11 +87,11 @@ function contactHandler(sessionId, event, getJid = undefined) {
60
87
  };
61
88
  const upsert = async (contacts) => {
62
89
  try {
63
- const normalizedContacts = contacts.map((c) => {
64
- const id = resolveContactId(c.id, c);
90
+ const normalizedContacts = await Promise.all(contacts.map(async (c) => {
91
+ const id = await resolveContactId(c.id, c);
65
92
  const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
66
93
  return Object.assign(Object.assign({}, data), { id });
67
- });
94
+ }));
68
95
  await Promise.any(normalizedContacts.map((data) => prisma.contact.upsert({
69
96
  select: { pkId: true },
70
97
  create: Object.assign(Object.assign({}, data), { sessionId }),
@@ -83,7 +110,7 @@ function contactHandler(sessionId, event, getJid = undefined) {
83
110
  continue;
84
111
  }
85
112
  try {
86
- const contactId = resolveContactId(update.id, update);
113
+ const contactId = await resolveContactId(update.id, update);
87
114
  const transformedData = sanitizeContactData((0, utils_1.transformPrisma)(update));
88
115
  await prisma.contact.upsert({
89
116
  select: { pkId: true },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whatsapp-store-db",
3
- "version": "1.3.44",
3
+ "version": "1.3.45",
4
4
  "description": "Minimal Baileys data storage for your favorite DBMS built with Prisma",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",