Skip to content

Commit

Permalink
chore: update phonenumber and emails in the CRM
Browse files Browse the repository at this point in the history
This change ensures that when a user updates their primary phone or
email, the change will propagate all the way to the crm

Signed-off-by: Salaton <salaton@healthcloud.co.ke>
  • Loading branch information
Salaton authored and NYARAS committed Jul 29, 2021
1 parent 9d4e456 commit 414ae70
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/vektah/gqlparser/v2 v2.1.0
gitlab.slade360emr.com/go/apiclient v0.0.13
gitlab.slade360emr.com/go/apiclient v0.0.14
gitlab.slade360emr.com/go/commontools/accounting v0.0.0-20210726041609-f5e1334c2640
gitlab.slade360emr.com/go/commontools/crm v0.0.0-20210726041609-f5e1334c2640
go.opencensus.io v0.23.0
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ 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 @@ -365,9 +363,7 @@ 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 @@ -414,7 +410,6 @@ 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 @@ -448,7 +443,6 @@ 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 Expand Up @@ -478,8 +472,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
gitlab.slade360emr.com/go/apiclient v0.0.8/go.mod h1:il6dRia5Er8nGEwYCAYmXqqtT7vExTBgou1hu4UsDK8=
gitlab.slade360emr.com/go/apiclient v0.0.13 h1:aeHDH97qPAlejW3Z0vY7iJANAWGugV+yXgFQAcEF34w=
gitlab.slade360emr.com/go/apiclient v0.0.13/go.mod h1:Mf8poMYJMkJVOiR1LFR9JJJbm9oeB564NA8CzkjdGCI=
gitlab.slade360emr.com/go/apiclient v0.0.14 h1:ynzNeTIgEH28yhd1KzL1CeDoqiNX9W7/GmbywHRKDiE=
gitlab.slade360emr.com/go/apiclient v0.0.14/go.mod h1:Mf8poMYJMkJVOiR1LFR9JJJbm9oeB564NA8CzkjdGCI=
gitlab.slade360emr.com/go/base v0.0.157/go.mod h1:2I0e87cNOQj2hT42jHER4MzXyW+kgGsKFoCFDvG99ro=
gitlab.slade360emr.com/go/commontools/accounting v0.0.0-20210726041609-f5e1334c2640 h1:9RlBjoncH+KV7yH7P3wjb6ZMuTe8yU5lN7v/eoEZRxM=
gitlab.slade360emr.com/go/commontools/accounting v0.0.0-20210726041609-f5e1334c2640/go.mod h1:JSX56SefYIg9rUZe97JxK7tiBoe1v6QfPOEcvfqw/Oo=
Expand Down
32 changes: 32 additions & 0 deletions pkg/onboarding/usecases/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ func (p *ProfileUseCaseImpl) UpdatePrimaryPhoneNumber(
utils.RecordSpanError(span, err)
return err
}

contact, err := p.crm.GetContactByPhone(ctx, *profile.PrimaryPhone)
if err != nil {
return fmt.Errorf("failed to get contact %s: %w", *profile.PrimaryPhone, err)
}
if contact == nil {
return nil
}

contact.Properties.Phone = phone

if err = p.pubsub.NotifyUpdateContact(ctx, *contact); err != nil {
utils.RecordSpanError(span, err)
log.Printf("failed to publish to crm.contact.update topic: %v", err)
}

// check if number to be set as primary exists in the list of secondary phones
index, exists := utils.FindItem(secondaryPhones, *phoneNumber)
if exists {
Expand Down Expand Up @@ -362,6 +378,22 @@ func (p *ProfileUseCaseImpl) UpdatePrimaryEmailAddress(
return err
}

// After updating the primary email, update it on the CRM too
contact, err := p.crm.GetContactByPhone(ctx, *profile.PrimaryPhone)
if err != nil {
return fmt.Errorf("failed to get contact %s: %w", *profile.PrimaryPhone, err)
}
if contact == nil {
return nil
}

contact.Properties.Email = emailAddress

if err = p.pubsub.NotifyUpdateContact(ctx, *contact); err != nil {
utils.RecordSpanError(span, err)
log.Printf("failed to publish to crm.contact.update topic: %v", err)
}

previousPrimaryEmail := profile.PrimaryEmailAddress
secondaryEmails := profile.SecondaryEmailAddresses

Expand Down
24 changes: 24 additions & 0 deletions pkg/onboarding/usecases/profile_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ func TestProfileUseCaseImpl_UpdatePrimaryEmailAddress(t *testing.T) {
return
}
primaryEmail := "me@gmail.com"
primaryPhone := "0711223344"

type args struct {
ctx context.Context
Expand Down Expand Up @@ -525,11 +526,17 @@ func TestProfileUseCaseImpl_UpdatePrimaryEmailAddress(t *testing.T) {
return &profileutils.UserProfile{
ID: "f4f39af7-5b64-4c2f-91bd-42b3af315a4e",
PrimaryEmailAddress: &primaryEmail,
PrimaryPhone: &primaryPhone,
}, nil
}
fakeRepo.UpdatePrimaryEmailAddressFn = func(ctx context.Context, id string, emailAddress string) error {
return nil
}

fakePubSub.NotifyUpdateContactFn = func(ctx context.Context, contact CRMDomain.CRMContact) error {
return nil
}

fakeRepo.UpdateSecondaryEmailAddressesFn = func(ctx context.Context, id string, emailAddresses []string) error {
return nil
}
Expand All @@ -547,6 +554,7 @@ func TestProfileUseCaseImpl_UpdatePrimaryEmailAddress(t *testing.T) {
return &profileutils.UserProfile{
ID: "f4f39af7-5b64-4c2f-91bd-42b3af315a4e",
PrimaryEmailAddress: &primaryEmail,
PrimaryPhone: &primaryPhone,
}, nil
}
fakeRepo.UpdatePrimaryEmailAddressFn = func(ctx context.Context, id string, emailAddress string) error {
Expand All @@ -566,6 +574,7 @@ func TestProfileUseCaseImpl_UpdatePrimaryEmailAddress(t *testing.T) {
return &profileutils.UserProfile{
ID: "f4f39af7-5b64-4c2f-91bd-42b3af315a4e",
PrimaryEmailAddress: &primaryEmail,
PrimaryPhone: &primaryPhone,
SecondaryEmailAddresses: []string{
"", "lulu@gmail.com",
},
Expand All @@ -574,6 +583,9 @@ func TestProfileUseCaseImpl_UpdatePrimaryEmailAddress(t *testing.T) {
fakeRepo.UpdatePrimaryEmailAddressFn = func(ctx context.Context, id string, emailAddress string) error {
return nil
}
fakePubSub.NotifyUpdateContactFn = func(ctx context.Context, contact CRMDomain.CRMContact) error {
return nil
}
fakeRepo.UpdateSecondaryEmailAddressesFn = func(ctx context.Context, id string, emailAddresses []string) error {
return fmt.Errorf("unable to update secondary email")
}
Expand Down Expand Up @@ -2177,6 +2189,10 @@ func TestProfileUseCaseImpl_UpdatePrimaryPhoneNumber(t *testing.T) {
return nil
}

fakePubSub.NotifyUpdateContactFn = func(ctx context.Context, contact CRMDomain.CRMContact) error {
return nil
}

fakeRepo.UpdateSecondaryPhoneNumbersFn = func(ctx context.Context, id string, phoneNumbers []string) error {
return nil
}
Expand Down Expand Up @@ -2210,6 +2226,10 @@ func TestProfileUseCaseImpl_UpdatePrimaryPhoneNumber(t *testing.T) {
return nil
}

fakePubSub.NotifyUpdateContactFn = func(ctx context.Context, contact CRMDomain.CRMContact) error {
return nil
}

fakeRepo.UpdateSecondaryPhoneNumbersFn = func(ctx context.Context, id string, phoneNumbers []string) error {
return nil
}
Expand Down Expand Up @@ -2276,6 +2296,10 @@ func TestProfileUseCaseImpl_UpdatePrimaryPhoneNumber(t *testing.T) {
return nil
}

fakePubSub.NotifyUpdateContactFn = func(ctx context.Context, contact CRMDomain.CRMContact) error {
return nil
}

fakeRepo.UpdateSecondaryPhoneNumbersFn = func(ctx context.Context, id string, phoneNumbers []string) error {
return fmt.Errorf("unable to update secondary phonenumber")
}
Expand Down

0 comments on commit 414ae70

Please sign in to comment.