@@ -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