Skip to content

Commit 66b82ac

Browse files
committed
If contact stored with name, name added in return
1 parent 058acc5 commit 66b82ac

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/whatsapp/services/whatsapp.service.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ export class WAStartupService {
31303130
const jids: {
31313131
groups: { number: string; jid: string }[];
31323132
broadcast: { number: string; jid: string }[];
3133-
users: { number: string; jid: string }[];
3133+
users: { number: string; jid: string; contact?: string; name?: string }[];
31343134
} = {
31353135
groups: [],
31363136
broadcast: [],
@@ -3145,7 +3145,7 @@ export class WAStartupService {
31453145
} else if (jid === 'status@broadcast') {
31463146
jids.broadcast.push({ number, jid });
31473147
} else {
3148-
jids.users.push({ number, jid });
3148+
jids.users.push({ number, jid, contact: jid });
31493149
}
31503150
});
31513151

@@ -3172,22 +3172,39 @@ export class WAStartupService {
31723172
const verify = await this.client.onWhatsApp(
31733173
...jids.users.map(({ jid }) => (!jid.startsWith('+') ? `+${jid}` : jid)),
31743174
);
3175-
const users: OnWhatsAppDto[] = jids.users.map((user) => {
3176-
const MAX_SIMILARITY_THRESHOLD = 0.01;
3177-
const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28;
3178-
const jid = isBrWithDigit ? user.jid.slice(0, 4) + user.jid.slice(5) : user.jid;
3179-
3180-
const numberVerified = verify.find((v) => {
3181-
const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length);
3182-
const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length);
3183-
return mainJidSimilarity <= MAX_SIMILARITY_THRESHOLD || jidSimilarity <= MAX_SIMILARITY_THRESHOLD;
3184-
});
3185-
return {
3186-
exists: !!numberVerified?.exists,
3187-
jid: numberVerified?.jid || user.jid,
3188-
number: user.number,
3189-
};
3190-
});
3175+
const users: OnWhatsAppDto[] = await Promise.all(
3176+
jids.users.map(async (user) => {
3177+
const MAX_SIMILARITY_THRESHOLD = 0.01;
3178+
const isBrWithDigit = user.jid.startsWith('55') && user.jid.slice(4, 5) === '9' && user.jid.length === 28;
3179+
const jid = isBrWithDigit ? user.jid.slice(0, 4) + user.jid.slice(5) : user.jid;
3180+
3181+
const query: ContactQuery = {
3182+
where: {
3183+
owner: this.instance.name,
3184+
id: user.contact,
3185+
},
3186+
};
3187+
const contacts: ContactRaw[] = await this.repository.contact.find(query);
3188+
let firstContactFound;
3189+
if (contacts.length > 0) {
3190+
firstContactFound = contacts[0].pushName;
3191+
console.log(contacts[0]);
3192+
}
3193+
3194+
const numberVerified = verify.find((v) => {
3195+
const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length);
3196+
const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length);
3197+
return mainJidSimilarity <= MAX_SIMILARITY_THRESHOLD || jidSimilarity <= MAX_SIMILARITY_THRESHOLD;
3198+
});
3199+
return {
3200+
exists: !!numberVerified?.exists,
3201+
jid: numberVerified?.jid || user.jid,
3202+
name: firstContactFound,
3203+
number: user.number,
3204+
};
3205+
}),
3206+
);
3207+
31913208
onWhatsapp.push(...users);
31923209

31933210
return onWhatsapp;

0 commit comments

Comments
 (0)