Skip to content

Commit

Permalink
config.shapeComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonkrogh committed Nov 16, 2022
1 parent fa99b7f commit 29b92af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
8 changes: 8 additions & 0 deletions .changeset/honest-flies-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@crystallize/import-utilities': minor
---

Added new bootstrapper config option shapeComponents. It opens up for replacing
all shape components with what you provide in the spec. Default value is
"amdend", which will amend your components to what is already present for the
shape.
26 changes: 6 additions & 20 deletions src/bootstrap-tenant/__dev__bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,18 @@ import { JsonSpec } from './json-spec'

async function bootstrap() {
try {
const tenantIdentifier = 'hkn-test-test'
const tenantIdentifier = 'hkn-test-shape-clear'
const jsonSpec: JsonSpec = {
shapes: [
{
identifier: 'test-folder',
name: 'Test Folder',
type: 'folder',
},
{
identifier: 'test-product',
name: 'Test Product',
type: 'product',
},
],
items: [
{
name: 'Basic Folder',
shape: 'test-folder',
},
{
name: 'Basic product',
shape: 'test-product',
vatType: 'No Tax',
variants: [
components: [
{
name: 'Basic Variant',
sku: 'basic-variant',
id: 'blah',
type: 'paragraphCollection',
name: 'blah',
},
],
},
Expand All @@ -48,6 +33,7 @@ async function bootstrap() {
const bootstrapper = new Bootstrapper()
// bootstrapper.env = 'dev'

bootstrapper.config.shapeComponents = 'replace'
bootstrapper.setTenantIdentifier(tenantIdentifier)

bootstrapper.setAccessToken(
Expand Down
54 changes: 45 additions & 9 deletions src/bootstrap-tenant/bootstrapper/shapes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,54 @@ export async function setShapes({
// Ensure that the shape identifier is truncated
data.identifier = validShapeIdentifier(data.identifier, onUpdate)

// Merge in existing shape
// Get existing shape
const existingShapeConfig = existingShapes.find(
(e) => e.identifier === data.identifier
)
if (existingShapeConfig?.components) {
const comps = data.components || []
data.components = [
...existingShapeConfig.components.filter(
(c) => !comps.map((c) => c.id).includes(c.id)
),
...comps,
]

// Delete shape components (not variant components) before adding new ones
if (existingShapeConfig) {
if (context.config.shapeComponents === 'replace') {
try {
await context.callPIM({
query: `
mutation CLEAR_SHAPE_COMPONENTS (
$tenantId: ID!
$identifier: String!
) {
shape {
update (
tenantId: $tenantId
identifier: $identifier
input: {
components: []
}
) {
identifier
}
}
}
`,
variables: {
identifier: data.identifier,
tenantId: context.tenantId,
},
})
} catch (e) {
console.log(e)
}
}

// Merge in existing shape
else if (existingShapeConfig?.components) {
const comps = data.components || []
data.components = [
...existingShapeConfig.components.filter(
(c) => !comps.map((c) => c.id).includes(c.id)
),
...comps,
]
}
}

const result = await handleShape(data, context)
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap-tenant/bootstrapper/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface AreaUpdate {
export interface Config {
itemTopics?: 'amend' | 'replace'
itemPublish?: 'publish' | 'auto'
shapeComponents?: 'amend' | 'replace'
logLevel?: LogLevel
multilingual?: boolean
experimental: {
Expand Down

0 comments on commit 29b92af

Please sign in to comment.