Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Adição de FetchProfile" #20

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,6 @@ export const profilePictureSchema: JSONSchema7 = {
},
};

export const profileSchema: JSONSchema7 = {
type: 'object',
properties: {
wuid: { type: 'string' },
name: { type: 'string' },
picture: { type: 'string' },
status: { type: 'string' },
isBusiness: { type: 'boolean' },
},
};

export const messageValidateSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
Expand Down
5 changes: 0 additions & 5 deletions src/whatsapp/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ export class ChatController {
logger.verbose('requested fetchProfilePicture from ' + instanceName + ' instance');
return await this.waMonitor.waInstances[instanceName].profilePicture(data.number);
}

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

public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');
Expand Down
13 changes: 0 additions & 13 deletions src/whatsapp/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ export class NumberDto {
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 {
name: string;
}
Expand Down
18 changes: 0 additions & 18 deletions src/whatsapp/routers/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
privacySettingsSchema,
profileNameSchema,
profilePictureSchema,
profileSchema,
profileStatusSchema,
readMessageSchema,
whatsappNumberSchema,
Expand Down Expand Up @@ -130,23 +129,6 @@ export class ChatRouter extends RouterBroker {

return res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
logger.verbose('request received in fetchProfile');
logger.verbose('request body: ');
logger.verbose(req.body);

logger.verbose('request query: ');
logger.verbose(req.query);

const response = await this.dataValidate<NumberDto>({
request: req,
schema: profileSchema,
ClassRef: NumberDto,
execute: (instance, data) => chatController.fetchProfile(instance, data),
});

return res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
logger.verbose('request received in findContacts');
logger.verbose('request body: ');
Expand Down
98 changes: 11 additions & 87 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import {
ArchiveChatDto,
DeleteMessage,
OnWhatsAppDto,
NumberBusiness,
PrivacySettingDto,
ReadMessageDto,
WhatsAppNumberDto,
Expand Down Expand Up @@ -1444,79 +1443,6 @@ export class WAStartupService {
};
}
}

public async getStatus(number: string) {
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);
try {
this.logger.verbose('Getting profile info');
const business = await this.fetchBusinessProfile(jid);

if (number) {
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
const picture = await this.profilePicture(jid);
const status = await this.getStatus(jid);

return {
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) {
this.logger.verbose('Profile not found');
return {
wuid: jid,
name: null,
picture: null,
status: null,
os: null,
isBusiness: false,
};
}
}

private async sendMessageWithTyping<T = proto.IMessage>(
number: string,
Expand Down Expand Up @@ -2534,31 +2460,29 @@ export class WAStartupService {
}
}

public async fetchBusinessProfile(number: string) : Promise<NumberBusiness> {
public async fetchBusinessProfile(number: string) {
this.logger.verbose('Fetching business profile');
try {
const jid = (number)
? this.createJid(number)
: this.instance.wuid;
let jid;

if (!number) {
jid = this.instance.wuid;
} else {
jid = this.createJid(number);
}

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

if (!profile) {
const info = await this.whatsappNumber({ numbers: [jid] });

return {
isBusiness: false,
message: 'Not is business profile',
...info?.shift()
exists: false,
message: 'Business profile not found',
};
}

this.logger.verbose('Business profile fetched');
return {
isBusiness: true,
...profile
};
return profile;
} catch (error) {
throw new InternalServerErrorException(
'Error updating profile name',
Expand Down