Melhorias

This commit is contained in:
Alan Mosko 2023-07-23 10:18:00 -03:00
parent e851696430
commit 1ec3ed32ee
4 changed files with 84 additions and 25 deletions

View File

@ -50,7 +50,7 @@ export class ChatController {
public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) { public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) {
logger.verbose('requested fetchProfile from ' + instanceName + ' instance'); logger.verbose('requested fetchProfile from ' + instanceName + ' instance');
return await this.waMonitor.waInstances[instanceName].profile(instanceName, data.number); return await this.waMonitor.waInstances[instanceName].fetchProfile(instanceName, data.number);
} }
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) { public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {

View File

@ -26,6 +26,19 @@ export class NumberDto {
number: string; number: string;
} }
export class NumberBusiness {
wid?: string;
jid?: string;
exists?: boolean;
isBusiness: boolean;
name?: string;
message?: string;
description?: string;
email?: string;
website?: string[];
address?: string;
}
export class ProfileNameDto { export class ProfileNameDto {
name: string; name: string;
} }

View File

@ -130,7 +130,7 @@ export class ChatRouter extends RouterBroker {
return res.status(HttpStatus.OK).json(response); return res.status(HttpStatus.OK).json(response);
}) })
.get(this.routerPath('fetchProfile'), ...guards, async (req, res) => { .post(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
logger.verbose('request received in fetchProfile'); logger.verbose('request received in fetchProfile');
logger.verbose('request body: '); logger.verbose('request body: ');
logger.verbose(req.body); logger.verbose(req.body);

View File

@ -85,6 +85,7 @@ import {
ArchiveChatDto, ArchiveChatDto,
DeleteMessage, DeleteMessage,
OnWhatsAppDto, OnWhatsAppDto,
NumberBusiness,
PrivacySettingDto, PrivacySettingDto,
ReadMessageDto, ReadMessageDto,
WhatsAppNumberDto, WhatsAppNumberDto,
@ -1437,23 +1438,66 @@ export class WAStartupService {
} }
} }
public async profile(instanceName: string) { public async getStatus(number: string) {
const jid = this.client?.user?.id; const jid = this.createJid(number);
this.logger.verbose('Getting profile status with jid:' + jid);
try {
this.logger.verbose('Getting status');
return {
wuid: jid,
status: (await this.client.fetchStatus(jid))?.status,
};
} catch (error) {
this.logger.verbose('Status not found');
return {
wuid: jid,
status: null,
};
}
}
public async fetchProfile(instanceName: string, number?: string) {
const jid = (number)
? this.createJid(number)
: this.client?.user?.id;
this.logger.verbose('Getting profile with jid: ' + jid); this.logger.verbose('Getting profile with jid: ' + jid);
try { try {
this.logger.verbose('Getting profile info'); this.logger.verbose('Getting profile info');
const info = await waMonitor.instanceInfo(instanceName);
const business = await this.fetchBusinessProfile(jid); const business = await this.fetchBusinessProfile(jid);
return { if (number) {
wuid: jid, const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
name: info?.instance?.profileName, const picture = await this.profilePicture(jid);
picture: info?.instance?.profilePictureUrl, const status = await this.getStatus(jid);
status: info?.instance?.profileStatus,
os: this.client?.authState?.creds?.platform, return {
isBusiness: typeof business !== 'undefined', wuid: jid,
}; name: info?.name,
numberExists: info?.exists,
picture: picture?.profilePictureUrl,
status: status?.status,
isBusiness: business.isBusiness,
email: business?.email,
description: business?.description,
website: business?.website?.shift(),
};
} else {
const info = await waMonitor.instanceInfo(instanceName);
return {
wuid: jid,
name: info?.instance?.profileName,
numberExists: true,
picture: info?.instance?.profilePictureUrl,
status: info?.instance?.profileStatus,
isBusiness: business.isBusiness,
email: business?.email,
description: business?.description,
website: business?.website?.shift(),
};
}
} catch (error) { } catch (error) {
this.logger.verbose('Profile not found'); this.logger.verbose('Profile not found');
return { return {
@ -2463,29 +2507,31 @@ export class WAStartupService {
} }
} }
public async fetchBusinessProfile(number: string) { public async fetchBusinessProfile(number: string) : Promise<NumberBusiness> {
this.logger.verbose('Fetching business profile'); this.logger.verbose('Fetching business profile');
try { try {
let jid; const jid = (number)
? this.createJid(number)
if (!number) { : this.instance.wuid;
jid = this.instance.wuid;
} else {
jid = this.createJid(number);
}
const profile = await this.client.getBusinessProfile(jid); const profile = await this.client.getBusinessProfile(jid);
this.logger.verbose('Trying to get business profile'); this.logger.verbose('Trying to get business profile');
if (!profile) { if (!profile) {
const info = await this.whatsappNumber({ numbers: [jid] });
return { return {
exists: false, isBusiness: false,
message: 'Business profile not found', message: 'Not is business profile',
...info?.shift()
}; };
} }
this.logger.verbose('Business profile fetched'); this.logger.verbose('Business profile fetched');
return profile; return {
isBusiness: true,
...profile
};
} catch (error) { } catch (error) {
throw new InternalServerErrorException( throw new InternalServerErrorException(
'Error updating profile name', 'Error updating profile name',