diff --git a/pkg/onboarding/usecases/admin.go b/pkg/onboarding/usecases/admin.go index e39f6e8c..3367861a 100644 --- a/pkg/onboarding/usecases/admin.go +++ b/pkg/onboarding/usecases/admin.go @@ -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) @@ -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() @@ -147,7 +153,7 @@ 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) } @@ -155,34 +161,40 @@ func (a *AdminUseCaseImpl) RegisterAdmin(ctx context.Context, input dto.Register 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 @@ -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() @@ -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() @@ -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 diff --git a/pkg/onboarding/usecases/agent.go b/pkg/onboarding/usecases/agent.go index cc4bc702..6fd171e6 100644 --- a/pkg/onboarding/usecases/agent.go +++ b/pkg/onboarding/usecases/agent.go @@ -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) @@ -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) } @@ -181,9 +184,7 @@ 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 @@ -191,38 +192,40 @@ func (a *AgentUseCaseImpl) notifyNewAgent( } 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() @@ -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()