Skip to content

Commit

Permalink
fix: allow empty email in registration input (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Muchogoc authored and NYARAS committed Aug 18, 2021
1 parent 42ab045 commit ef28543
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
50 changes: 35 additions & 15 deletions pkg/onboarding/usecases/admin.go
Expand Up @@ -25,7 +25,10 @@ const (

// AdminUseCase represent the business logic required for management of admins
type AdminUseCase interface {
RegisterAdmin(ctx context.Context, input dto.RegisterAdminInput) (*profileutils.UserProfile, error)
RegisterAdmin(
ctx context.Context,
input dto.RegisterAdminInput,
) (*profileutils.UserProfile, error)
FetchAdmins(ctx context.Context) ([]*dto.Admin, error)
ActivateAdmin(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error)
DeactivateAdmin(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error)
Expand Down Expand Up @@ -56,7 +59,10 @@ func NewAdminUseCases(
}

// RegisterAdmin creates a new Admin in bewell
func (a *AdminUseCaseImpl) RegisterAdmin(ctx context.Context, input dto.RegisterAdminInput) (*profileutils.UserProfile, error) {
func (a *AdminUseCaseImpl) RegisterAdmin(
ctx context.Context,
input dto.RegisterAdminInput,
) (*profileutils.UserProfile, error) {
ctx, span := tracer.Start(ctx, "RegisterAdmin")
defer span.End()

Expand Down Expand Up @@ -147,42 +153,48 @@ func (a *AdminUseCaseImpl) RegisterAdmin(ctx context.Context, input dto.Register
return nil, err
}

if err := a.notifyNewAdmin(ctx, []string{input.Email}, []string{input.PhoneNumber}, *profile.UserBioData.FirstName, otp); err != nil {
if err := a.notifyNewAdmin(ctx, input.Email, input.PhoneNumber, *profile.UserBioData.FirstName, otp); err != nil {
utils.RecordSpanError(span, err)
return nil, fmt.Errorf("unable to send admin registration notifications: %w", err)
}

return profile, nil
}

func (a *AdminUseCaseImpl) notifyNewAdmin(ctx context.Context, emails []string, phoneNumbers []string, name, otp string) error {
func (a *AdminUseCaseImpl) notifyNewAdmin(
ctx context.Context,
email, phoneNumber, firstName, tempPIN string,
) error {
type pin struct {
Name string
Pin string
}

message := fmt.Sprintf("%sPlease use this One Time PIN: %s to log onto Bewell with your phone number. For enquiries call us on 0790360360", adminWelcomeMessage, otp)
if err := a.engagement.SendSMS(ctx, phoneNumbers, message); err != nil {
message := fmt.Sprintf(
"%sPlease use this One Time PIN: %s to log onto Bewell with your phone number. For enquiries call us on 0790360360",
adminWelcomeMessage,
tempPIN,
)
if err := a.engagement.SendSMS(ctx, []string{phoneNumber}, message); err != nil {
return fmt.Errorf("unable to send admin registration message: %w", err)
}

if len(emails) > 0 {
if email != "" {
t := template.Must(template.New("adminApprovalEmail").Parse(utils.AdminApprovalEmail))

buf := new(bytes.Buffer)

err := t.Execute(buf, pin{name, otp})
err := t.Execute(buf, pin{firstName, tempPIN})
if err != nil {
log.Fatalf("error while generating admin approval email template: %s", err)
}

text := buf.String()

for _, email := range emails {
if err := a.engagement.SendMail(ctx, email, text, adminWelcomeEmailSubject); err != nil {
return fmt.Errorf("unable to send admin registration email: %w", err)
}
if err := a.engagement.SendMail(ctx, email, text, adminWelcomeEmailSubject); err != nil {
return fmt.Errorf("unable to send admin registration email: %w", err)
}

}

return nil
Expand Down Expand Up @@ -230,7 +242,10 @@ func (a *AdminUseCaseImpl) FetchAdmins(ctx context.Context) ([]*dto.Admin, error
}

// ActivateAdmin activates/unsuspend the admin profile
func (a *AdminUseCaseImpl) ActivateAdmin(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error) {
func (a *AdminUseCaseImpl) ActivateAdmin(
ctx context.Context,
input dto.ProfileSuspensionInput,
) (bool, error) {
ctx, span := tracer.Start(ctx, "ActivateAdmin")
defer span.End()

Expand Down Expand Up @@ -266,7 +281,10 @@ func (a *AdminUseCaseImpl) ActivateAdmin(ctx context.Context, input dto.ProfileS
}

// DeactivateAdmin deactivates/suspends the admin profile
func (a *AdminUseCaseImpl) DeactivateAdmin(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error) {
func (a *AdminUseCaseImpl) DeactivateAdmin(
ctx context.Context,
input dto.ProfileSuspensionInput,
) (bool, error) {
ctx, span := tracer.Start(ctx, "DeactivateAdmin")
defer span.End()

Expand All @@ -283,7 +301,9 @@ func (a *AdminUseCaseImpl) DeactivateAdmin(ctx context.Context, input dto.Profil
}

if !allowed {
return false, fmt.Errorf("error, user do not have the permissions to deactivate an employee")
return false, fmt.Errorf(
"error, user do not have the permissions to deactivate an employee",
)
}

// Get admin profile using phoneNumber
Expand Down
38 changes: 22 additions & 16 deletions pkg/onboarding/usecases/agent.go
Expand Up @@ -25,7 +25,10 @@ const (

// AgentUseCase represent the business logic required for management of agents
type AgentUseCase interface {
RegisterAgent(ctx context.Context, input dto.RegisterAgentInput) (*profileutils.UserProfile, error)
RegisterAgent(
ctx context.Context,
input dto.RegisterAgentInput,
) (*profileutils.UserProfile, error)
ActivateAgent(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error)
DeactivateAgent(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error)
FetchAgents(ctx context.Context) ([]*dto.Agent, error)
Expand Down Expand Up @@ -171,7 +174,7 @@ func (a *AgentUseCaseImpl) RegisterAgent(
return nil, err
}

if err := a.notifyNewAgent(ctx, []string{input.Email}, []string{input.PhoneNumber}, *profile.UserBioData.FirstName, otp); err != nil {
if err := a.notifyNewAgent(ctx, input.Email, input.PhoneNumber, *profile.UserBioData.FirstName, otp); err != nil {
utils.RecordSpanError(span, err)
return nil, fmt.Errorf("unable to send agent registration notifications: %w", err)
}
Expand All @@ -181,48 +184,48 @@ func (a *AgentUseCaseImpl) RegisterAgent(

func (a *AgentUseCaseImpl) notifyNewAgent(
ctx context.Context,
emails []string,
phoneNumbers []string,
name, otp string,
email, phoneNumber, firstName, tempPIN string,
) error {
type pin struct {
Name string
Pin string
}

message := fmt.Sprintf(
"%sPlease 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",
"%sPlease use this One Time PIN: %s to log onto Bewell Pro with your phone number. You will be prompted to change the PIN on login. For enquiries call us on 0790360360",
agentWelcomeMessage,
otp,
tempPIN,
)
if err := a.engagement.SendSMS(ctx, phoneNumbers, message); err != nil {
if err := a.engagement.SendSMS(ctx, []string{phoneNumber}, message); err != nil {
return fmt.Errorf("unable to send agent registration message: %w", err)
}

if len(emails) > 0 {
if email != "" {
t := template.Must(template.New("agentApprovalEmail").Parse(utils.AgentApprovalEmail))

buf := new(bytes.Buffer)

err := t.Execute(buf, pin{name, otp})
err := t.Execute(buf, pin{firstName, tempPIN})
if err != nil {
log.Fatalf("error while generating agent approval email template: %s", err)
}

text := buf.String()

for _, email := range emails {
if err := a.engagement.SendMail(ctx, email, text, agentWelcomeEmailSubject); err != nil {
return fmt.Errorf("unable to send agent registration email: %w", err)
}
if err := a.engagement.SendMail(ctx, email, text, agentWelcomeEmailSubject); err != nil {
return fmt.Errorf("unable to send agent registration email: %w", err)
}

}

return nil
}

// ActivateAgent activates/unsuspend the agent profile
func (a *AgentUseCaseImpl) ActivateAgent(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error) {
func (a *AgentUseCaseImpl) ActivateAgent(
ctx context.Context,
input dto.ProfileSuspensionInput,
) (bool, error) {
a.checkPreconditions()
ctx, span := tracer.Start(ctx, "ActivateAgent")
defer span.End()
Expand All @@ -242,7 +245,10 @@ func (a *AgentUseCaseImpl) ActivateAgent(ctx context.Context, input dto.ProfileS
}

// DeactivateAgent deactivates/suspends the agent profile
func (a *AgentUseCaseImpl) DeactivateAgent(ctx context.Context, input dto.ProfileSuspensionInput) (bool, error) {
func (a *AgentUseCaseImpl) DeactivateAgent(
ctx context.Context,
input dto.ProfileSuspensionInput,
) (bool, error) {
a.checkPreconditions()
ctx, span := tracer.Start(ctx, "DeactivateAgent")
defer span.End()
Expand Down

0 comments on commit ef28543

Please sign in to comment.