Skip to content

Commit eca4285

Browse files
committed
fix: Fixed the problem when do not save contacts when receive messages
1 parent 437803d commit eca4285

6 files changed

Lines changed: 124 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
### Features
44

55
* Route to send status broadcast
6+
* Added verbose logs
7+
* Insert allContacts in payload of endpoint sendStatus
68

79
### Fixed
810

911
* Adjusted set in webhook to go empty when enabled false
1012
* Adjust in store files
11-
* Added verbose logs
13+
* Fixed the problem when do not save contacts when receive messages
1214

1315
# 1.1.3 (2023-07-06 11:43)
1416

src/validate/validate.schema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ export const statusMessageSchema: JSONSchema7 = {
211211
description: '"statusJidList" must be an array of numeric strings',
212212
},
213213
},
214+
allContacts: { type: 'boolean', enum: [true, false] },
214215
},
215-
required: ['type', 'content', 'statusJidList'],
216-
...isNotEmpty('type', 'content', 'statusJidList'),
216+
required: ['type', 'content'],
217+
...isNotEmpty('type', 'content'),
217218
},
218219
},
219220
required: ['statusMessage'],

src/whatsapp/abstract/abstract.repository.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type IInsert = { insertCount: number };
77

88
export interface IRepository {
99
insert(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
10+
update(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
1011
find(query: any): Promise<any>;
1112
delete(query: any, force?: boolean): Promise<any>;
1213

@@ -48,6 +49,11 @@ export abstract class Repository implements IRepository {
4849
public insert(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
4950
throw new Error('Method not implemented.');
5051
}
52+
53+
public update(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
54+
throw new Error('Method not implemented.');
55+
}
56+
5157
public find(query: any): Promise<any> {
5258
throw new Error('Method not implemented.');
5359
}

src/whatsapp/dto/sendMessage.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class linkPreviewMessage {
3535
export class StatusMessage {
3636
type: string;
3737
content: string;
38-
statusJidList: string[];
38+
statusJidList?: string[];
39+
allContacts?: boolean;
3940
caption?: string;
4041
backgroundColor?: string;
4142
font?: number;

src/whatsapp/repository/contact.repository.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ export class ContactRepository extends Repository {
5353
}
5454
}
5555

56+
public async update(
57+
data: ContactRaw,
58+
instanceName: string,
59+
saveDb = false,
60+
): Promise<IInsert> {
61+
try {
62+
if (this.dbSettings.ENABLED && saveDb) {
63+
const contact = await this.contactModel.findOneAndUpdate(
64+
{ id: data.id },
65+
{ ...data },
66+
);
67+
return { insertCount: contact ? 1 : 0 };
68+
}
69+
70+
const store = this.configService.get<StoreConf>('STORE');
71+
72+
if (store.CONTACTS) {
73+
this.writeStore({
74+
path: join(this.storePath, 'contacts', instanceName),
75+
fileName: data.id,
76+
data,
77+
});
78+
79+
return { insertCount: 1 };
80+
}
81+
82+
return { insertCount: 0 };
83+
} catch (error) {
84+
return error;
85+
} finally {
86+
data = undefined;
87+
}
88+
}
89+
5690
public async find(query: ContactQuery): Promise<ContactRaw[]> {
5791
try {
5892
if (this.dbSettings.ENABLED) {

src/whatsapp/services/whatsapp.service.ts

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ export class WAStartupService {
770770
);
771771
},
772772

773-
'contacts.update': async (contacts: Partial<Contact>[]) => {
773+
'contacts.update': async (contacts: Partial<Contact>[], database: Database) => {
774774
this.logger.verbose('Event received: contacts.update');
775775

776776
this.logger.verbose('Verifying if contacts exists in database to update');
@@ -782,6 +782,18 @@ export class WAStartupService {
782782
profilePictureUrl: (await this.profilePicture(contact.id)).profilePictureUrl,
783783
owner: this.instance.wuid,
784784
});
785+
786+
this.logger.verbose('Updating contacts in database');
787+
await this.repository.contact.update(
788+
{
789+
id: contact.id,
790+
pushName: contact?.name ?? contact?.verifiedName,
791+
profilePictureUrl: (await this.profilePicture(contact.id)).profilePictureUrl,
792+
owner: this.instance.wuid,
793+
},
794+
this.instance.name,
795+
database.SAVE_DATA.CONTACTS,
796+
);
785797
}
786798

787799
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
@@ -909,6 +921,52 @@ export class WAStartupService {
909921
this.instance.name,
910922
database.SAVE_DATA.NEW_MESSAGE,
911923
);
924+
925+
this.logger.verbose('Verifying contact from message');
926+
const contact = await this.repository.contact.find({
927+
where: { owner: this.instance.wuid, id: received.key.remoteJid },
928+
});
929+
930+
if (contact?.length) {
931+
this.logger.verbose('Contact found in database');
932+
const contactRaw: ContactRaw = {
933+
id: received.key.remoteJid,
934+
pushName: contact[0].pushName,
935+
profilePictureUrl: (await this.profilePicture(received.key.remoteJid))
936+
.profilePictureUrl,
937+
owner: this.instance.wuid,
938+
};
939+
940+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
941+
await this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
942+
943+
this.logger.verbose('Updating contact in database');
944+
await this.repository.contact.update(
945+
contactRaw,
946+
this.instance.name,
947+
database.SAVE_DATA.CONTACTS,
948+
);
949+
return;
950+
}
951+
952+
this.logger.verbose('Contact not found in database');
953+
const contactRaw: ContactRaw = {
954+
id: received.key.remoteJid,
955+
pushName: received.pushName,
956+
profilePictureUrl: (await this.profilePicture(received.key.remoteJid))
957+
.profilePictureUrl,
958+
owner: this.instance.wuid,
959+
};
960+
961+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
962+
await this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
963+
964+
this.logger.verbose('Inserting contact in database');
965+
await this.repository.contact.insert(
966+
[contactRaw],
967+
this.instance.name,
968+
database.SAVE_DATA.CONTACTS,
969+
);
912970
},
913971

914972
'messages.update': async (args: WAMessageUpdate[], database: Database) => {
@@ -1072,7 +1130,7 @@ export class WAStartupService {
10721130
if (events['contacts.update']) {
10731131
this.logger.verbose('Listening event: contacts.update');
10741132
const payload = events['contacts.update'];
1075-
this.contactHandle['contacts.update'](payload);
1133+
this.contactHandle['contacts.update'](payload, database);
10761134
}
10771135
}
10781136
});
@@ -1386,12 +1444,22 @@ export class WAStartupService {
13861444
throw new BadRequestException('Content is required');
13871445
}
13881446

1389-
if (
1390-
!status.statusJidList ||
1391-
!Array.isArray(status.statusJidList) ||
1392-
!status.statusJidList.length
1393-
) {
1394-
throw new BadRequestException('Status jid list is required');
1447+
if (status.allContacts) {
1448+
const contacts = await this.repository.contact.find({
1449+
where: { owner: this.instance.wuid },
1450+
});
1451+
1452+
if (!contacts.length) {
1453+
throw new BadRequestException('Contacts not found');
1454+
}
1455+
1456+
status.statusJidList = contacts
1457+
.filter((contact) => contact.pushName)
1458+
.map((contact) => contact.id);
1459+
}
1460+
1461+
if (!status.statusJidList?.length && !status.allContacts) {
1462+
throw new BadRequestException('StatusJidList is required');
13951463
}
13961464

13971465
if (status.type === 'text') {

0 commit comments

Comments
 (0)