Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(product): add sorting options #587

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions product/infrastructure/fake/searchservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"strconv"

"flamingo.me/flamingo/v3/framework/config"

searchDomain "flamingo.me/flamingo-commerce/v3/search/domain"

"flamingo.me/flamingo-commerce/v3/product/domain"
Expand All @@ -17,27 +19,39 @@ type (
productService *ProductService
liveSearchJSON string
categoryFacetItemsJSON string
sortConfig []sortConfig
}
liveSearchData struct {
Marketplacecodes []string `json:"marketplacecodes"`
Sugestions []searchDomain.Suggestion `json:"sugestions"`
Promotions []searchDomain.Promotion `json:"promotions"`
Actions []searchDomain.Action `json:"actions"`
}

// sortConfig contains sorting configuration for search results
sortConfig struct {
Key string
Label string
Asc string
Desc string
}
)

// Inject dependencies
func (s *SearchService) Inject(
productService *ProductService,
cfg *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"`
carstendietrich marked this conversation as resolved.
Show resolved Hide resolved
},
) *SearchService {
s.productService = productService
if cfg != nil {
s.liveSearchJSON = cfg.LiveSearchJSON
s.categoryFacetItemsJSON = cfg.CategoryFacetItemsJSON

_ = cfg.SortConfig.MapInto(&s.sortConfig)
carstendietrich marked this conversation as resolved.
Show resolved Hide resolved
}

return s
Expand All @@ -63,7 +77,7 @@ func (s *SearchService) Search(ctx context.Context, filters ...searchDomain.Filt
NumPages: 10,
NumResults: len(hits),
SelectedFacets: selectedFacets,
SortOptions: s.findSorts(filters),
SortOptions: mapSortOptions(s.sortConfig, filters),
},
Hits: documents,
Suggestion: []searchDomain.Suggestion{},
Expand Down Expand Up @@ -168,17 +182,26 @@ func (s *SearchService) findCurrentPage(filters []searchDomain.Filter) int {
return currentPage
}

func (s *SearchService) findSorts(filters []searchDomain.Filter) []searchDomain.SortOption {
result := make([]searchDomain.SortOption, 0)
// first check if the searchDomain.SortFilter is given
for _, v := range filters {
if filter, ok := v.(*searchDomain.SortFilter); ok {
result = append(result, searchDomain.SortOption{
Label: filter.Field(),
Field: filter.Field(),
SelectedAsc: !filter.Descending(),
SelectedDesc: filter.Descending(),
})
// mapSortOptions maps searchperience delivered sort options to corresponding domain objects.
carstendietrich marked this conversation as resolved.
Show resolved Hide resolved
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 {
result[i] = searchDomain.SortOption{
Label: sortConfig.Label,
Field: sortConfig.Key,
Asc: sortConfig.Asc,
Desc: sortConfig.Desc,
SelectedAsc: lookup[sortConfig.Asc],
SelectedDesc: lookup[sortConfig.Desc],
}
}

Expand Down
48 changes: 39 additions & 9 deletions product/infrastructure/fake/searchservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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",
carstendietrich marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
)
got, err := s.SearchBy(context.Background(), tt.args.attribute, nil, tt.args.filters...)
Expand Down
4 changes: 2 additions & 2 deletions search/domain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
carstendietrich marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Down