Skip to content

Commit

Permalink
chore: clean up supplier login implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Otieno Calvine <nyarangaotieno@gmail.com>
  • Loading branch information
NYARAS committed Aug 29, 2021
1 parent 83651d9 commit 68b7abd
Show file tree
Hide file tree
Showing 8 changed files with 1,698 additions and 3,508 deletions.
110 changes: 0 additions & 110 deletions pkg/onboarding/application/dto/input.go
@@ -1,12 +1,10 @@
package dto

import (
"net/url"
"time"

"github.com/savannahghi/enumutils"
"github.com/savannahghi/feedlib"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/profileutils"
"github.com/savannahghi/scalarutils"
Expand All @@ -30,80 +28,6 @@ type PostVisitSurveyInput struct {
Suggestions string `json:"suggestions" firestore:"suggestions"`
}

// BusinessPartnerFilterInput is used to supply filter parameters for organizatiom filter inputs
type BusinessPartnerFilterInput struct {
Search *string `json:"search"`
Name *string `json:"name"`
SladeCode *string `json:"slade_code"`
}

// ToURLValues transforms the filter input to `url.Values`
func (i *BusinessPartnerFilterInput) ToURLValues() (values url.Values) {
vals := url.Values{}
if i.Search != nil {
vals.Add("search", *i.Search)
}
if i.Name != nil {
vals.Add("name", *i.Name)
}
if i.SladeCode != nil {
vals.Add("slade_code", *i.SladeCode)
}
return vals
}

// BusinessPartnerSortInput is used to supply sort input for organization list queries
type BusinessPartnerSortInput struct {
Name *enumutils.SortOrder `json:"name"`
SladeCode *enumutils.SortOrder `json:"slade_code"`
}

// ToURLValues transforms the filter input to `url.Values`
func (i *BusinessPartnerSortInput) ToURLValues() (values url.Values) {
vals := url.Values{}
if i.Name != nil {
if *i.Name == enumutils.SortOrderAsc {
vals.Add("order_by", "name")
} else {
vals.Add("order_by", "-name")
}
}
if i.SladeCode != nil {
if *i.Name == enumutils.SortOrderAsc {
vals.Add("slade_code", "number")
} else {
vals.Add("slade_code", "-number")
}
}
return vals
}

// BranchSortInput is used to supply sorting input for location list queries
type BranchSortInput struct {
Name *enumutils.SortOrder `json:"name"`
SladeCode *enumutils.SortOrder `json:"slade_code"`
}

// ToURLValues transforms the sort input to `url.Values`
func (i *BranchSortInput) ToURLValues() (values url.Values) {
vals := url.Values{}
if i.Name != nil {
if *i.Name == enumutils.SortOrderAsc {
vals.Add("order_by", "name")
} else {
vals.Add("order_by", "-name")
}
}
if i.SladeCode != nil {
if *i.SladeCode == enumutils.SortOrderAsc {
vals.Add("slade_code", "number")
} else {
vals.Add("slade_code", "-number")
}
}
return vals
}

// SignUpInput represents the user information required to create a new account
type SignUpInput struct {
PhoneNumber *string `json:"phoneNumber"`
Expand All @@ -112,40 +36,6 @@ type SignUpInput struct {
OTP *string `json:"otp"`
}

// BranchEdge is used to serialize GraphQL Relay edges for locations
type BranchEdge struct {
Cursor *string `json:"cursor"`
Node *domain.Branch `json:"node"`
}

// BranchConnection is used tu serialize GraphQL Relay connections for locations
type BranchConnection struct {
Edges []*BranchEdge `json:"edges"`
PageInfo *firebasetools.PageInfo `json:"pageInfo"`
}

// BranchFilterInput is used to supply filter parameters for locatioon list queries
type BranchFilterInput struct {
Search *string `json:"search"`
SladeCode *string `json:"sladeCode"`
ParentOrganizationID *string `json:"parentOrganizationID"`
}

// ToURLValues transforms the filter input to `url.Values`
func (i *BranchFilterInput) ToURLValues() url.Values {
vals := url.Values{}
if i.Search != nil {
vals.Add("search", *i.Search)
}
if i.SladeCode != nil {
vals.Add("slade_code", *i.SladeCode)
}
if i.ParentOrganizationID != nil {
vals.Add("parent", *i.ParentOrganizationID)
}
return vals
}

// PhoneNumberPayload used when verifying a phone number.
type PhoneNumberPayload struct {
PhoneNumber *string `json:"phoneNumber"`
Expand Down
217 changes: 0 additions & 217 deletions pkg/onboarding/application/dto/input_test.go
@@ -1,218 +1 @@
package dto

import (
"net/url"
"reflect"
"testing"

"github.com/savannahghi/enumutils"
)

func TestBusinessPartnerFilterInput_ToURLValues(t *testing.T) {
var (
search = "data"
name = "somename"
sladeCode = "slasde"
)

correctFilters := BusinessPartnerFilterInput{
Search: &search,
Name: &name,
SladeCode: &sladeCode,
}

failingFilters := BusinessPartnerFilterInput{
Search: &search,
}

expectedFilter := url.Values{
"search": []string{search},
"name": []string{name},
"slade_code": []string{sladeCode},
}

tests := []struct {
name string
filter BusinessPartnerFilterInput
wantValues url.Values
wantError bool
}{
{
name: "success url values transformation",
filter: correctFilters,
wantValues: expectedFilter,
wantError: false,
},
{
name: "bad filter data",
filter: failingFilters,
wantValues: expectedFilter,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !reflect.DeepEqual(tt.wantValues, tt.filter.ToURLValues()) && !tt.wantError {
t.Errorf("BusinessPartnerFilterInput.ToURLValues() = %v, want %v", tt.filter.ToURLValues(), tt.wantValues)
}
})
}
}

func TestBusinessPartnerSortInput_ToURLValues(t *testing.T) {

var name enumutils.SortOrder = enumutils.SortOrderAsc
var sladeCode enumutils.SortOrder = enumutils.SortOrderAsc
var sladeCode2 enumutils.SortOrder = enumutils.SortOrderDesc

correctFilters := BusinessPartnerSortInput{
Name: &name,
SladeCode: &sladeCode,
}

failingFilters := BusinessPartnerSortInput{
Name: &name,
SladeCode: &sladeCode2,
}

expectedSortValue := url.Values{
"order_by": []string{"name"},
"slade_code": []string{"number"},
}

tests := []struct {
name string
filter BusinessPartnerSortInput
wantValues url.Values
wantError bool
}{
{
name: "success passing sort filter",
filter: correctFilters,
wantValues: expectedSortValue,
wantError: false,
},
{
name: "bad filters",
filter: failingFilters,
wantValues: expectedSortValue,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !reflect.DeepEqual(tt.wantValues, tt.filter.ToURLValues()) && !tt.wantError {
t.Errorf("BusinessPartnerSortInput.ToURLValues() = %v, want %v", tt.filter.ToURLValues(), tt.wantValues)
}
})
}
}

func TestBranchSortInput_ToURLValues(t *testing.T) {
var name enumutils.SortOrder = enumutils.SortOrderAsc
var sladeCode enumutils.SortOrder = enumutils.SortOrderDesc
var sladeCode2 enumutils.SortOrder = enumutils.SortOrderAsc

correctFilters := BranchSortInput{
Name: &name,
SladeCode: &sladeCode,
}

failingFilters := BranchSortInput{
Name: &name,
SladeCode: &sladeCode2,
}

expectedFilters := url.Values{
"order_by": []string{"name"},
"slade_code": []string{"-number"},
}

tests := []struct {
name string
filter BranchSortInput
wantValues url.Values
wantError bool
}{
{
name: "success building filters",
filter: correctFilters,
wantValues: expectedFilters,
wantError: false,
},
{
name: "bad filters",
filter: failingFilters,
wantValues: expectedFilters,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &BranchSortInput{
Name: tt.filter.Name,
SladeCode: tt.filter.SladeCode,
}
if gotValues := i.ToURLValues(); !reflect.DeepEqual(gotValues, tt.wantValues) && !tt.wantError {
t.Errorf("BranchSortInput.ToURLValues() = %v, want %v", gotValues, tt.wantValues)
}
})
}
}

func TestBranchFilterInput_ToURLValues(t *testing.T) {

var (
search = "somesearch"
sladeCode = "sladecode"
parentOrganizationID = "parentorg"
)

correctFilters := BranchFilterInput{
Search: &search,
SladeCode: &sladeCode,
ParentOrganizationID: &parentOrganizationID,
}

failingFilter := BranchFilterInput{
Search: &search,
}

expectedFilters := url.Values{
"search": []string{search},
"slade_code": []string{sladeCode},
"parent": []string{parentOrganizationID},
}

tests := []struct {
name string
filter BranchFilterInput
wantValues url.Values
wantError bool
}{
{
name: "success transforming url values ",
filter: correctFilters,
wantValues: expectedFilters,
wantError: false,
},
{
name: "bad filters",
filter: failingFilter,
wantValues: expectedFilters,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &BranchFilterInput{
Search: tt.filter.Search,
SladeCode: tt.filter.SladeCode,
ParentOrganizationID: tt.filter.ParentOrganizationID,
}
if got := i.ToURLValues(); !reflect.DeepEqual(got, tt.wantValues) && !tt.wantError {
t.Errorf("BranchFilterInput.ToURLValues() = %v, want %v", got, tt.wantValues)
}
})
}
}
18 changes: 0 additions & 18 deletions pkg/onboarding/application/dto/output.go
Expand Up @@ -6,18 +6,6 @@ import (
"github.com/savannahghi/profileutils"
)

// BusinessPartnerEdge is used to serialize GraphQL Relay edges for organization
type BusinessPartnerEdge struct {
Cursor *string `json:"cursor"`
Node *domain.BusinessPartner `json:"node"`
}

// BusinessPartnerConnection is used to serialize GraphQL Relay connections for organizations
type BusinessPartnerConnection struct {
Edges []*BusinessPartnerEdge `json:"edges"`
PageInfo *firebasetools.PageInfo `json:"pageInfo"`
}

// AgentConnection is used to serialize GraphQL Relay connections for agents
type AgentConnection struct {
Edges []AgentEdge `json:"edges"`
Expand Down Expand Up @@ -114,12 +102,6 @@ type CreatedUserResponse struct {
ProviderID string `json:"provider_id,omitempty"`
}

// SupplierLogin is the response returned after the user has successfully login to edi
type SupplierLogin struct {
Branches *BranchConnection `json:"branches,omitempty"`
Supplier *profileutils.Supplier `json:"supplier,omitempty"`
}

// UserInfo is a collection of standard profile information for a user.
type UserInfo struct {
DisplayName string `json:"displayName,omitempty"`
Expand Down

0 comments on commit 68b7abd

Please sign in to comment.