Skip to content

Commit

Permalink
Fix for basePath on createSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonkrogh committed Dec 20, 2022
1 parent 5f182b5 commit c8ee74d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-flies-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crystallize/import-utilities': patch
---

Fixed an issue with `bootstrapper.createSpec` with `basePath` being set as it only returned the _first_ matching item. It now returns _all_ items that has a `cataloguePath` that starts with the `basePath`.
26 changes: 14 additions & 12 deletions src/bootstrap-tenant/__dev__create-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { BootstrapperError } from './bootstrapper'
import { Bootstrapper, EVENT_NAMES } from './index'

async function createSpec() {
const tenantIdentifier = 'giphy'
const tenantIdentifier = 'bos-ecom-qa'

console.log(`✨ Creating spec for ${tenantIdentifier} ✨`)

const bootstrapper = new Bootstrapper()
bootstrapper.env = 'dev'
// bootstrapper.env = 'dev'
// bootstrapper.env = 'prod'

bootstrapper.setAccessToken(
process.env.DEV_CRYSTALLIZE_ACCESS_TOKEN_ID!,
process.env.DEV_CRYSTALLIZE_ACCESS_TOKEN_SECRET!
process.env.CRYSTALLIZE_ACCESS_TOKEN_ID!,
process.env.CRYSTALLIZE_ACCESS_TOKEN_SECRET!
)

bootstrapper.setTenantIdentifier(tenantIdentifier)
Expand All @@ -31,18 +31,20 @@ async function createSpec() {
})

const spec = await bootstrapper.createSpec({
shapes: false,
grids: false,
items: false,
languages: false,
vatTypes: false,
priceVariants: false,
shapes: false,
topicMaps: false,
grids: false,
items: {
basePath: '/pim/vehicles/mercedes-benz/eqc/eqc-400-4m/eqc400-advanced',
},
stockLocations: false,
vatTypes: false,
subscriptionPlans: false,
topicMaps: false,
orders: false,
customers: true,
onUpdate: (u) => console.log(JSON.stringify(u, null, 1)),
onUpdate: (areaUpdate) => {
console.log(JSON.stringify(areaUpdate, null, 1))
},
})

writeFileSync(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import test from 'ava'
import { buildPathShouldBeIncludedValidator } from './get-all-catalogue-items'
import { JSONItem } from '../../json-spec'
import {
buildPathShouldBeIncludedValidator,
getOnlyItemsWithPathStartingWith,
} from './get-all-catalogue-items'

test('pathValidator should work with single level paths', (t) => {
const validator = buildPathShouldBeIncludedValidator('/shop')
Expand All @@ -23,3 +27,32 @@ test('pathValidator should work with nested paths', (t) => {
t.is(false, validator('/shoes').descendants)
t.is(false, validator('/').thisItem)
})

test('final filtering should work', (t) => {
const basePath = '/products/shoes/shoe'

const firstShoe: JSONItem = {
name: 'first',
cataloguePath: '/products/shoes/shoe-first',
shape: '',
}
const secondShoe: JSONItem = {
name: 'second',
cataloguePath: '/products/shoes/shoe-second',
shape: '',
}

const allCatalogueItemsForLanguage: JSONItem[] = [
{
name: 'Shoes',
cataloguePath: '/products/shoes',
shape: 'default-folder',
children: [firstShoe, secondShoe],
},
]

t.deepEqual(
[firstShoe, secondShoe],
getOnlyItemsWithPathStartingWith(basePath, allCatalogueItemsForLanguage)
)
})
29 changes: 18 additions & 11 deletions src/bootstrap-tenant/bootstrapper/utils/get-all-catalogue-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ export function buildPathShouldBeIncludedValidator(basePath = '') {

const p = path || ''

const thisItem = p.startsWith(basePath)

return {
thisItem: p.startsWith(basePath),
descendants: basePath.startsWith(p),
thisItem,
descendants: thisItem || basePath.startsWith(p),
}
}
}
Expand Down Expand Up @@ -248,6 +250,7 @@ export async function getAllCatalogueItems(
const children = await getChildren()
if (children.length > 0) {
itemWithChildren.children = children
console.log(itemWithChildren.children.map((c) => c.cataloguePath))
}
}

Expand All @@ -266,6 +269,7 @@ export async function getAllCatalogueItems(

for (let i = 0; i < rawChilds.length; i++) {
const pathValid = pathShouldBeIncluded(rawChilds[i].path)

if (pathValid.descendants || pathValid.thisItem) {
const item = await getItem(rawChilds[i])
if (item) {
Expand Down Expand Up @@ -797,19 +801,26 @@ fragment subscriptionPlanPricing on ProductVariantSubscriptionPlanPricing {
}
}
`
function getOnlyItemsWithPathStartingWith(
export function getOnlyItemsWithPathStartingWith(
basePath: string,
allCatalogueItemsForLanguage: JSONItem[]
): JSONItem[] {
if (basePath === '/') {
return allCatalogueItemsForLanguage
}
let foundItem: JSONItem | null = null

const ret: JSONItem[] = []

let foundExactFolderMatch = false

function handleLevel(item: JSONItem) {
if (!foundItem) {
if (!foundExactFolderMatch) {
if (item.cataloguePath?.startsWith(basePath)) {
foundItem = item
if ('children' in item) {
foundExactFolderMatch = true
}

ret.push(item)
} else {
const f = item as JSONFolder
if (f.children) {
Expand All @@ -821,9 +832,5 @@ function getOnlyItemsWithPathStartingWith(

allCatalogueItemsForLanguage.forEach(handleLevel)

if (foundItem) {
return [foundItem]
}

return []
return ret
}

0 comments on commit c8ee74d

Please sign in to comment.