Skip to content

Commit

Permalink
fix: Fixed WPP.contact.queryExists function for false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jan 26, 2022
1 parent c95a931 commit bf29270
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/contact/functions/queryExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ export interface QueryExistsResult {

const cache = new Map<string, QueryExistsResult | null>();

/**
* Check if the number exists and what is correct ID
*
* This help to identify numbers with nine digit in Brazil
*
* @example
* ```javascript
* const result = await WPP.contact.queryExists('<number>@c.us');
* console.log(result.wid); // Correct ID
* ```
*
* @category Chat
*/
export async function queryExists(
contactId: string | Wid
): Promise<QueryExistsResult | null> {
Expand All @@ -49,30 +62,34 @@ export async function queryExists(
return cache.get(id)!;
}

let result = await sendQueryExists(wid).catch(() => null);
let result: QueryExistsResult | null = null;

if (!result) {
const query = await Wap.queryExist(id);
if (query.status === 200) {
result = {
wid: query.jid,
biz: query.biz || false,
};
const query = await Wap.queryExist(id);
if (query.status === 200) {
result = {
wid: query.jid,
biz: query.biz || false,
};

// @todo: Migrate condition to isDisappearingModeEnabled()
if (result) {
const disappearing = await Wap.queryDisappearingMode(wid);
// @todo: Migrate condition to isDisappearingModeEnabled()
if (result) {
const disappearing = await Wap.queryDisappearingMode(wid).catch(
() => null
);

if (disappearing.status === 200) {
result.disappearingMode = {
duration: disappearing.duration!,
settingTimestamp: disappearing.settingTimestamp!,
};
}
if (disappearing?.status === 200) {
result.disappearingMode = {
duration: disappearing.duration!,
settingTimestamp: disappearing.settingTimestamp!,
};
}
}
}

if (!result) {
result = await sendQueryExists(wid).catch(() => null);
}

cache.set(id, result);

return result;
Expand Down

0 comments on commit bf29270

Please sign in to comment.