Skip to content

Commit

Permalink
refactor purge user (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
KathurimaKimathi committed Aug 18, 2021
1 parent 51da0e1 commit c67f70a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
87 changes: 47 additions & 40 deletions pkg/onboarding/infrastructure/database/fb/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2809,22 +2809,25 @@ func (fr *Repository) PurgeUserByPhoneNumber(ctx context.Context, phone string)
pin, err := fr.GetPINByProfileID(ctx, profile.ID)
if err != nil {
utils.RecordSpanError(span, err)
return exceptions.InternalServerError(err)
}

query := &GetAllQuery{
CollectionName: fr.GetPINsCollectionName(),
FieldName: "id",
Value: pin.ID,
Operator: "==",
// Should not panic but allow for deletion of the profile
log.Printf("failed to get a user pin %v", err)
}
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
// Remove user profile with or without PIN
if pin != nil {
query := &GetAllQuery{
CollectionName: fr.GetPINsCollectionName(),
ID: docs[0].Ref.ID,
FieldName: "id",
Value: pin.ID,
Operator: "==",
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
CollectionName: fr.GetPINsCollectionName(),
ID: docs[0].Ref.ID,
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
}
}
}
// delete user supplier profile
Expand All @@ -2837,25 +2840,27 @@ func (fr *Repository) PurgeUserByPhoneNumber(ctx context.Context, phone string)
if err != nil {
utils.RecordSpanError(span, err)
} else {
err = fr.RemoveKYCProcessingRequest(ctx, supplier.ID)
if err != nil {
utils.RecordSpanError(span, err)
log.Printf("KYC request information was not removed %v", err)
}
if supplier != nil {
err = fr.RemoveKYCProcessingRequest(ctx, supplier.ID)
if err != nil {
utils.RecordSpanError(span, err)
log.Printf("KYC request information was not removed %v", err)
}

query := &GetAllQuery{
CollectionName: fr.GetSupplierProfileCollectionName(),
FieldName: "id",
Value: supplier.ID,
Operator: "==",
}
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
query := &GetAllQuery{
CollectionName: fr.GetSupplierProfileCollectionName(),
ID: docs[0].Ref.ID,
FieldName: "id",
Value: supplier.ID,
Operator: "==",
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
CollectionName: fr.GetSupplierProfileCollectionName(),
ID: docs[0].Ref.ID,
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
}
}
}
}
Expand All @@ -2870,19 +2875,21 @@ func (fr *Repository) PurgeUserByPhoneNumber(ctx context.Context, phone string)
if err != nil {
utils.RecordSpanError(span, err)
} else {
query := &GetAllQuery{
CollectionName: fr.GetCustomerProfileCollectionName(),
FieldName: "id",
Value: customer.ID,
Operator: "==",
}
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
if customer != nil {
query := &GetAllQuery{
CollectionName: fr.GetCustomerProfileCollectionName(),
ID: docs[0].Ref.ID,
FieldName: "id",
Value: customer.ID,
Operator: "==",
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
if docs, err := fr.FirestoreClient.GetAll(ctx, query); err == nil {
command := &DeleteCommand{
CollectionName: fr.GetCustomerProfileCollectionName(),
ID: docs[0].Ref.ID,
}
if err = fr.FirestoreClient.Delete(ctx, command); err != nil {
return exceptions.InternalServerError(err)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,6 @@ func TestPurgeUserByPhoneNumber(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, spr)

// call PurgeUserByPhoneNumber using the phone number associated with invalidpr1. this should fail since it does not have
// an associated pin
err = fr.PurgeUserByPhoneNumber(ctx, interserviceclient.TestUserPhoneNumber)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "server error! unable to perform operation")

// now set a pin. this should not fail
userpin := "1234"
pset, err := s.UserPIN.SetUserPIN(ctx, userpin, invalidpr1.ID)
Expand Down

0 comments on commit c67f70a

Please sign in to comment.