Skip to content

Commit

Permalink
fix: ensure that returned navigation actions are in the order required
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Aug 9, 2021
1 parent 404c0ba commit bfb3fc5
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 55 deletions.
66 changes: 35 additions & 31 deletions pkg/onboarding/application/utils/actions.go
Expand Up @@ -36,8 +36,9 @@ func GetUserNavigationActions(
//all user actions
userNavigationActions := []domain.NavigationAction{}

for _, action := range domain.AllNavigationActions {

allActions := domain.AllNavigationActions
for i := 0; i < len(allActions); i++ {
action := allActions[i]
if action.RequiredPermission == nil || CheckUserHasPermission(roles, *action.RequiredPermission) {
// check for favorite navigation actions
if IsFavNavAction(&user, action.Title) {
Expand All @@ -61,38 +62,40 @@ func GetUserNavigationActions(
// GroupNested groups navigation actions into parents and children
func GroupNested(
actions []domain.NavigationAction,
) map[domain.NavigationGroup]domain.NavigationAction {
grouped := make(map[domain.NavigationGroup]domain.NavigationAction)

) []domain.NavigationAction {
parents := []domain.NavigationAction{}
grouped := []domain.NavigationAction{}
// map all parents to grouped
for _, action := range actions {
for i := 0; i < len(actions); i++ {
action := actions[i]

if !action.HasParent {
grouped[action.Group] = action
parents = append(parents, action)
}
}

//map all children to respective parents
for _, action := range actions {
if action.HasParent {
parent, isAvailable := grouped[action.Group]
if isAvailable {
for _, parent := range parents {
for i := 0; i < len(actions); i++ {
action := actions[i]
if action.HasParent && action.Group == parent.Group {
parent.Nested = append(parent.Nested, action)
grouped[action.Group] = parent
}
}
grouped = append(grouped, parent)
}

return grouped
}

// GroupPriority groups navigation actions into primary and secondary actions
func GroupPriority(
actions map[domain.NavigationGroup]domain.NavigationAction,
actions []domain.NavigationAction,
) (primary, secondary []domain.NavigationAction) {
primary = []domain.NavigationAction{}
secondary = []domain.NavigationAction{}

added := make(map[domain.NavigationGroup]domain.NavigationAction)
mapped := make(map[domain.NavigationGroup]bool)

//pb is number of navactions that can possibly be on bottom navigation ie primary
pb := 0
Expand All @@ -104,10 +107,11 @@ func GroupPriority(

// add all the possible bottom action to primary if they are less or equal to 4
if pb <= 4 {
for _, action := range actions {
for i := 0; i < len(actions); i++ {
action := actions[i]
if len(action.Nested) == 0 {
primary = append(primary, action)
added[action.Group] = action
mapped[action.Group] = true
}
}
} else {
Expand All @@ -116,15 +120,15 @@ func GroupPriority(
break
}
// add all the high priority first
for _, action := range actions {

for i := 0; i < len(actions); i++ {
action := actions[i]
if action.IsHighPriority {

_, exist := added[action.Group]
if !exist && len(action.Nested) == 0 {
_, wasMapped := mapped[action.Group]
if !wasMapped && len(action.Nested) == 0 {

primary = append(primary, action)
added[action.Group] = action
mapped[action.Group] = true

if len(primary) == 4 {
break
Expand All @@ -133,13 +137,13 @@ func GroupPriority(
}
}
// add other actions is high priority actions are less than four
for _, action := range actions {

_, exist := added[action.Group]
if !exist && len(action.Nested) == 0 {
for i := 0; i < len(actions); i++ {
action := actions[i]
_, wasMapped := mapped[action.Group]
if !wasMapped && len(action.Nested) == 0 {

primary = append(primary, action)
added[action.Group] = action
mapped[action.Group] = true

if len(primary) == 4 {
break
Expand All @@ -150,13 +154,13 @@ func GroupPriority(
}

// add all remaining items to secondary
for _, action := range actions {

_, exists := added[action.Group]
if !exists {
for i := 0; i < len(actions); i++ {
action := actions[i]
_, wasMapped := mapped[action.Group]
if !wasMapped {

secondary = append(secondary, action)
added[action.Group] = action
mapped[action.Group] = true
}
}

Expand Down
115 changes: 95 additions & 20 deletions pkg/onboarding/application/utils/actions_test.go
@@ -1,9 +1,11 @@
package utils

import (
"context"
"reflect"
"testing"

"github.com/savannahghi/onboarding/pkg/onboarding/application/dto"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/profileutils"
)
Expand Down Expand Up @@ -60,11 +62,74 @@ func TestCheckUserHasPermission(t *testing.T) {
}
}

func TestGetUserNavigationActions(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
user profileutils.UserProfile
roles []profileutils.Role
}

homeNavAction := domain.HomeNavAction
homeNavAction.Favorite = true

agentNavActions := domain.AgentNavActions
agentNavActions.Nested = []interface{}{
domain.AgentRegistrationNavAction,
domain.AgentidentificationNavAction,
}
tests := []struct {
name string
args args
want *dto.GroupedNavigationActions
wantErr bool
}{
{
name: "happy got user navigation actions",
args: args{
ctx: ctx,
user: profileutils.UserProfile{
FavNavActions: []string{"Home"},
},
roles: []profileutils.Role{
{
Scopes: []string{"agent.view", "agent.register", "agent.identify"},
Active: true,
},
},
},
want: &dto.GroupedNavigationActions{
Primary: []domain.NavigationAction{
homeNavAction,
domain.HelpNavAction,
},
Secondary: []domain.NavigationAction{
agentNavActions,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserNavigationActions(tt.args.ctx, tt.args.user, tt.args.roles)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserNavigationActions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserNavigationActions() = %v, want %v", got, tt.want)
}
})
}
}

func TestGroupNested(t *testing.T) {
type args struct {
actions []domain.NavigationAction
}
expectedOutput := make(map[domain.NavigationGroup]domain.NavigationAction)
expectedOutput := []domain.NavigationAction{}

navAction := domain.NavigationAction{
Group: domain.HomeGroup,
Title: "Home",
Expand All @@ -82,12 +147,12 @@ func TestGroupNested(t *testing.T) {
},
}

expectedOutput[domain.HomeGroup] = navAction
expectedOutput = append(expectedOutput, navAction)

tests := []struct {
name string
args args
want map[domain.NavigationGroup]domain.NavigationAction
want []domain.NavigationAction
}{
{
name: "happy grouped nested navigation actions",
Expand Down Expand Up @@ -123,9 +188,10 @@ func TestGroupNested(t *testing.T) {

func TestGroupPriority(t *testing.T) {
type args struct {
actions map[domain.NavigationGroup]domain.NavigationAction
actions []domain.NavigationAction
}
actions := make(map[domain.NavigationGroup]domain.NavigationAction)
actions := []domain.NavigationAction{}

navAction1 := domain.NavigationAction{
Group: domain.HomeGroup,
Title: "Home",
Expand Down Expand Up @@ -170,37 +236,46 @@ func TestGroupPriority(t *testing.T) {
IsHighPriority: true,
}

actions[domain.HomeGroup] = navAction1
actions[domain.AgentGroup] = navAction2
actions[domain.PatientGroup] = navAction3
actions[domain.PartnerGroup] = navAction4
actions[domain.RoleGroup] = navAction5
actions[domain.ConsumerGroup] = navAction6
actions[domain.EmployeeGroup] = navAction7
actions = append(actions, navAction1)
actions = append(actions, navAction2)
actions = append(actions, navAction3)
actions = append(actions, navAction4)
actions = append(actions, navAction5)
actions = append(actions, navAction6)
actions = append(actions, navAction7)

tests := []struct {
name string
args args
wantPrimary int
wantSecondary int
wantPrimary []domain.NavigationAction
wantSecondary []domain.NavigationAction
}{
{
name: "happy: grouped into priorities",
args: args{
actions: actions,
},
wantPrimary: 4,
wantSecondary: 3,
wantPrimary: []domain.NavigationAction{
navAction5,
navAction6,
navAction7,
navAction2,
},
wantSecondary: []domain.NavigationAction{
navAction1,
navAction3,
navAction4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPrimary, gotSecondary := GroupPriority(tt.args.actions)
if !reflect.DeepEqual(len(gotPrimary), tt.wantPrimary) {
t.Errorf("GroupPriority() gotPrimary = %v, want %v", len(gotPrimary), tt.wantPrimary)
if !reflect.DeepEqual(gotPrimary, tt.wantPrimary) {
t.Errorf("GroupPriority() gotPrimary = %v, want %v", gotPrimary, tt.wantPrimary)
}
if !reflect.DeepEqual(len(gotSecondary), tt.wantSecondary) {
t.Errorf("GroupPriority() gotSecondary = %v, want %v", len(gotSecondary), tt.wantSecondary)
if !reflect.DeepEqual(gotSecondary, tt.wantSecondary) {
t.Errorf("GroupPriority() gotSecondary = %v, want %v", gotSecondary, tt.wantSecondary)
}
})
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/onboarding/application/utils/helpers.go
Expand Up @@ -192,7 +192,8 @@ func CheckEmptyString(text string) (*string, error) {
func NewActionsMapper(ctx context.Context, grouped *dto.GroupedNavigationActions) *profileutils.NavigationActions {
mapped := &profileutils.NavigationActions{}

for _, action := range grouped.Primary {
for i := 0; i < len(grouped.Primary); i++ {
action := grouped.Primary[i]
c := profileutils.NavAction{
Title: action.Title,
OnTapRoute: action.OnTapRoute,
Expand All @@ -202,7 +203,8 @@ func NewActionsMapper(ctx context.Context, grouped *dto.GroupedNavigationActions
mapped.Primary = append(mapped.Primary, c)
}

for _, action := range grouped.Secondary {
for i := 0; i < len(grouped.Secondary); i++ {
action := grouped.Secondary[i]
c := profileutils.NavAction{
Title: action.Title,
OnTapRoute: action.OnTapRoute,
Expand All @@ -211,8 +213,9 @@ func NewActionsMapper(ctx context.Context, grouped *dto.GroupedNavigationActions
}

if len(action.Nested) > 0 {
for _, nested := range action.Nested {
nestedAction := nested.(domain.NavigationAction)

for i := 0; i < len(action.Nested); i++ {
nestedAction := (action.Nested[i]).(domain.NavigationAction)
m := profileutils.NestedNavAction{
Title: nestedAction.Title,
OnTapRoute: nestedAction.OnTapRoute,
Expand Down

0 comments on commit bfb3fc5

Please sign in to comment.