fix: Fixed the problem when do not save contacts when receive messages

This commit is contained in:
Davidson Gomes 2023-07-08 07:15:34 -03:00
parent 437803da07
commit eca4285ea8
6 changed files with 124 additions and 12 deletions

View File

@ -3,12 +3,14 @@
### Features ### Features
* Route to send status broadcast * Route to send status broadcast
* Added verbose logs
* Insert allContacts in payload of endpoint sendStatus
### Fixed ### Fixed
* Adjusted set in webhook to go empty when enabled false * Adjusted set in webhook to go empty when enabled false
* Adjust in store files * Adjust in store files
* Added verbose logs * Fixed the problem when do not save contacts when receive messages
# 1.1.3 (2023-07-06 11:43) # 1.1.3 (2023-07-06 11:43)

View File

@ -211,9 +211,10 @@ export const statusMessageSchema: JSONSchema7 = {
description: '"statusJidList" must be an array of numeric strings', description: '"statusJidList" must be an array of numeric strings',
}, },
}, },
allContacts: { type: 'boolean', enum: [true, false] },
}, },
required: ['type', 'content', 'statusJidList'], required: ['type', 'content'],
...isNotEmpty('type', 'content', 'statusJidList'), ...isNotEmpty('type', 'content'),
}, },
}, },
required: ['statusMessage'], required: ['statusMessage'],

View File

@ -7,6 +7,7 @@ export type IInsert = { insertCount: number };
export interface IRepository { export interface IRepository {
insert(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>; insert(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
update(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
find(query: any): Promise<any>; find(query: any): Promise<any>;
delete(query: any, force?: boolean): Promise<any>; delete(query: any, force?: boolean): Promise<any>;
@ -48,6 +49,11 @@ export abstract class Repository implements IRepository {
public insert(data: any, instanceName: string, saveDb = false): Promise<IInsert> { public insert(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }
public update(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
throw new Error('Method not implemented.');
}
public find(query: any): Promise<any> { public find(query: any): Promise<any> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }

View File

@ -35,7 +35,8 @@ class linkPreviewMessage {
export class StatusMessage { export class StatusMessage {
type: string; type: string;
content: string; content: string;
statusJidList: string[]; statusJidList?: string[];
allContacts?: boolean;
caption?: string; caption?: string;
backgroundColor?: string; backgroundColor?: string;
font?: number; font?: number;

View File

@ -53,6 +53,40 @@ export class ContactRepository extends Repository {
} }
} }
public async update(
data: ContactRaw,
instanceName: string,
saveDb = false,
): Promise<IInsert> {
try {
if (this.dbSettings.ENABLED && saveDb) {
const contact = await this.contactModel.findOneAndUpdate(
{ id: data.id },
{ ...data },
);
return { insertCount: contact ? 1 : 0 };
}
const store = this.configService.get<StoreConf>('STORE');
if (store.CONTACTS) {
this.writeStore({
path: join(this.storePath, 'contacts', instanceName),
fileName: data.id,
data,
});
return { insertCount: 1 };
}
return { insertCount: 0 };
} catch (error) {
return error;
} finally {
data = undefined;
}
}
public async find(query: ContactQuery): Promise<ContactRaw[]> { public async find(query: ContactQuery): Promise<ContactRaw[]> {
try { try {
if (this.dbSettings.ENABLED) { if (this.dbSettings.ENABLED) {

View File

@ -770,7 +770,7 @@ export class WAStartupService {
); );
}, },
'contacts.update': async (contacts: Partial<Contact>[]) => { 'contacts.update': async (contacts: Partial<Contact>[], database: Database) => {
this.logger.verbose('Event received: contacts.update'); this.logger.verbose('Event received: contacts.update');
this.logger.verbose('Verifying if contacts exists in database to update'); this.logger.verbose('Verifying if contacts exists in database to update');
@ -782,6 +782,18 @@ export class WAStartupService {
profilePictureUrl: (await this.profilePicture(contact.id)).profilePictureUrl, profilePictureUrl: (await this.profilePicture(contact.id)).profilePictureUrl,
owner: this.instance.wuid, owner: this.instance.wuid,
}); });
this.logger.verbose('Updating contacts in database');
await this.repository.contact.update(
{
id: contact.id,
pushName: contact?.name ?? contact?.verifiedName,
profilePictureUrl: (await this.profilePicture(contact.id)).profilePictureUrl,
owner: this.instance.wuid,
},
this.instance.name,
database.SAVE_DATA.CONTACTS,
);
} }
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE'); this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
@ -909,6 +921,52 @@ export class WAStartupService {
this.instance.name, this.instance.name,
database.SAVE_DATA.NEW_MESSAGE, database.SAVE_DATA.NEW_MESSAGE,
); );
this.logger.verbose('Verifying contact from message');
const contact = await this.repository.contact.find({
where: { owner: this.instance.wuid, id: received.key.remoteJid },
});
if (contact?.length) {
this.logger.verbose('Contact found in database');
const contactRaw: ContactRaw = {
id: received.key.remoteJid,
pushName: contact[0].pushName,
profilePictureUrl: (await this.profilePicture(received.key.remoteJid))
.profilePictureUrl,
owner: this.instance.wuid,
};
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
await this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
this.logger.verbose('Updating contact in database');
await this.repository.contact.update(
contactRaw,
this.instance.name,
database.SAVE_DATA.CONTACTS,
);
return;
}
this.logger.verbose('Contact not found in database');
const contactRaw: ContactRaw = {
id: received.key.remoteJid,
pushName: received.pushName,
profilePictureUrl: (await this.profilePicture(received.key.remoteJid))
.profilePictureUrl,
owner: this.instance.wuid,
};
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
await this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
this.logger.verbose('Inserting contact in database');
await this.repository.contact.insert(
[contactRaw],
this.instance.name,
database.SAVE_DATA.CONTACTS,
);
}, },
'messages.update': async (args: WAMessageUpdate[], database: Database) => { 'messages.update': async (args: WAMessageUpdate[], database: Database) => {
@ -1072,7 +1130,7 @@ export class WAStartupService {
if (events['contacts.update']) { if (events['contacts.update']) {
this.logger.verbose('Listening event: contacts.update'); this.logger.verbose('Listening event: contacts.update');
const payload = events['contacts.update']; const payload = events['contacts.update'];
this.contactHandle['contacts.update'](payload); this.contactHandle['contacts.update'](payload, database);
} }
} }
}); });
@ -1386,12 +1444,22 @@ export class WAStartupService {
throw new BadRequestException('Content is required'); throw new BadRequestException('Content is required');
} }
if ( if (status.allContacts) {
!status.statusJidList || const contacts = await this.repository.contact.find({
!Array.isArray(status.statusJidList) || where: { owner: this.instance.wuid },
!status.statusJidList.length });
) {
throw new BadRequestException('Status jid list is required'); if (!contacts.length) {
throw new BadRequestException('Contacts not found');
}
status.statusJidList = contacts
.filter((contact) => contact.pushName)
.map((contact) => contact.id);
}
if (!status.statusJidList?.length && !status.allContacts) {
throw new BadRequestException('StatusJidList is required');
} }
if (status.type === 'text') { if (status.type === 'text') {