Skip to content

Commit

Permalink
feat: add searching admin by phone
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Aug 22, 2021
1 parent 17f48de commit dfb0838
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -118,8 +118,10 @@ github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcju
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -363,7 +365,9 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/savannahghi/converterandformatter v0.0.3/go.mod h1:0o7yieYU10WabPqKuqj+5QL52eTL1eGElxjb+A68bbA=
Expand Down Expand Up @@ -412,6 +416,7 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20180121065927-ffb13db8def0/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
Expand Down Expand Up @@ -445,6 +450,7 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand Down
81 changes: 81 additions & 0 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
Expand Up @@ -37,6 +37,8 @@ extend type Query {

findAgentbyPhone(phoneNumber: String): Agent

findAdminByNameOrPhone(nameOrPhone: String): [Admin]

fetchUserNavigationActions: NavigationActions

listMicroservices: [Microservice!]!
Expand Down
10 changes: 10 additions & 0 deletions pkg/onboarding/presentation/graph/profile.resolvers.go
Expand Up @@ -855,6 +855,16 @@ func (r *queryResolver) FindAgentbyPhone(ctx context.Context, phoneNumber *strin
return agent, err
}

func (r *queryResolver) FindAdminByNameOrPhone(ctx context.Context, nameOrPhone *string) ([]*dto.Admin, error) {
startTime := time.Now()

admins, err := r.interactor.Admin.FindAdminByNameOrPhone(ctx, nameOrPhone)

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

return admins, err
}

func (r *queryResolver) FetchUserNavigationActions(ctx context.Context) (*profileutils.NavigationActions, error) {
startTime := time.Now()

Expand Down
42 changes: 42 additions & 0 deletions pkg/onboarding/usecases/admin.go
Expand Up @@ -3,6 +3,7 @@ package usecases
import (
"context"
"fmt"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -30,6 +31,7 @@ type AdminUseCase interface {
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)
FindAdminByNameOrPhone(ctx context.Context, nameOrPhone *string) ([]*dto.Admin, error)
}

// AdminUseCaseImpl represents usecase implementation object
Expand Down Expand Up @@ -334,3 +336,43 @@ func (a *AdminUseCaseImpl) DeactivateAdmin(
}
return true, nil
}

// FindAdminByNameOrPhone is used to find an Admin using their phone number
func (a *AdminUseCaseImpl) FindAdminByNameOrPhone(
ctx context.Context,
nameOrPhone *string,
) ([]*dto.Admin, error) {
ctx, span := tracer.Start(ctx, "FindAdminByNameOrPhone")
defer span.End()

profiles, err := a.repo.ListUserProfiles(ctx, profileutils.RoleTypeEmployee)
if err != nil {
utils.RecordSpanError(span, err)
return nil, exceptions.UserNotFoundError(err)
}

admins := []*dto.Admin{}

for _, profile := range profiles {

fullName := strings.ToLower(fmt.Sprintf("%v %v", *profile.UserBioData.FirstName, *profile.UserBioData.LastName))
phoneNumber := profile.PrimaryPhone

if strings.Contains(*phoneNumber, *nameOrPhone) || strings.Contains(fullName, strings.ToLower(*nameOrPhone)) {
admin := dto.Admin{
ID: profile.ID,
PhotoUploadID: profile.PhotoUploadID,
UserBioData: profile.UserBioData,
PrimaryPhone: *profile.PrimaryPhone,
PrimaryEmailAddress: profile.PrimaryEmailAddress,
SecondaryPhoneNumbers: profile.SecondaryPhoneNumbers,
SecondaryEmailAddresses: profile.SecondaryEmailAddresses,
TermsAccepted: profile.TermsAccepted,
Suspended: profile.Suspended,
}
admins = append(admins, &admin)
}
}

return admins, nil
}
93 changes: 93 additions & 0 deletions pkg/onboarding/usecases/admin_unit_test.go
Expand Up @@ -886,3 +886,96 @@ func TestAdminUseCaseImpl_DeactivateAdmin(t *testing.T) {
})
}
}

func TestAdminUseCaseImpl_FindAdminByNameOrPhone(t *testing.T) {
ctx := context.Background()

i, err := InitializeFakeOnboardingInteractor()
if err != nil {
t.Errorf("failed to fake initialize onboarding interactor: %v",
err,
)
return
}

nameOrPhone := "Test"
fName := "Test"
lName := "User"

type args struct {
ctx context.Context
nameOrPhone *string
}
tests := []struct {
name string
args args
want int
wantErr bool
}{
{
name: "sad: unable get user profiles",
args: args{ctx: ctx, nameOrPhone: &nameOrPhone},
want: 0,
wantErr: true,
},
{
name: "sad: did not get any user",
args: args{ctx: ctx, nameOrPhone: &nameOrPhone},
want: 0,
wantErr: false,
},
{
name: "happy: got user profiles",
args: args{ctx: ctx, nameOrPhone: &nameOrPhone},
want: 1,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "sad: unable get user profiles" {
fakeRepo.ListUserProfilesFn = func(ctx context.Context, role profileutils.RoleType) ([]*profileutils.UserProfile, error) {
return nil, fmt.Errorf("error unable to get user profile by phone")
}
}

if tt.name == "sad: did not get any user" {
fakeRepo.ListUserProfilesFn = func(ctx context.Context, role profileutils.RoleType) ([]*profileutils.UserProfile, error) {
return []*profileutils.UserProfile{}, nil
}
}

if tt.name == "happy: got user profiles" {
fakeRepo.ListUserProfilesFn = func(ctx context.Context, role profileutils.RoleType) ([]*profileutils.UserProfile, error) {
phone := interserviceclient.TestUserPhoneNumber
profile1 := profileutils.UserProfile{
UserBioData: profileutils.BioData{
FirstName: &fName,
LastName: &lName,
},
PrimaryPhone: &phone,
}
profile2 := profileutils.UserProfile{
UserBioData: profileutils.BioData{
FirstName: &lName,
LastName: &lName,
},
PrimaryPhone: &phone,
}
return []*profileutils.UserProfile{
&profile1,
&profile2,
}, nil
}
}
got, err := i.Admin.FindAdminByNameOrPhone(tt.args.ctx, tt.args.nameOrPhone)
if (err != nil) != tt.wantErr {
t.Errorf("AdminUseCaseImpl.FindAdminByNameOrPhone() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(len(got), tt.want) {
t.Errorf("AdminUseCaseImpl.FindAdminByNameOrPhone() = %v, want %v", len(got), tt.want)
}
})
}
}

0 comments on commit dfb0838

Please sign in to comment.