@@ -541,8 +541,18 @@ export class ConsentService {
541541 const dreamsyncUser = await this . findDreamSyncUser ( ) ;
542542 if ( ! dreamsyncUser ) return ;
543543
544- const adminChat = await this . findOrCreateMutualChat ( adminUser . id ) ;
545- if ( ! adminChat ) return ;
544+ const adminChatResult = await this . findOrCreateMutualChat ( adminUser . id ) ;
545+ if ( ! adminChatResult . chat ) return ;
546+
547+ const adminChat = adminChatResult . chat ;
548+ const wasCreated = adminChatResult . wasCreated ;
549+
550+ // If chat was just created, wait 15 seconds before sending message
551+ if ( wasCreated ) {
552+ console . log ( `⏳ Admin chat was just created, waiting 15 seconds before sending message...` ) ;
553+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
554+ console . log ( `✅ 15-second delay completed for admin message` ) ;
555+ }
546556
547557 const activity = this . extractActivityFromMatch ( match ) ;
548558 const messageContent = `$$system-message$$
@@ -602,11 +612,21 @@ DreamSync
602612 }
603613 console . log ( `✅ DreamSync user found: ${ dreamsyncUser . id } ` ) ;
604614
605- const adminChat = await this . findOrCreateMutualChat ( adminUser . id ) ;
606- if ( ! adminChat ) {
615+ const adminChatResult = await this . findOrCreateMutualChat ( adminUser . id ) ;
616+ if ( ! adminChatResult . chat ) {
607617 console . error ( "❌ Could not find/create admin chat" ) ;
608618 return ;
609619 }
620+
621+ const adminChat = adminChatResult . chat ;
622+ const wasCreated = adminChatResult . wasCreated ;
623+
624+ // If chat was just created, wait 15 seconds before sending message
625+ if ( wasCreated ) {
626+ console . log ( `⏳ Admin chat was just created, waiting 15 seconds before sending message...` ) ;
627+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
628+ console . log ( `✅ 15-second delay completed for admin message` ) ;
629+ }
610630 console . log ( `✅ Admin chat found/created: ${ adminChat . id } ` ) ;
611631
612632 const activity = this . extractActivityFromMatch ( match ) ;
@@ -655,8 +675,18 @@ DreamSync
655675 const dreamsyncUser = await this . findDreamSyncUser ( ) ;
656676 if ( ! dreamsyncUser ) return ;
657677
658- const memberChat = await this . findOrCreateMutualChat ( originalMember . id ) ;
659- if ( ! memberChat ) return ;
678+ const memberChatResult = await this . findOrCreateMutualChat ( originalMember . id ) ;
679+ if ( ! memberChatResult . chat ) return ;
680+
681+ const memberChat = memberChatResult . chat ;
682+ const wasCreated = memberChatResult . wasCreated ;
683+
684+ // If chat was just created, wait 15 seconds before sending message
685+ if ( wasCreated ) {
686+ console . log ( `⏳ Member chat was just created, waiting 15 seconds before sending message...` ) ;
687+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
688+ console . log ( `✅ 15-second delay completed for member message` ) ;
689+ }
660690
661691 const activity = this . extractActivityFromMatch ( match ) ;
662692 const messageContent = `$$system-message$$
@@ -697,8 +727,18 @@ DreamSync
697727
698728 // Send notification to both users
699729 for ( const user of [ match . userA , match . userB ] ) {
700- const userChat = await this . findOrCreateMutualChat ( user . id ) ;
701- if ( ! userChat ) continue ;
730+ const userChatResult = await this . findOrCreateMutualChat ( user . id ) ;
731+ if ( ! userChatResult . chat ) continue ;
732+
733+ const userChat = userChatResult . chat ;
734+ const wasCreated = userChatResult . wasCreated ;
735+
736+ // If chat was just created, wait 15 seconds before sending message
737+ if ( wasCreated ) {
738+ console . log ( `⏳ User chat was just created, waiting 15 seconds before sending message...` ) ;
739+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
740+ console . log ( `✅ 15-second delay completed for user message` ) ;
741+ }
702742
703743 const messageContent = `$$system-message$$
704744
@@ -791,8 +831,17 @@ DreamSync
791831 const activity = this . extractActivityFromMatch ( match ) ;
792832
793833 // Send notification to admin with full details
794- const adminChat = await this . findOrCreateMutualChat ( adminUser . id ) ;
795- if ( adminChat ) {
834+ const adminChatResult = await this . findOrCreateMutualChat ( adminUser . id ) ;
835+ if ( adminChatResult . chat ) {
836+ const adminChat = adminChatResult . chat ;
837+ const wasCreated = adminChatResult . wasCreated ;
838+
839+ // If chat was just created, wait 15 seconds before sending message
840+ if ( wasCreated ) {
841+ console . log ( `⏳ Admin chat was just created, waiting 15 seconds before sending message...` ) ;
842+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
843+ console . log ( `✅ 15-second delay completed for admin message` ) ;
844+ }
796845 const adminMessageContent = `$$system-message$$
797846
798847👥 New member joined your ${ activity } group!
@@ -823,8 +872,17 @@ DreamSync
823872 }
824873
825874 // Send generic notification to new member
826- const memberChat = await this . findOrCreateMutualChat ( newMember . id ) ;
827- if ( memberChat ) {
875+ const memberChatResult = await this . findOrCreateMutualChat ( newMember . id ) ;
876+ if ( memberChatResult . chat ) {
877+ const memberChat = memberChatResult . chat ;
878+ const wasCreated = memberChatResult . wasCreated ;
879+
880+ // If chat was just created, wait 15 seconds before sending message
881+ if ( wasCreated ) {
882+ console . log ( `⏳ Member chat was just created, waiting 15 seconds before sending message...` ) ;
883+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
884+ console . log ( `✅ 15-second delay completed for member message` ) ;
885+ }
828886 const memberMessageContent = `$$system-message$$
829887
830888Welcome to the ${ activity } group!
@@ -1158,11 +1216,21 @@ DreamSync
11581216 return ;
11591217 }
11601218
1161- const userChat = await this . findOrCreateMutualChat ( userId ) ;
1162- if ( ! userChat ) {
1219+ const userChatResult = await this . findOrCreateMutualChat ( userId ) ;
1220+ if ( ! userChatResult . chat ) {
11631221 console . error ( "Could not find/create user chat for group join notification" ) ;
11641222 return ;
11651223 }
1224+
1225+ const userChat = userChatResult . chat ;
1226+ const wasCreated = userChatResult . wasCreated ;
1227+
1228+ // If chat was just created, wait 15 seconds before sending message
1229+ if ( wasCreated ) {
1230+ console . log ( `⏳ User chat was just created, waiting 15 seconds before sending message...` ) ;
1231+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
1232+ console . log ( `✅ 15-second delay completed for user message` ) ;
1233+ }
11661234
11671235 const activity = this . extractActivityFromMatch ( match ) ;
11681236 const messageContent = `$$system-message$$
@@ -1204,11 +1272,21 @@ DreamSync Team
12041272 return ;
12051273 }
12061274
1207- const userChat = await this . findOrCreateMutualChat ( userId ) ;
1208- if ( ! userChat ) {
1275+ const userChatResult = await this . findOrCreateMutualChat ( userId ) ;
1276+ if ( ! userChatResult . chat ) {
12091277 console . error ( "Could not find/create user chat for group created notification" ) ;
12101278 return ;
12111279 }
1280+
1281+ const userChat = userChatResult . chat ;
1282+ const wasCreated = userChatResult . wasCreated ;
1283+
1284+ // If chat was just created, wait 15 seconds before sending message
1285+ if ( wasCreated ) {
1286+ console . log ( `⏳ User chat was just created, waiting 15 seconds before sending message...` ) ;
1287+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
1288+ console . log ( `✅ 15-second delay completed for user message` ) ;
1289+ }
12121290
12131291 const activity = this . extractActivityFromMatch ( match ) ;
12141292 const messageContent = `$$system-message$$
@@ -1254,15 +1332,16 @@ DreamSync Team
12541332
12551333 /**
12561334 * Find or create a mutual chat between DreamSync and a user
1335+ * Returns both the chat and whether it was just created
12571336 */
1258- private async findOrCreateMutualChat ( userId : string ) : Promise < Group | null > {
1337+ private async findOrCreateMutualChat ( userId : string ) : Promise < { chat : Group | null ; wasCreated : boolean } > {
12591338 try {
12601339 console . log ( `🔍 ConsentService: Looking for mutual chat between DreamSync and user: ${ userId } ` ) ;
12611340
12621341 const dreamsyncUser = await this . findDreamSyncUser ( ) ;
12631342 if ( ! dreamsyncUser ) {
12641343 console . error ( "❌ ConsentService: DreamSync user not found for mutual chat creation" ) ;
1265- return null ;
1344+ return { chat : null , wasCreated : false } ;
12661345 }
12671346
12681347 console . log ( `👤 ConsentService: DreamSync user found: ${ dreamsyncUser . id } (${ dreamsyncUser . name || dreamsyncUser . ename } )` ) ;
@@ -1292,7 +1371,7 @@ DreamSync Team
12921371 if ( existingChat ) {
12931372 console . log ( `✅ ConsentService: Found existing mutual chat: ${ existingChat . id } ` ) ;
12941373 console . log ( `📋 ConsentService: Chat details: Name="${ existingChat . name } ", Private=${ existingChat . isPrivate } , Members=${ existingChat . members ?. length || 0 } ` ) ;
1295- return existingChat ;
1374+ return { chat : existingChat , wasCreated : false } ;
12961375 }
12971376
12981377 console . log ( `🆕 ConsentService: No existing mutual chat found, creating new one...` ) ;
@@ -1321,12 +1400,12 @@ DreamSync Team
13211400
13221401 console . log ( `✅ ConsentService: Created new mutual chat: ${ mutualChat . id } ` ) ;
13231402 console . log ( `📋 ConsentService: New chat details: Name="${ mutualChat . name } ", Private=${ mutualChat . isPrivate } , Members=${ mutualChat . members ?. length || 0 } ` ) ;
1324- return mutualChat ;
1403+ return { chat : mutualChat , wasCreated : true } ;
13251404
13261405 } catch ( error ) {
13271406 console . error ( "❌ ConsentService: Error finding or creating mutual chat:" , error ) ;
13281407 console . error ( "❌ ConsentService: Error details:" , ( error as Error ) . message ) ;
1329- return null ;
1408+ return { chat : null , wasCreated : false } ;
13301409 }
13311410 }
13321411
@@ -1392,11 +1471,21 @@ DreamSync Team
13921471 return ;
13931472 }
13941473
1395- const userChat = await this . findOrCreateMutualChat ( userId ) ;
1396- if ( ! userChat ) {
1474+ const userChatResult = await this . findOrCreateMutualChat ( userId ) ;
1475+ if ( ! userChatResult . chat ) {
13971476 console . error ( "❌ Could not find/create user chat for acknowledgment" ) ;
13981477 return ;
13991478 }
1479+
1480+ const userChat = userChatResult . chat ;
1481+ const wasCreated = userChatResult . wasCreated ;
1482+
1483+ // If chat was just created, wait 15 seconds before sending message
1484+ if ( wasCreated ) {
1485+ console . log ( `⏳ User chat was just created, waiting 15 seconds before sending message...` ) ;
1486+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
1487+ console . log ( `✅ 15-second delay completed for user message` ) ;
1488+ }
14001489
14011490 const activity = this . extractActivityFromMatch ( match ) ;
14021491 const messageContent = `$$system-message$$
0 commit comments