Skip to content

Commit

Permalink
Merge pull request #14 from vtex-apps/fix/categoryTree
Browse files Browse the repository at this point in the history
in categoryTree, prevent empty answer when categoryId is not found
  • Loading branch information
jgfidelis authored Oct 15, 2019
2 parents b706b4f + 079fa2b commit 527a512
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Prevents empty answer in categoryTree if categoryId for product is not found in categoriesIds.

## [0.6.1] - 2019-10-15
### Fixed
Expand Down
28 changes: 23 additions & 5 deletions node/resolvers/catalog/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('tests related to product resolver', () => {
mockContext as any
)
expect(mockContext.clients.catalog.category).toBeCalledTimes(2)
expect(mockContext.clients.catalog.category.mock.calls[0][0]).toBe(10)
expect(mockContext.clients.catalog.category.mock.calls[1][0]).toBe(25)
expect(mockContext.clients.catalog.category.mock.calls[0][0]).toBe(25)
expect(mockContext.clients.catalog.category.mock.calls[1][0]).toBe(10)
})

test('get correct main category tree for product with more than one tree', async () => {
Expand All @@ -53,11 +53,11 @@ describe('tests related to product resolver', () => {
mockContext as any
)
expect(mockContext.clients.catalog.category).toBeCalledTimes(3)
expect(mockContext.clients.catalog.category.mock.calls[0][0]).toBe(
expect(mockContext.clients.catalog.category.mock.calls[0][0]).toBe(101)
expect(mockContext.clients.catalog.category.mock.calls[1][0]).toBe(101003)
expect(mockContext.clients.catalog.category.mock.calls[2][0]).toBe(
101003009
)
expect(mockContext.clients.catalog.category.mock.calls[1][0]).toBe(101003)
expect(mockContext.clients.catalog.category.mock.calls[2][0]).toBe(101)
})

test('ensure that GC account calls the category tree API ', async () => {
Expand Down Expand Up @@ -87,5 +87,23 @@ describe('tests related to product resolver', () => {
expect(mockContext.clients.catalog.categories.mock.calls[0][0]).toBe(2) //ensure maximum level was correct
expect(result!.length).toBe(2)
})

test('if categoryId does not match any id in categoriesIds, find biggest tree', async () => {
const catalogProduct = getProduct()
catalogProduct.categoryId = '1'
catalogProduct.categoriesIds = ['/2064927469/', '/2064927469/630877787/']
await resolvers.Product.categoryTree(
catalogProduct as any,
{},
mockContext as any
)
expect(mockContext.clients.catalog.category).toBeCalledTimes(2)
expect(mockContext.clients.catalog.category.mock.calls[0][0]).toBe(
2064927469
)
expect(mockContext.clients.catalog.category.mock.calls[1][0]).toBe(
630877787
)
})
})
})
36 changes: 23 additions & 13 deletions node/resolvers/catalog/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
prop,
propOr,
reject,
reverse,
split,
toPairs,
} from 'ramda'
Expand Down Expand Up @@ -61,6 +60,25 @@ const treeStringToArray = compose(
removeStartingSlashes
)

const findMainTree = (categoriesIds: string[], prodCategoryId: string) => {
const mainTree = categoriesIds.find(
treeIdString => getLastCategory(treeIdString) === prodCategoryId
)
if (mainTree) {
return treeStringToArray(mainTree)
}

// If we are here, did not find the specified main category ID in given strings. It is probably a bug.
// We will return the biggest tree we find

const trees = categoriesIds.map(treeStringToArray)

return trees.reduce(
(acc, currTree) => (currTree.length > acc.length ? currTree : acc),
[]
)
}

const productCategoriesToCategoryTree = async (
{ categories, categoriesIds, categoryId: prodCategoryId }: CatalogProduct,
_: any,
Expand All @@ -70,22 +88,14 @@ const productCategoriesToCategoryTree = async (
return []
}

const mainTree = categoriesIds.find(
treeIdString => getLastCategory(treeIdString) === prodCategoryId
)

if (!mainTree) {
return []
}
const mainTreeIds = treeStringToArray(mainTree)
const reversedIds = reverse(mainTreeIds)
const mainTreeIds = findMainTree(categoriesIds, prodCategoryId)

if (platform === 'vtex') {
return reversedIds.map(categoryId => catalog.category(Number(categoryId)))
return mainTreeIds.map(categoryId => catalog.category(Number(categoryId)))
}
const categoriesTree = await catalog.categories(reversedIds.length)
const categoriesTree = await catalog.categories(mainTreeIds.length)
const categoryMap = buildCategoryMap(categoriesTree)
const mappedCategories = reversedIds
const mappedCategories = mainTreeIds
.map(id => categoryMap[id])
.filter(Boolean)

Expand Down

0 comments on commit 527a512

Please sign in to comment.