diff --git a/product/infrastructure/fake/searchservice.go b/product/infrastructure/fake/searchservice.go index fee91bd64..3616308e3 100644 --- a/product/infrastructure/fake/searchservice.go +++ b/product/infrastructure/fake/searchservice.go @@ -77,7 +77,7 @@ func (s *SearchService) Search(ctx context.Context, filters ...searchDomain.Filt NumPages: 10, NumResults: len(hits), SelectedFacets: selectedFacets, - SortOptions: mapSortOptions(s.sortConfig), + SortOptions: mapSortOptions(s.sortConfig, filters), }, Hits: documents, Suggestion: []searchDomain.Suggestion{}, @@ -183,7 +183,15 @@ func (s *SearchService) findCurrentPage(filters []searchDomain.Filter) int { } // mapSortOptions maps searchperience delivered sort options to corresponding domain objects. -func mapSortOptions(sortConfigs []sortConfig) []searchDomain.SortOption { +func mapSortOptions(sortConfigs []sortConfig, filters []searchDomain.Filter) []searchDomain.SortOption { + lookup := make(map[string]bool, 1) // only one field expected + + for _, filter := range filters { + if sortFiler, ok := filter.(*searchDomain.SortFilter); ok { + lookup[sortFiler.Field()] = true // direction always true for that filter name + } + } + result := make([]searchDomain.SortOption, len(sortConfigs)) for i, sortConfig := range sortConfigs { @@ -192,8 +200,8 @@ func mapSortOptions(sortConfigs []sortConfig) []searchDomain.SortOption { Field: sortConfig.Key, Asc: sortConfig.Asc, Desc: sortConfig.Desc, - SelectedAsc: false, - SelectedDesc: true, + SelectedAsc: lookup[sortConfig.Asc], + SelectedDesc: lookup[sortConfig.Desc], } } diff --git a/product/infrastructure/fake/searchservice_test.go b/product/infrastructure/fake/searchservice_test.go index b10d0dc1d..4a3a43ecf 100644 --- a/product/infrastructure/fake/searchservice_test.go +++ b/product/infrastructure/fake/searchservice_test.go @@ -6,18 +6,30 @@ import ( "path/filepath" "testing" + "flamingo.me/flamingo/v3/framework/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "flamingo.me/flamingo-commerce/v3/product/domain" "flamingo.me/flamingo-commerce/v3/product/infrastructure/fake" searchDomain "flamingo.me/flamingo-commerce/v3/search/domain" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" +) + +type ( + testSortConfig struct { + Key string + Label string + Asc string + Desc string + } ) func TestSearchService_Search(t *testing.T) { s := fake.SearchService{} s.Inject(&fake.ProductService{}, &struct { - LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"` - CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"` + LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"` + CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"` + SortConfig config.Slice `inject:"config:commerce.product.fakeservice.sorting"` }{}) t.Run("Category Facet", func(t *testing.T) { @@ -150,9 +162,9 @@ func TestSearchService_SearchBy(t *testing.T) { NumResults: 0, SelectedFacets: []searchDomain.Facet{}, SortOptions: []searchDomain.SortOption{ - {Field: "camera", Label: "camera", SelectedDesc: false, SelectedAsc: true}, - {Field: "size", Label: "size", SelectedDesc: true, SelectedAsc: false}, - {Field: "no-direction", Label: "no-direction", SelectedDesc: false, SelectedAsc: true}, + {Field: "camera", Label: "camera", SelectedDesc: false, SelectedAsc: true, Asc: "camera"}, + {Field: "size", Label: "size", SelectedDesc: true, SelectedAsc: false, Desc: "size"}, + {Field: "no-direction", Label: "no-direction", SelectedDesc: false, SelectedAsc: true, Asc: "no-direction"}, }, }, Hits: []searchDomain.Document{}, @@ -258,10 +270,28 @@ func TestSearchService_SearchBy(t *testing.T) { s := new(fake.SearchService).Inject( new(fake.ProductService), &struct { - LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"` - CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"` + LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"` + CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"` + SortConfig config.Slice `inject:"config:commerce.product.fakeservice.sorting"` }{ LiveSearchJSON: tt.fields.liveSearchJSON, + SortConfig: config.Slice{ + testSortConfig{ + Key: "camera", + Label: "camera", + Asc: "camera", + }, + testSortConfig{ + Key: "size", + Label: "size", + Desc: "size", + }, + testSortConfig{ + Key: "no-direction", + Label: "no-direction", + Asc: "no-direction", + }, + }, }, ) got, err := s.SearchBy(context.Background(), tt.args.attribute, nil, tt.args.filters...) diff --git a/search/domain/service.go b/search/domain/service.go index 6dbc019e1..d735359ef 100644 --- a/search/domain/service.go +++ b/search/domain/service.go @@ -64,10 +64,10 @@ type ( SelectedDesc bool // Asc - represents the field that is used to trigger ascending search. // Deprecated: use "Field" and "SelectedAsc" instead to set which field should be sortable - Asc string + Asc string // Should it be deprecated ?? marked as deprecated in 2019 and used in 2020 // Desc - represents the field that is used to trigger descending search. // Deprecated: use "Field" and "SelectedDesc" instead to set which field should be sortable - Desc string + Desc string // Should it be deprecated ?? marked as deprecated in 2019 and used in 2020 } // FacetType for type facets