Skip to content

Commit 69f5cdd

Browse files
feat: added fetchProfile endpoint in chat controller
Added fetchProfile endpoint in chat controller
2 parents 40c230c + 1ec3ed3 commit 69f5cdd

File tree

5 files changed

+134
-11
lines changed

5 files changed

+134
-11
lines changed

src/validate/validate.schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,17 @@ export const profilePictureSchema: JSONSchema7 = {
587587
},
588588
};
589589

590+
export const profileSchema: JSONSchema7 = {
591+
type: 'object',
592+
properties: {
593+
wuid: { type: 'string' },
594+
name: { type: 'string' },
595+
picture: { type: 'string' },
596+
status: { type: 'string' },
597+
isBusiness: { type: 'boolean' },
598+
},
599+
};
600+
590601
export const messageValidateSchema: JSONSchema7 = {
591602
$id: v4(),
592603
type: 'object',

src/whatsapp/controllers/chat.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export class ChatController {
4747
logger.verbose('requested fetchProfilePicture from ' + instanceName + ' instance');
4848
return await this.waMonitor.waInstances[instanceName].profilePicture(data.number);
4949
}
50+
51+
public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) {
52+
logger.verbose('requested fetchProfile from ' + instanceName + ' instance');
53+
return await this.waMonitor.waInstances[instanceName].fetchProfile(instanceName, data.number);
54+
}
5055

5156
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {
5257
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
privacySettingsSchema,
99
profileNameSchema,
1010
profilePictureSchema,
11+
profileSchema,
1112
profileStatusSchema,
1213
readMessageSchema,
1314
whatsappNumberSchema,
@@ -129,6 +130,23 @@ export class ChatRouter extends RouterBroker {
129130

130131
return res.status(HttpStatus.OK).json(response);
131132
})
133+
.post(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
134+
logger.verbose('request received in fetchProfile');
135+
logger.verbose('request body: ');
136+
logger.verbose(req.body);
137+
138+
logger.verbose('request query: ');
139+
logger.verbose(req.query);
140+
141+
const response = await this.dataValidate<NumberDto>({
142+
request: req,
143+
schema: profileSchema,
144+
ClassRef: NumberDto,
145+
execute: (instance, data) => chatController.fetchProfile(instance, data),
146+
});
147+
148+
return res.status(HttpStatus.OK).json(response);
149+
})
132150
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
133151
logger.verbose('request received in findContacts');
134152
logger.verbose('request body: ');

src/whatsapp/services/whatsapp.service.ts

Lines changed: 87 additions & 11 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,
@@ -1443,6 +1444,79 @@ export class WAStartupService {
14431444
};
14441445
}
14451446
}
1447+
1448+
public async getStatus(number: string) {
1449+
const jid = this.createJid(number);
1450+
1451+
this.logger.verbose('Getting profile status with jid:' + jid);
1452+
try {
1453+
this.logger.verbose('Getting status');
1454+
return {
1455+
wuid: jid,
1456+
status: (await this.client.fetchStatus(jid))?.status,
1457+
};
1458+
} catch (error) {
1459+
this.logger.verbose('Status not found');
1460+
return {
1461+
wuid: jid,
1462+
status: null,
1463+
};
1464+
}
1465+
}
1466+
1467+
public async fetchProfile(instanceName: string, number?: string) {
1468+
const jid = (number)
1469+
? this.createJid(number)
1470+
: this.client?.user?.id;
1471+
this.logger.verbose('Getting profile with jid: ' + jid);
1472+
try {
1473+
this.logger.verbose('Getting profile info');
1474+
const business = await this.fetchBusinessProfile(jid);
1475+
1476+
if (number) {
1477+
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
1478+
const picture = await this.profilePicture(jid);
1479+
const status = await this.getStatus(jid);
1480+
1481+
return {
1482+
wuid: jid,
1483+
name: info?.name,
1484+
numberExists: info?.exists,
1485+
picture: picture?.profilePictureUrl,
1486+
status: status?.status,
1487+
isBusiness: business.isBusiness,
1488+
email: business?.email,
1489+
description: business?.description,
1490+
website: business?.website?.shift(),
1491+
};
1492+
} else {
1493+
const info = await waMonitor.instanceInfo(instanceName);
1494+
1495+
return {
1496+
wuid: jid,
1497+
name: info?.instance?.profileName,
1498+
numberExists: true,
1499+
picture: info?.instance?.profilePictureUrl,
1500+
status: info?.instance?.profileStatus,
1501+
isBusiness: business.isBusiness,
1502+
email: business?.email,
1503+
description: business?.description,
1504+
website: business?.website?.shift(),
1505+
};
1506+
}
1507+
1508+
} catch (error) {
1509+
this.logger.verbose('Profile not found');
1510+
return {
1511+
wuid: jid,
1512+
name: null,
1513+
picture: null,
1514+
status: null,
1515+
os: null,
1516+
isBusiness: false,
1517+
};
1518+
}
1519+
}
14461520

14471521
private async sendMessageWithTyping<T = proto.IMessage>(
14481522
number: string,
@@ -2460,29 +2534,31 @@ export class WAStartupService {
24602534
}
24612535
}
24622536

2463-
public async fetchBusinessProfile(number: string) {
2537+
public async fetchBusinessProfile(number: string) : Promise<NumberBusiness> {
24642538
this.logger.verbose('Fetching business profile');
24652539
try {
2466-
let jid;
2467-
2468-
if (!number) {
2469-
jid = this.instance.wuid;
2470-
} else {
2471-
jid = this.createJid(number);
2472-
}
2540+
const jid = (number)
2541+
? this.createJid(number)
2542+
: this.instance.wuid;
24732543

24742544
const profile = await this.client.getBusinessProfile(jid);
24752545
this.logger.verbose('Trying to get business profile');
24762546

24772547
if (!profile) {
2548+
const info = await this.whatsappNumber({ numbers: [jid] });
2549+
24782550
return {
2479-
exists: false,
2480-
message: 'Business profile not found',
2551+
isBusiness: false,
2552+
message: 'Not is business profile',
2553+
...info?.shift()
24812554
};
24822555
}
24832556

24842557
this.logger.verbose('Business profile fetched');
2485-
return profile;
2558+
return {
2559+
isBusiness: true,
2560+
...profile
2561+
};
24862562
} catch (error) {
24872563
throw new InternalServerErrorException(
24882564
'Error updating profile name',

0 commit comments

Comments
 (0)