Skip to content

Commit

Permalink
feat: resending temporary pin to users
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Aug 6, 2021
1 parent dea8364 commit 51291a5
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 55 deletions.
88 changes: 83 additions & 5 deletions pkg/onboarding/presentation/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/onboarding/presentation/graph/profile.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,6 @@ extend type Mutation {
activateRole(roleID: ID!): RoleOutput!

deactivateRole(roleID: ID!): RoleOutput!

resendTemporaryPin(phoneNumber: String!): Boolean!
}
36 changes: 28 additions & 8 deletions pkg/onboarding/presentation/graph/profile.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package graph

import (
"context"
"fmt"
"time"

"github.com/savannahghi/enumutils"
Expand Down Expand Up @@ -681,6 +682,15 @@ func (r *mutationResolver) DeactivateRole(ctx context.Context, roleID string) (*
return role, err
}

func (r *mutationResolver) ResendTemporaryPin(ctx context.Context, phoneNumber string) (bool, error) {
startTime := time.Now()

success, err := r.interactor.UserPIN.ResendTemporaryPin(ctx, phoneNumber)
defer serverutils.RecordGraphqlResolverMetrics(ctx, startTime, "resendTemporaryPin", err)

return success, err
}

func (r *queryResolver) DummyQuery(ctx context.Context) (*bool, error) {
dummy := true
return &dummy, nil
Expand Down Expand Up @@ -836,14 +846,8 @@ func (r *queryResolver) FetchAgents(ctx context.Context) ([]*dto.Agent, error) {
return agents, err
}

func (r *queryResolver) FindAgentByPhone(ctx context.Context, phoneNumber *string) (*dto.Agent, error) {
startTime := time.Now()

agent, err := r.interactor.Agent.FindAgentByPhone(ctx, phoneNumber)

defer serverutils.RecordGraphqlResolverMetrics(ctx, startTime, "findAgentbyPhone", err)

return agent, err
func (r *queryResolver) FindAgentbyPhone(ctx context.Context, phoneNumber *string) (*dto.Agent, error) {
panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) FetchUserNavigationActions(ctx context.Context) (*profileutils.NavigationActions, error) {
Expand Down Expand Up @@ -914,3 +918,19 @@ func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }

// !!! WARNING !!!
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
// one last chance to move it out of harms way if you want. There are two reasons this happens:
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
// it when you're done.
// - You have helper methods in this file. Move them out to keep these resolver files clean.
func (r *queryResolver) FindAgentByPhone(ctx context.Context, phoneNumber *string) (*dto.Agent, error) {
startTime := time.Now()

agent, err := r.interactor.Agent.FindAgentByPhone(ctx, phoneNumber)

defer serverutils.RecordGraphqlResolverMetrics(ctx, startTime, "findAgentbyPhone", err)

return agent, err
}
42 changes: 0 additions & 42 deletions pkg/onboarding/usecases/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ type ProfileUseCase interface {
SwitchUserFlaggedFeatures(ctx context.Context, phoneNumber string) (*dto.OKResp, error)

FindUserByPhone(ctx context.Context, phoneNumber string) (*profileutils.UserProfile, error)

ResendTemporaryPin(ctx context.Context, phone string) (bool, error)
}

// ProfileUseCaseImpl represents usecase implementation object
Expand All @@ -193,7 +191,6 @@ type ProfileUseCaseImpl struct {
engagement engagement.ServiceEngagement
pubsub pubsubmessaging.ServicePubSub
crm crm.ServiceCrm
pin UserPINUseCases
}

// NewProfileUseCase returns a new a onboarding usecase
Expand All @@ -203,15 +200,13 @@ func NewProfileUseCase(
eng engagement.ServiceEngagement,
pubsub pubsubmessaging.ServicePubSub,
crm crm.ServiceCrm,
pin UserPINUseCases,
) ProfileUseCase {
return &ProfileUseCaseImpl{
onboardingRepository: r,
baseExt: ext,
engagement: eng,
pubsub: pubsub,
crm: crm,
pin: pin,
}
}

Expand Down Expand Up @@ -2100,40 +2095,3 @@ func (p *ProfileUseCaseImpl) GetNavigationActions(

return navActions, nil
}

//ResendTemporaryPin resends a temporary pin to user during sign up
func (p *ProfileUseCaseImpl) ResendTemporaryPin(ctx context.Context, phone string) (bool, error) {
ctx, span := tracer.Start(ctx, "ResendTemporaryPin")
defer span.End()
phoneNumber, err := p.baseExt.NormalizeMSISDN(phone)
if err != nil {
utils.RecordSpanError(span, err)
return false, exceptions.NormalizeMSISDNError(err)
}

profile, err := p.onboardingRepository.GetUserProfileByPrimaryPhoneNumber(
ctx,
*phoneNumber,
false,
)
if err != nil {
utils.RecordSpanError(span, err)
return false, err
}

otp, err := p.pin.SetUserTempPIN(ctx, profile.ID)
if err != nil {
utils.RecordSpanError(span, err)
return false, err
}

message := fmt.Sprintf(
"Please use this One Time PIN: %s to log onto Bewell with your phone number. You will be prompted to change the PIN on login. For enquiries call us on 0790360360",
otp,
)
if err := p.engagement.SendSMS(ctx, []string{*phoneNumber}, message); err != nil {
return false, fmt.Errorf("unable to send agent registration message: %w", err)
}

return true, nil
}
38 changes: 38 additions & 0 deletions pkg/onboarding/usecases/user_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UserPINUseCases interface {
ChangeUserPIN(ctx context.Context, phone string, pin string) (bool, error)
RequestPINReset(ctx context.Context, phone string, appID *string) (*profileutils.OtpResponse, error)
CheckHasPIN(ctx context.Context, profileID string) (bool, error)
ResendTemporaryPin(ctx context.Context, phone string) (bool, error)
}

// UserPinUseCaseImpl represents usecase implementation object
Expand Down Expand Up @@ -287,3 +288,40 @@ func (u *UserPinUseCaseImpl) SetUserTempPIN(ctx context.Context, profileID strin

return pin, nil
}

//ResendTemporaryPin resends a temporary pin to user during sign up
func (u *UserPinUseCaseImpl) ResendTemporaryPin(ctx context.Context, phone string) (bool, error) {
ctx, span := tracer.Start(ctx, "ResendTemporaryPin")
defer span.End()
phoneNumber, err := u.baseExt.NormalizeMSISDN(phone)
if err != nil {
utils.RecordSpanError(span, err)
return false, exceptions.NormalizeMSISDNError(err)
}

profile, err := u.onboardingRepository.GetUserProfileByPrimaryPhoneNumber(
ctx,
*phoneNumber,
false,
)
if err != nil {
utils.RecordSpanError(span, err)
return false, err
}

otp, err := u.SetUserTempPIN(ctx, profile.ID)
if err != nil {
utils.RecordSpanError(span, err)
return false, err
}

message := fmt.Sprintf(
"Please use this One Time PIN: %s to log onto Bewell with your phone number. You will be prompted to change the PIN on login. For enquiries call us on 0790360360",
otp,
)
if err := u.engagement.SendSMS(ctx, []string{*phoneNumber}, message); err != nil {
return false, fmt.Errorf("unable to send agent registration message: %w", err)
}

return true, nil
}
Loading

0 comments on commit 51291a5

Please sign in to comment.