Skip to content

Commit b095465

Browse files
Merge pull request #390 from leandrosroc/develop
feat: If contact stored with name, name added in return
2 parents 6e84c1d + 8fa61e4 commit b095465

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/whatsapp/services/whatsapp.service.ts

Lines changed: 33 additions & 17 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; name?: string }[];
31343134
} = {
31353135
groups: [],
31363136
broadcast: [],
@@ -3172,22 +3172,38 @@ 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.jid.startsWith('+') ? user.jid.substring(1) : user.jid,
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+
}
3192+
3193+
const numberVerified = verify.find((v) => {
3194+
const mainJidSimilarity = levenshtein.get(user.jid, v.jid) / Math.max(user.jid.length, v.jid.length);
3195+
const jidSimilarity = levenshtein.get(jid, v.jid) / Math.max(jid.length, v.jid.length);
3196+
return mainJidSimilarity <= MAX_SIMILARITY_THRESHOLD || jidSimilarity <= MAX_SIMILARITY_THRESHOLD;
3197+
});
3198+
return {
3199+
exists: !!numberVerified?.exists,
3200+
jid: numberVerified?.jid || user.jid,
3201+
name: firstContactFound,
3202+
number: user.number,
3203+
};
3204+
}),
3205+
);
3206+
31913207
onWhatsapp.push(...users);
31923208

31933209
return onWhatsapp;

0 commit comments

Comments
 (0)