Skip to content

Commit 1ec3ed3

Browse files
committed
Melhorias
1 parent e851696 commit 1ec3ed3

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

src/whatsapp/controllers/chat.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class ChatController {
5050

5151
public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) {
5252
logger.verbose('requested fetchProfile from ' + instanceName + ' instance');
53-
return await this.waMonitor.waInstances[instanceName].profile(instanceName, data.number);
53+
return await this.waMonitor.waInstances[instanceName].fetchProfile(instanceName, data.number);
5454
}
5555

5656
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {

src/whatsapp/dto/chat.dto.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ export class NumberDto {
2626
number: string;
2727
}
2828

29+
export class NumberBusiness {
30+
wid?: string;
31+
jid?: string;
32+
exists?: boolean;
33+
isBusiness: boolean;
34+
name?: string;
35+
message?: string;
36+
description?: string;
37+
email?: string;
38+
website?: string[];
39+
address?: string;
40+
}
41+
2942
export class ProfileNameDto {
3043
name: string;
3144
}

src/whatsapp/routers/chat.router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class ChatRouter extends RouterBroker {
130130

131131
return res.status(HttpStatus.OK).json(response);
132132
})
133-
.get(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
133+
.post(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
134134
logger.verbose('request received in fetchProfile');
135135
logger.verbose('request body: ');
136136
logger.verbose(req.body);

src/whatsapp/services/whatsapp.service.ts

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import {
8585
ArchiveChatDto,
8686
DeleteMessage,
8787
OnWhatsAppDto,
88+
NumberBusiness,
8889
PrivacySettingDto,
8990
ReadMessageDto,
9091
WhatsAppNumberDto,
@@ -1437,23 +1438,66 @@ export class WAStartupService {
14371438
}
14381439
}
14391440

1440-
public async profile(instanceName: string) {
1441-
const jid = this.client?.user?.id;
1442-
1441+
public async getStatus(number: string) {
1442+
const jid = this.createJid(number);
1443+
1444+
this.logger.verbose('Getting profile status with jid:' + jid);
1445+
try {
1446+
this.logger.verbose('Getting status');
1447+
return {
1448+
wuid: jid,
1449+
status: (await this.client.fetchStatus(jid))?.status,
1450+
};
1451+
} catch (error) {
1452+
this.logger.verbose('Status not found');
1453+
return {
1454+
wuid: jid,
1455+
status: null,
1456+
};
1457+
}
1458+
}
1459+
1460+
public async fetchProfile(instanceName: string, number?: string) {
1461+
const jid = (number)
1462+
? this.createJid(number)
1463+
: this.client?.user?.id;
14431464
this.logger.verbose('Getting profile with jid: ' + jid);
14441465
try {
14451466
this.logger.verbose('Getting profile info');
1446-
const info = await waMonitor.instanceInfo(instanceName);
14471467
const business = await this.fetchBusinessProfile(jid);
14481468

1449-
return {
1450-
wuid: jid,
1451-
name: info?.instance?.profileName,
1452-
picture: info?.instance?.profilePictureUrl,
1453-
status: info?.instance?.profileStatus,
1454-
os: this.client?.authState?.creds?.platform,
1455-
isBusiness: typeof business !== 'undefined',
1456-
};
1469+
if (number) {
1470+
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
1471+
const picture = await this.profilePicture(jid);
1472+
const status = await this.getStatus(jid);
1473+
1474+
return {
1475+
wuid: jid,
1476+
name: info?.name,
1477+
numberExists: info?.exists,
1478+
picture: picture?.profilePictureUrl,
1479+
status: status?.status,
1480+
isBusiness: business.isBusiness,
1481+
email: business?.email,
1482+
description: business?.description,
1483+
website: business?.website?.shift(),
1484+
};
1485+
} else {
1486+
const info = await waMonitor.instanceInfo(instanceName);
1487+
1488+
return {
1489+
wuid: jid,
1490+
name: info?.instance?.profileName,
1491+
numberExists: true,
1492+
picture: info?.instance?.profilePictureUrl,
1493+
status: info?.instance?.profileStatus,
1494+
isBusiness: business.isBusiness,
1495+
email: business?.email,
1496+
description: business?.description,
1497+
website: business?.website?.shift(),
1498+
};
1499+
}
1500+
14571501
} catch (error) {
14581502
this.logger.verbose('Profile not found');
14591503
return {
@@ -2463,29 +2507,31 @@ export class WAStartupService {
24632507
}
24642508
}
24652509

2466-
public async fetchBusinessProfile(number: string) {
2510+
public async fetchBusinessProfile(number: string) : Promise<NumberBusiness> {
24672511
this.logger.verbose('Fetching business profile');
24682512
try {
2469-
let jid;
2470-
2471-
if (!number) {
2472-
jid = this.instance.wuid;
2473-
} else {
2474-
jid = this.createJid(number);
2475-
}
2513+
const jid = (number)
2514+
? this.createJid(number)
2515+
: this.instance.wuid;
24762516

24772517
const profile = await this.client.getBusinessProfile(jid);
24782518
this.logger.verbose('Trying to get business profile');
24792519

24802520
if (!profile) {
2521+
const info = await this.whatsappNumber({ numbers: [jid] });
2522+
24812523
return {
2482-
exists: false,
2483-
message: 'Business profile not found',
2524+
isBusiness: false,
2525+
message: 'Not is business profile',
2526+
...info?.shift()
24842527
};
24852528
}
24862529

24872530
this.logger.verbose('Business profile fetched');
2488-
return profile;
2531+
return {
2532+
isBusiness: true,
2533+
...profile
2534+
};
24892535
} catch (error) {
24902536
throw new InternalServerErrorException(
24912537
'Error updating profile name',

0 commit comments

Comments
 (0)