@@ -587,10 +587,10 @@ class FollowUpService {
587587 const reply = this . _normalizeInboundText ( messageBody ) ;
588588
589589 const memberByPhone = await MembersModel . findOne ( {
590- phone : { $regex : cleanedPhone } ,
590+ phone : cleanedPhone ,
591591 } ) ;
592592
593- console . log ( `Member found by phone: ${ memberByPhone } ` ) ;
593+ console . log ( `Member found by phone: ${ memberByPhone ?. whatsappOptIn } ` ) ;
594594
595595 // // Global commands should work even when there is no active journey.
596596 if ( this . _isOptOut ( reply ) && memberByPhone ) {
@@ -634,26 +634,60 @@ class FollowUpService {
634634 ) ;
635635 return { action : 'help' , journey : null } ;
636636 }
637-
638637
639- const journey = await FollowUpJourney . findOne ( {
640- phone : { $regex : cleanedPhone } ,
641- status : { $in : [ 'active' , 'escalated' ] } ,
642- } ) . populate ( 'memberId' ) ;
643638
644- console . log ( `Active journey found: ${ ! ! journey } ` ) ;
645- if ( ! journey ) {
646- if ( memberByPhone ) {
647- const absentHandled = await this . _handleAbsentReminderReply (
648- memberByPhone ,
649- cleanedPhone ,
650- reply
651- ) ;
652- if ( absentHandled ) return { action : absentHandled . action , journey : null } ;
653- }
639+ // const journey = await FollowUpJourney.findOne({
640+ // phone: { $regex: cleanedPhone },
641+ // status: { $in: ['active', 'escalated'] },
642+ // }).populate('memberId');
643+
644+ if ( ! memberByPhone ) return null ;
645+
646+ const lastOutbound = await WhatsappActivity . findOne ( {
647+ memberId : memberByPhone . _id ,
648+ direction : 'outbound' ,
649+ conversationStage : 'awaiting_reply'
650+ } ) . sort ( { createdAt : - 1 } ) ;
651+
652+ if ( ! lastOutbound ) {
654653 return null ;
655654 }
656655
656+ // Handle absent reminder outside journey
657+ if ( lastOutbound . messageType === 'absent_reminder' ) {
658+ const result = await this . _handleAbsentReminderReply (
659+ memberByPhone ,
660+ cleanedPhone ,
661+ reply
662+ ) ;
663+
664+ lastOutbound . conversationStage = 'replied' ;
665+ await lastOutbound . save ( ) ;
666+
667+ return result ? { action : result . action } : null ;
668+ }
669+
670+ // console.log(`Active journey found: ${!!journey}`);
671+ // if (!journey) {
672+ // if (memberByPhone) {
673+ // const absentHandled = await this._handleAbsentReminderReply(
674+ // memberByPhone,
675+ // cleanedPhone,
676+ // reply
677+ // );
678+ // if (absentHandled) return { action: absentHandled.action, journey: null };
679+ // }
680+ // return null;
681+ // }
682+
683+ // For follow-up stages
684+ const journey = await FollowUpJourney . findOne ( {
685+ memberId : memberByPhone . _id ,
686+ status : { $in : [ 'active' , 'escalated' ] }
687+ } ) . populate ( 'memberId' ) ;
688+
689+ if ( ! journey ) return null ;
690+
657691 const member = journey . memberId ;
658692 const firstName = member ?. firstName || 'Friend' ;
659693 const flowStages = await this . getActiveFlowStages ( ) ;
0 commit comments