Skip to content

Commit 09a1e44

Browse files
committed
switched normalize phone to resolvereal phone7
1 parent 53d36ee commit 09a1e44

File tree

1 file changed

+92
-77
lines changed

1 file changed

+92
-77
lines changed

services/followUpService.js

Lines changed: 92 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class FollowUpService {
233233
direction: 'outbound',
234234
messageType: 'manual',
235235
content: responseMessage,
236-
conversationStage: 'awaiting_reply',
236+
conversationStage: 'none',
237237
status: sendResult.success ? 'sent' : 'failed',
238238
wahaMessageId: sendResult.data?.messageId || null,
239239
errorDetails: sendResult.success ? null : JSON.stringify(sendResult.error),
@@ -646,11 +646,11 @@ class FollowUpService {
646646
// status: { $in: ['active', 'escalated'] },
647647
// }).populate('memberId');
648648

649-
650649
const lastOutbound = await WhatsappActivity.findOne({
651650
memberId: memberByPhone._id,
652651
direction: 'outbound',
653-
conversationStage: 'awaiting_reply'
652+
conversationStage: 'awaiting_reply',
653+
messageType: { $in: ['absent_reminder', 'follow_up'] }
654654
}).sort({ createdAt: -1 });
655655

656656
if (!lastOutbound) {
@@ -662,24 +662,24 @@ class FollowUpService {
662662
// Handle absent reminder outside journey
663663
if (lastOutbound.messageType === 'absent_reminder') {
664664

665-
return
666-
// const result = await this._handleAbsentReminderReply(
667-
// memberByPhone,
668-
// cleanedPhone,
669-
// reply
670-
// );
671665

672-
// await WhatsappActivity.findOneAndUpdate(
673-
// {
674-
// _id: lastOutbound._id,
675-
// conversationStage: 'awaiting_reply'
676-
// },
677-
// {
678-
// $set: { conversationStage: 'completed' }
679-
// }
680-
// );
666+
const result = await this._handleAbsentReminderReply(
667+
memberByPhone,
668+
cleanedPhone,
669+
reply
670+
);
681671

682-
// return result ? { action: result.action } : null;
672+
await WhatsappActivity.findOneAndUpdate(
673+
{
674+
_id: lastOutbound._id,
675+
conversationStage: 'awaiting_reply'
676+
},
677+
{
678+
$set: { conversationStage: 'completed' }
679+
}
680+
);
681+
682+
return result ? { action: result.action } : null;
683683
}
684684

685685
// console.log(`Active journey found: ${!!journey}`);
@@ -695,76 +695,91 @@ class FollowUpService {
695695
// return null;
696696
// }
697697

698-
// For follow-up stages
699-
const journey = await FollowUpJourney.findOne({
700-
memberId: memberByPhone._id,
701-
status: { $in: ['active', 'escalated'] }
702-
}).populate('memberId');
703-
704-
if (!journey) return null;
698+
if (lastOutbound.messageType === 'follow_up' && lastOutbound.followUpStage !== undefined) {
705699

706-
const member = journey.memberId;
707-
const firstName = member?.firstName || 'Friend';
708-
const flowStages = await this.getActiveFlowStages();
709-
const stageConfig = this._findStageConfig(flowStages, journey.currentStage);
700+
// For follow-up stages
701+
const journey = await FollowUpJourney.findOne({
702+
memberId: memberByPhone._id,
703+
status: { $in: ['active', 'escalated'] }
704+
}).populate('memberId');
710705

711-
await WhatsappActivity.create({
712-
memberId: member?._id,
713-
phone: cleanedPhone,
714-
direction: 'inbound',
715-
messageType: 'reply',
716-
content: reply,
717-
followUpStage: journey.currentStage,
718-
conversationStage: 'awaiting_reply',
719-
status: 'read',
720-
});
706+
if (!journey) return null;
721707

722-
let action = 'unknown';
723-
const configuredOption = this._findConfiguredResponseOption(
724-
reply,
725-
stageConfig?.responseOptions || []
726-
);
708+
const member = journey.memberId;
709+
const firstName = member?.firstName || 'Friend';
710+
const flowStages = await this.getActiveFlowStages();
711+
const stageConfig = this._findStageConfig(flowStages, journey.currentStage);
727712

728-
if (configuredOption) {
729-
action = await this._applyConfiguredResponseOption({
730-
option: configuredOption,
731-
journey,
732-
member,
713+
await WhatsappActivity.create({
714+
memberId: member?._id,
733715
phone: cleanedPhone,
716+
direction: 'inbound',
717+
messageType: 'reply',
718+
content: reply,
719+
followUpStage: journey.currentStage,
720+
conversationStage: 'awaiting_reply',
721+
status: 'read',
734722
});
735-
} else {
736-
const option = this._detectOption(reply, journey.currentStage);
737-
if (option === 1) action = await this._handleOption1(journey, firstName, cleanedPhone);
738-
else if (option === 2) action = await this._handleOption2(journey, firstName, cleanedPhone);
739-
else if (option === 3) action = await this._handleOption3(journey, firstName, cleanedPhone);
740-
else if (
741-
journey.currentStage === 2 ||
742-
member?.whatsappConversationStage === 'prayer_requested'
743-
) {
744-
await this._handlePrayerRequest(member, cleanedPhone, reply);
745-
action = 'prayer_submitted';
723+
724+
let action = 'unknown';
725+
const configuredOption = this._findConfiguredResponseOption(
726+
reply,
727+
stageConfig?.responseOptions || []
728+
);
729+
730+
if (configuredOption) {
731+
action = await this._applyConfiguredResponseOption({
732+
option: configuredOption,
733+
journey,
734+
member,
735+
phone: cleanedPhone,
736+
});
746737
} else {
747-
action = 'free_text';
738+
const option = this._detectOption(reply, journey.currentStage);
739+
if (option === 1) action = await this._handleOption1(journey, firstName, cleanedPhone);
740+
else if (option === 2) action = await this._handleOption2(journey, firstName, cleanedPhone);
741+
else if (option === 3) action = await this._handleOption3(journey, firstName, cleanedPhone);
742+
else if (
743+
journey.currentStage === 2 ||
744+
member?.whatsappConversationStage === 'prayer_requested'
745+
) {
746+
await this._handlePrayerRequest(member, cleanedPhone, reply);
747+
action = 'prayer_submitted';
748+
} else {
749+
action = 'free_text';
750+
}
748751
}
749-
}
750752

751-
journey.replies.push({
752-
content: reply,
753-
receivedAt: new Date(),
754-
stage: journey.currentStage,
755-
action,
756-
});
757-
journey.engagementScore = this._calculateEngagement(journey);
758-
await journey.save();
753+
journey.replies.push({
754+
content: reply,
755+
receivedAt: new Date(),
756+
stage: journey.currentStage,
757+
action,
758+
});
759+
journey.engagementScore = this._calculateEngagement(journey);
760+
await journey.save();
759761

760-
if (member) {
761-
member.lastWhatsappReply = new Date();
762-
member.totalReplies = (member.totalReplies || 0) + 1;
763-
member.whatsappEngagementStatus = 'active';
764-
await member.save();
762+
if (member) {
763+
member.lastWhatsappReply = new Date();
764+
member.totalReplies = (member.totalReplies || 0) + 1;
765+
member.whatsappEngagementStatus = 'active';
766+
await member.save();
767+
}
768+
769+
await WhatsappActivity.findOneAndUpdate(
770+
{
771+
_id: lastOutbound._id,
772+
conversationStage: 'awaiting_reply'
773+
},
774+
{
775+
$set: { conversationStage: 'completed' }
776+
}
777+
);
778+
return { action, journey };
765779
}
766780

767-
return { action, journey };
781+
return null;
782+
768783
}
769784

770785
async _handleOption1(journey, firstName, phone) {

0 commit comments

Comments
 (0)