Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
- Loading branch information
1 parent
1ff99b7
commit b8a397a
Showing
15 changed files
with
241 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package usecases | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/savannahghi/onboarding/pkg/onboarding/application/dto" | ||
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension" | ||
"github.com/savannahghi/onboarding/pkg/onboarding/application/utils" | ||
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/crm" | ||
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/engagement" | ||
pubsubmessaging "github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/pubsub" | ||
"github.com/savannahghi/onboarding/pkg/onboarding/repository" | ||
log "github.com/sirupsen/logrus" | ||
"gitlab.slade360emr.com/go/commontools/crm/pkg/domain" | ||
) | ||
|
||
const ( | ||
// The right copy of reply will be availed later | ||
shortcodeReplyMessage = "We have received your request and one of our representatives will reach out to you. Thanks you.\n\nDid you know you can now view your medical cover and benefits on Be.Well. To get started, Download Now from https://bwl.mobi/1bvf" | ||
) | ||
|
||
// SMSUsecase represent the logic involved in processing SMSs from shortcode | ||
type SMSUsecase interface { | ||
CreateSMSData(ctx context.Context, input *dto.AfricasTalkingMessage) error | ||
} | ||
|
||
//SMSImpl represents usecase implemention object | ||
type SMSImpl struct { | ||
onboardingRepository repository.OnboardingRepository | ||
baseExt extension.BaseExtension | ||
engagement engagement.ServiceEngagement | ||
pubSub pubsubmessaging.ServicePubSub | ||
hubspotCRM crm.ServiceCrm | ||
} | ||
|
||
// NewSMSUsecase returns a new SMS usecase | ||
func NewSMSUsecase( | ||
r repository.OnboardingRepository, | ||
ext extension.BaseExtension, | ||
engage engagement.ServiceEngagement, | ||
ps pubsubmessaging.ServicePubSub, | ||
crm crm.ServiceCrm, | ||
|
||
) SMSUsecase { | ||
return &SMSImpl{ | ||
onboardingRepository: r, | ||
baseExt: ext, | ||
engagement: engage, | ||
pubSub: ps, | ||
hubspotCRM: crm, | ||
} | ||
} | ||
|
||
// CreateSMSData saves incoming shortcode messages, replies to the sender and creates hubspot contact | ||
func (s *SMSImpl) CreateSMSData(ctx context.Context, input *dto.AfricasTalkingMessage) error { | ||
ctx, span := tracer.Start(ctx, "CreateSMSData") | ||
defer span.End() | ||
|
||
validatedInput, err := utils.ValidateAficasTalkingSMSData(input) | ||
if err != nil { | ||
utils.RecordSpanError(span, err) | ||
return err | ||
} | ||
|
||
// Checking if the shortcode SMS sender has a profile | ||
profile, err := s.onboardingRepository.GetUserProfileByPhoneNumber(ctx, validatedInput.From, false) | ||
if err != nil { | ||
utils.RecordSpanError(span, err) | ||
//should not panic when the user profile is not found | ||
log.Errorf("an error occurred: %v", err) | ||
} | ||
|
||
if profile != nil { | ||
to := validatedInput.From | ||
message := shortcodeReplyMessage | ||
|
||
supportEmailPayload := &dto.EmailNotificationPayload{ | ||
SubjectTitle: validatedInput.Text, | ||
EmailBody: validatedInput.Text, | ||
PrimaryPhone: validatedInput.From, | ||
BeWellUser: domain.GeneralOptionTypeYes, | ||
Time: validatedInput.Date, | ||
} | ||
|
||
return s.replyAndNotifyAdmin(ctx, to, message, validatedInput, supportEmailPayload) | ||
} | ||
|
||
to := validatedInput.From | ||
message := shortcodeReplyMessage | ||
|
||
supportEmailPayload := &dto.EmailNotificationPayload{ | ||
SubjectTitle: validatedInput.Text, | ||
EmailBody: validatedInput.Text, | ||
PrimaryPhone: validatedInput.From, | ||
BeWellUser: domain.GeneralOptionTypeNo, | ||
Time: validatedInput.Date, | ||
} | ||
|
||
err = s.replyAndNotifyAdmin(ctx, to, message, validatedInput, supportEmailPayload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//Create CRM contact stub for the shortcode sms sender | ||
contact := &domain.CRMContact{ | ||
Properties: domain.ContactProperties{ | ||
Phone: validatedInput.From, | ||
Gender: string(domain.GeneralOptionTypeNotGiven), | ||
FirstChannelOfContact: domain.ChannelOfContactShortcode, | ||
BeWellEnrolled: domain.GeneralOptionTypeNo, | ||
OptOut: domain.GeneralOptionTypeNo, | ||
}, | ||
} | ||
|
||
err = s.pubSub.NotifyCreateContact(ctx, *contact) | ||
if err != nil { | ||
utils.RecordSpanError(span, err) | ||
log.Printf("failed to publish to crm.contact.create topic: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *SMSImpl) replyAndNotifyAdmin( | ||
ctx context.Context, | ||
to string, | ||
message string, | ||
validatedInput *dto.AfricasTalkingMessage, | ||
supportEmailPayload *dto.EmailNotificationPayload) error { | ||
ctx, span := tracer.Start(ctx, "replyAndNotifyAdmin") | ||
defer span.End() | ||
|
||
err := s.engagement.SendSMS(ctx, []string{to}, message) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while sending SMS: %v", err) | ||
} | ||
|
||
err = s.engagement.NotifyAdmins(ctx, *supportEmailPayload) | ||
if err != nil { | ||
return fmt.Errorf("an error occurred while notifying admins: %v", err) | ||
} | ||
|
||
err = s.onboardingRepository.PersistIncomingSMSData(ctx, validatedInput) | ||
if err != nil { | ||
utils.RecordSpanError(span, err) | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.