@labdigital/commercetools-mock 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +8 -0
  2. package/dist/index.d.ts +18 -17
  3. package/dist/index.global.js +1751 -1664
  4. package/dist/index.global.js.map +1 -1
  5. package/dist/index.js +1773 -1684
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +1966 -1877
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +28 -20
  10. package/src/constants.ts +4 -2
  11. package/src/ctMock.ts +19 -84
  12. package/src/helpers.ts +9 -10
  13. package/src/index.test.ts +1 -1
  14. package/src/lib/haversine.ts +2 -2
  15. package/src/lib/masking.ts +3 -1
  16. package/src/lib/predicateParser.test.ts +16 -0
  17. package/src/lib/predicateParser.ts +94 -86
  18. package/src/lib/projectionSearchFilter.test.ts +28 -36
  19. package/src/lib/projectionSearchFilter.ts +86 -102
  20. package/src/oauth/store.ts +3 -3
  21. package/src/priceSelector.test.ts +18 -35
  22. package/src/priceSelector.ts +6 -9
  23. package/src/product-projection-search.ts +51 -57
  24. package/src/repositories/abstract.ts +85 -41
  25. package/src/repositories/cart-discount.ts +1 -1
  26. package/src/repositories/cart.ts +36 -31
  27. package/src/repositories/category.ts +17 -19
  28. package/src/repositories/channel.ts +1 -1
  29. package/src/repositories/custom-object.ts +35 -22
  30. package/src/repositories/customer-group.ts +1 -1
  31. package/src/repositories/customer.ts +39 -1
  32. package/src/repositories/discount-code.ts +1 -1
  33. package/src/repositories/errors.ts +9 -11
  34. package/src/repositories/extension.ts +13 -11
  35. package/src/repositories/helpers.ts +8 -13
  36. package/src/repositories/index.ts +59 -0
  37. package/src/repositories/inventory-entry.ts +1 -1
  38. package/src/repositories/order.ts +6 -6
  39. package/src/repositories/payment.ts +3 -3
  40. package/src/repositories/product-discount.ts +1 -1
  41. package/src/repositories/product-projection.ts +1 -0
  42. package/src/repositories/product-type.ts +29 -34
  43. package/src/repositories/product.ts +124 -80
  44. package/src/repositories/project.ts +10 -27
  45. package/src/repositories/shipping-method.ts +15 -17
  46. package/src/repositories/shopping-list.ts +2 -2
  47. package/src/repositories/state.ts +9 -9
  48. package/src/repositories/store.ts +2 -2
  49. package/src/repositories/subscription.ts +1 -1
  50. package/src/repositories/tax-category.ts +4 -4
  51. package/src/repositories/type.ts +12 -14
  52. package/src/repositories/zone.ts +5 -6
  53. package/src/server.ts +5 -0
  54. package/src/services/abstract.ts +44 -11
  55. package/src/services/cart-discount.ts +2 -3
  56. package/src/services/cart.test.ts +8 -10
  57. package/src/services/cart.ts +8 -11
  58. package/src/services/category.test.ts +1 -2
  59. package/src/services/category.ts +2 -3
  60. package/src/services/channel.ts +2 -3
  61. package/src/services/custom-object.test.ts +5 -5
  62. package/src/services/custom-object.ts +2 -3
  63. package/src/services/customer-group.ts +2 -3
  64. package/src/services/customer.test.ts +136 -0
  65. package/src/services/customer.ts +2 -3
  66. package/src/services/discount-code.ts +2 -3
  67. package/src/services/extension.ts +2 -3
  68. package/src/services/index.ts +74 -0
  69. package/src/services/inventory-entry.test.ts +8 -12
  70. package/src/services/inventory-entry.ts +2 -3
  71. package/src/services/my-cart.ts +3 -4
  72. package/src/services/my-customer.ts +2 -3
  73. package/src/services/my-order.ts +3 -4
  74. package/src/services/my-payment.ts +2 -3
  75. package/src/services/order.test.ts +4 -6
  76. package/src/services/order.ts +2 -3
  77. package/src/services/payment.ts +2 -3
  78. package/src/services/product-discount.ts +2 -3
  79. package/src/services/product-projection.test.ts +76 -8
  80. package/src/services/product-projection.ts +2 -3
  81. package/src/services/product-type.ts +2 -3
  82. package/src/services/product.test.ts +199 -89
  83. package/src/services/product.ts +2 -3
  84. package/src/services/project.ts +3 -3
  85. package/src/services/shipping-method.ts +2 -3
  86. package/src/services/shopping-list.ts +2 -3
  87. package/src/services/state.ts +2 -3
  88. package/src/services/store.test.ts +11 -2
  89. package/src/services/store.ts +2 -3
  90. package/src/services/subscription.ts +2 -3
  91. package/src/services/tax-category.ts +2 -3
  92. package/src/services/type.ts +2 -3
  93. package/src/services/zone.ts +2 -3
  94. package/src/storage.ts +23 -30
  95. package/src/types.ts +46 -6
@@ -36,7 +36,7 @@ export class ChannelRepository extends AbstractResourceRepository {
36
36
  this._storage
37
37
  ),
38
38
  }
39
- this.save(context, resource)
39
+ this.saveNew(context, resource)
40
40
  return resource
41
41
  }
42
42
 
@@ -1,12 +1,14 @@
1
1
  import {
2
2
  CustomObject,
3
3
  CustomObjectDraft,
4
+ InvalidOperationError,
4
5
  ReferenceTypeId,
5
6
  } from '@commercetools/platform-sdk'
6
7
  import { checkConcurrentModification } from './errors'
7
8
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
8
9
  import { Writable } from '../types'
9
- import { getBaseResourceProperties } from '../helpers'
10
+ import { cloneObject, getBaseResourceProperties } from '../helpers'
11
+ import { CommercetoolsError } from '../exceptions'
10
12
 
11
13
  export class CustomObjectRepository extends AbstractResourceRepository {
12
14
  getTypeId(): ReferenceTypeId {
@@ -21,37 +23,46 @@ export class CustomObjectRepository extends AbstractResourceRepository {
21
23
  context,
22
24
  draft.container,
23
25
  draft.key
24
- )
26
+ ) as Writable<CustomObject | undefined>
25
27
 
26
- const baseProperties = getBaseResourceProperties()
27
28
  if (current) {
28
- baseProperties.id = current.id
29
-
30
- if (!draft.version) {
29
+ // Only check version if it is passed in the draft
30
+ if (draft.version) {
31
+ checkConcurrentModification(current.version, draft.version, current.id)
32
+ } else {
31
33
  draft.version = current.version
32
34
  }
33
35
 
34
- checkConcurrentModification(current, draft.version)
35
- if (draft.value === current.value) {
36
- return current
36
+ if (draft.value !== current.value) {
37
+ const updated = cloneObject(current)
38
+ updated.value = draft.value
39
+ updated.version += 1
40
+ this.saveUpdate(context, draft.version, updated)
41
+ return updated
37
42
  }
38
-
39
- baseProperties.version = current.version
43
+ return current
40
44
  } else {
45
+ // If the resource is new the only valid version is 0
41
46
  if (draft.version) {
42
- baseProperties.version = draft.version
47
+ throw new CommercetoolsError<InvalidOperationError>(
48
+ {
49
+ code: 'InvalidOperation',
50
+ message: 'version on create must be 0',
51
+ },
52
+ 400
53
+ )
54
+ }
55
+ const baseProperties = getBaseResourceProperties()
56
+ const resource: CustomObject = {
57
+ ...baseProperties,
58
+ container: draft.container,
59
+ key: draft.key,
60
+ value: draft.value,
43
61
  }
44
- }
45
62
 
46
- const resource: CustomObject = {
47
- ...baseProperties,
48
- container: draft.container,
49
- key: draft.key,
50
- value: draft.value,
63
+ this.saveNew(context, resource)
64
+ return resource
51
65
  }
52
-
53
- this.save(context, resource)
54
- return resource
55
66
  }
56
67
 
57
68
  getWithContainerAndKey(
@@ -63,6 +74,8 @@ export class CustomObjectRepository extends AbstractResourceRepository {
63
74
  context.projectKey,
64
75
  this.getTypeId()
65
76
  ) as Array<CustomObject>
66
- return items.find(item => item.container === container && item.key === key)
77
+ return items.find(
78
+ (item) => item.container === container && item.key === key
79
+ )
67
80
  }
68
81
  }
@@ -27,7 +27,7 @@ export class CustomerGroupRepository extends AbstractResourceRepository {
27
27
  this._storage
28
28
  ),
29
29
  }
30
- this.save(context, resource)
30
+ this.saveNew(context, resource)
31
31
  return resource
32
32
  }
33
33
 
@@ -2,11 +2,15 @@ import {
2
2
  Customer,
3
3
  CustomerChangeEmailAction,
4
4
  CustomerDraft,
5
+ CustomerSetAuthenticationModeAction,
6
+ InvalidInputError,
7
+ InvalidJsonInputError,
5
8
  ReferenceTypeId,
6
9
  } from '@commercetools/platform-sdk'
7
10
  import { Writable } from 'types'
8
11
  import { getBaseResourceProperties } from '../helpers'
9
12
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
13
+ import { CommercetoolsError } from '../exceptions'
10
14
 
11
15
  export class CustomerRepository extends AbstractResourceRepository {
12
16
  getTypeId(): ReferenceTypeId {
@@ -23,7 +27,7 @@ export class CustomerRepository extends AbstractResourceRepository {
23
27
  isEmailVerified: draft.isEmailVerified || false,
24
28
  addresses: [],
25
29
  }
26
- this.save(context, resource)
30
+ this.saveNew(context, resource)
27
31
  return resource
28
32
  }
29
33
 
@@ -48,5 +52,39 @@ export class CustomerRepository extends AbstractResourceRepository {
48
52
  ) => {
49
53
  resource.email = email
50
54
  },
55
+ setAuthenticationMode: (
56
+ _context: RepositoryContext,
57
+ resource: Writable<Customer>,
58
+ { authMode, password }: CustomerSetAuthenticationModeAction
59
+ ) => {
60
+ if (resource.authenticationMode === authMode) {
61
+ throw new CommercetoolsError<InvalidInputError>(
62
+ {
63
+ code: 'InvalidInput',
64
+ message: `The customer is already using the '${resource.authenticationMode}' authentication mode.`,
65
+ },
66
+ 400
67
+ )
68
+ }
69
+ resource.authenticationMode = authMode
70
+ if (authMode === 'ExternalAuth') {
71
+ delete resource.password
72
+ return
73
+ }
74
+ if (authMode === 'Password') {
75
+ resource.password = password
76
+ ? Buffer.from(password).toString('base64')
77
+ : undefined
78
+ return
79
+ }
80
+ throw new CommercetoolsError<InvalidJsonInputError>(
81
+ {
82
+ code: 'InvalidJsonInput',
83
+ message: 'Request body does not contain valid JSON.',
84
+ detailedErrorMessage: `actions -> authMode: Invalid enum value: '${authMode}'. Expected one of: 'Password','ExternalAuth'`,
85
+ },
86
+ 400
87
+ )
88
+ },
51
89
  }
52
90
  }
@@ -54,7 +54,7 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
54
54
  this._storage
55
55
  ),
56
56
  }
57
- this.save(context, resource)
57
+ this.saveNew(context, resource)
58
58
  return resource
59
59
  }
60
60
 
@@ -1,24 +1,22 @@
1
1
  import {
2
- BaseResource,
3
2
  ConcurrentModificationError,
4
- Project,
5
3
  } from '@commercetools/platform-sdk'
6
4
  import { CommercetoolsError } from '../exceptions'
7
5
 
8
6
  export const checkConcurrentModification = (
9
- resource: BaseResource | Project,
10
- expectedVersion: number
7
+ currentVersion: number,
8
+ expectedVersion: number,
9
+ identifier: string
11
10
  ) => {
12
- if (resource.version === expectedVersion) return
13
-
14
- const identifier = (resource as BaseResource).id
15
- ? (resource as BaseResource).id
16
- : (resource as Project).key
11
+ if (currentVersion === expectedVersion) return
12
+ console.error(
13
+ `Object ${identifier} has a different version than expected. Expected: ${expectedVersion} - Actual: ${currentVersion}.`
14
+ )
17
15
 
18
16
  throw new CommercetoolsError<ConcurrentModificationError>(
19
17
  {
20
- message: `Object ${identifier} has a different version than expected. Expected: ${expectedVersion} - Actual: ${resource.version}.`,
21
- currentVersion: resource.version,
18
+ message: `Object ${identifier} has a different version than expected. Expected: ${expectedVersion} - Actual: ${currentVersion}.`,
19
+ currentVersion: currentVersion,
22
20
  code: 'ConcurrentModification',
23
21
  },
24
22
  409
@@ -8,9 +8,9 @@ import {
8
8
  ExtensionUpdateAction,
9
9
  ReferenceTypeId,
10
10
  } from '@commercetools/platform-sdk'
11
- import { Writable } from '../types'
11
+ import { Resource, Writable } from '../types'
12
12
  import { getBaseResourceProperties } from '../helpers'
13
- import { AbstractResourceRepository, GetParams, RepositoryContext } from './abstract'
13
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
14
14
  import { maskSecretValue } from '../lib/masking'
15
15
 
16
16
  export class ExtensionRepository extends AbstractResourceRepository {
@@ -18,17 +18,19 @@ export class ExtensionRepository extends AbstractResourceRepository {
18
18
  return 'extension'
19
19
  }
20
20
 
21
- postProcessResource(resource: Extension) {
21
+ postProcessResource<T extends Resource>(resource: T): T {
22
22
  if (resource) {
23
- if (resource.destination.type === "HTTP" &&
24
- resource.destination.authentication?.type === "AuthorizationHeader"
23
+ const extension = resource as Extension
24
+ if (
25
+ extension.destination.type === 'HTTP' &&
26
+ extension.destination.authentication?.type === 'AuthorizationHeader'
25
27
  ) {
26
28
  return maskSecretValue(
27
- resource, 'destination.authentication.headerValue')
28
- }
29
- else if (resource.destination.type == "AWSLambda") {
30
- return maskSecretValue(
31
- resource, 'destination.accessSecret')
29
+ extension,
30
+ 'destination.authentication.headerValue'
31
+ ) as T
32
+ } else if (extension.destination.type == 'AWSLambda') {
33
+ return maskSecretValue(resource, 'destination.accessSecret')
32
34
  }
33
35
  }
34
36
  return resource
@@ -42,7 +44,7 @@ export class ExtensionRepository extends AbstractResourceRepository {
42
44
  destination: draft.destination,
43
45
  triggers: draft.triggers,
44
46
  }
45
- this.save(context, resource)
47
+ this.saveNew(context, resource)
46
48
  return resource
47
49
  }
48
50
 
@@ -67,15 +67,12 @@ export const createCustomFields = (
67
67
  }
68
68
  }
69
69
 
70
- export const createPrice = (draft: PriceDraft): Price => {
71
- return {
72
- id: uuidv4(),
73
- value: createTypedMoney(draft.value),
74
- }
75
- }
70
+ export const createPrice = (draft: PriceDraft): Price => ({
71
+ id: uuidv4(),
72
+ value: createTypedMoney(draft.value),
73
+ })
76
74
 
77
75
  export const createTypedMoney = (value: Money): TypedMoney => {
78
-
79
76
  // Taken from https://docs.adyen.com/development-resources/currency-codes
80
77
  let fractionDigits = 2
81
78
  switch (value.currencyCode.toUpperCase()) {
@@ -175,9 +172,7 @@ export const getReferenceFromResourceIdentifier = <T extends Reference>(
175
172
  } as unknown as T
176
173
  }
177
174
 
178
- export const getRepositoryContext = (request: Request): RepositoryContext => {
179
- return {
180
- projectKey: request.params.projectKey,
181
- storeKey: request.params.storeKey,
182
- }
183
- }
175
+ export const getRepositoryContext = (request: Request): RepositoryContext => ({
176
+ projectKey: request.params.projectKey,
177
+ storeKey: request.params.storeKey,
178
+ })
@@ -0,0 +1,59 @@
1
+ import { AbstractStorage } from '../storage'
2
+ import { CartRepository } from './cart'
3
+ import { CartDiscountRepository } from './cart-discount'
4
+ import { CategoryRepository } from './category'
5
+ import { ChannelRepository } from './channel'
6
+ import { CustomObjectRepository } from './custom-object'
7
+ import { CustomerRepository } from './customer'
8
+ import { CustomerGroupRepository } from './customer-group'
9
+ import { DiscountCodeRepository } from './discount-code'
10
+ import { ExtensionRepository } from './extension'
11
+ import { InventoryEntryRepository } from './inventory-entry'
12
+ import { MyOrderRepository } from './my-order'
13
+ import { OrderRepository } from './order'
14
+ import { PaymentRepository } from './payment'
15
+ import { ProductRepository } from './product'
16
+ import { ProductDiscountRepository } from './product-discount'
17
+ import { ProductProjectionRepository } from './product-projection'
18
+ import { ProductTypeRepository } from './product-type'
19
+ import { ProjectRepository } from './project'
20
+ import { ShippingMethodRepository } from './shipping-method'
21
+ import { ShoppingListRepository } from './shopping-list'
22
+ import { StateRepository } from './state'
23
+ import { StoreRepository } from './store'
24
+ import { SubscriptionRepository } from './subscription'
25
+ import { TaxCategoryRepository } from './tax-category'
26
+ import { TypeRepository } from './type'
27
+ import { ZoneRepository } from './zone'
28
+
29
+ export const createRepositories = (storage: AbstractStorage) => ({
30
+ category: new CategoryRepository(storage),
31
+ cart: new CartRepository(storage),
32
+ 'cart-discount': new CartDiscountRepository(storage),
33
+ customer: new CustomerRepository(storage),
34
+ channel: new ChannelRepository(storage),
35
+ 'customer-group': new CustomerGroupRepository(storage),
36
+ 'discount-code': new DiscountCodeRepository(storage),
37
+ extension: new ExtensionRepository(storage),
38
+ 'inventory-entry': new InventoryEntryRepository(storage),
39
+ 'key-value-document': new CustomObjectRepository(storage),
40
+ order: new OrderRepository(storage),
41
+ payment: new PaymentRepository(storage),
42
+ 'my-cart': new CartRepository(storage),
43
+ 'my-order': new MyOrderRepository(storage),
44
+ 'my-customer': new CustomerRepository(storage),
45
+ 'my-payment': new PaymentRepository(storage),
46
+ 'shipping-method': new ShippingMethodRepository(storage),
47
+ 'product-type': new ProductTypeRepository(storage),
48
+ product: new ProductRepository(storage),
49
+ project: new ProjectRepository(storage),
50
+ 'product-discount': new ProductDiscountRepository(storage),
51
+ 'product-projection': new ProductProjectionRepository(storage),
52
+ 'shopping-list': new ShoppingListRepository(storage),
53
+ state: new StateRepository(storage),
54
+ store: new StoreRepository(storage),
55
+ subscription: new SubscriptionRepository(storage),
56
+ 'tax-category': new TaxCategoryRepository(storage),
57
+ type: new TypeRepository(storage),
58
+ zone: new ZoneRepository(storage),
59
+ })
@@ -40,7 +40,7 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
40
40
  this._storage
41
41
  ),
42
42
  }
43
- this.save(context, resource)
43
+ this.saveNew(context, resource)
44
44
  return resource
45
45
  }
46
46
 
@@ -93,7 +93,7 @@ export class OrderRepository extends AbstractResourceRepository {
93
93
  : undefined,
94
94
  lastMessageSequenceNumber: 0,
95
95
  }
96
- this.save(context, resource)
96
+ this.saveNew(context, resource)
97
97
  return resource
98
98
  }
99
99
 
@@ -126,11 +126,11 @@ export class OrderRepository extends AbstractResourceRepository {
126
126
  syncInfo: [],
127
127
 
128
128
  lineItems:
129
- draft.lineItems?.map(item =>
129
+ draft.lineItems?.map((item) =>
130
130
  this.lineItemFromImportDraft.bind(this)(context, item)
131
131
  ) || [],
132
132
  customLineItems:
133
- draft.customLineItems?.map(item =>
133
+ draft.customLineItems?.map((item) =>
134
134
  this.customLineItemFromImportDraft.bind(this)(context, item)
135
135
  ) || [],
136
136
 
@@ -140,7 +140,7 @@ export class OrderRepository extends AbstractResourceRepository {
140
140
  fractionDigits: 2,
141
141
  },
142
142
  }
143
- this.save(context, resource)
143
+ this.saveNew(context, resource)
144
144
  return resource
145
145
  }
146
146
 
@@ -157,7 +157,7 @@ export class OrderRepository extends AbstractResourceRepository {
157
157
  sku: draft.variant.sku,
158
158
  }
159
159
 
160
- var items = this._storage.query(context.projectKey, 'product', {
160
+ const items = this._storage.query(context.projectKey, 'product', {
161
161
  where: [
162
162
  `masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`,
163
163
  ],
@@ -175,7 +175,7 @@ export class OrderRepository extends AbstractResourceRepository {
175
175
  variant = product.masterData.current.masterVariant
176
176
  } else {
177
177
  variant = product.masterData.current.variants.find(
178
- v => v.sku === draft.variant.sku
178
+ (v) => v.sku === draft.variant.sku
179
179
  )
180
180
  }
181
181
  if (!variant) {
@@ -44,11 +44,11 @@ export class PaymentRepository extends AbstractResourceRepository {
44
44
  : undefined,
45
45
  }
46
46
  : {},
47
- transactions: (draft.transactions || []).map(t =>
47
+ transactions: (draft.transactions || []).map((t) =>
48
48
  this.transactionFromTransactionDraft(t, context)
49
49
  ),
50
50
  interfaceInteractions: (draft.interfaceInteractions || []).map(
51
- interaction =>
51
+ (interaction) =>
52
52
  createCustomFields(interaction, context.projectKey, this._storage)!
53
53
  ),
54
54
  custom: createCustomFields(
@@ -58,7 +58,7 @@ export class PaymentRepository extends AbstractResourceRepository {
58
58
  ),
59
59
  }
60
60
 
61
- this.save(context, resource)
61
+ this.saveNew(context, resource)
62
62
  return resource
63
63
  }
64
64
 
@@ -46,7 +46,7 @@ export class ProductDiscountRepository extends AbstractResourceRepository {
46
46
  validUntil: draft.validUntil,
47
47
  references: [],
48
48
  }
49
- this.save(context, resource)
49
+ this.saveNew(context, resource)
50
50
  return resource
51
51
  }
52
52
 
@@ -43,6 +43,7 @@ export class ProductProjectionRepository extends AbstractResourceRepository {
43
43
  offset: query.offset ? Number(query.offset) : undefined,
44
44
  limit: query.limit ? Number(query.limit) : undefined,
45
45
  expand: QueryParamsAsArray(query.expand),
46
+ staged: query.staged === 'true',
46
47
  })
47
48
 
48
49
  return results
@@ -28,30 +28,28 @@ export class ProductTypeRepository extends AbstractResourceRepository {
28
28
  key: draft.key,
29
29
  name: draft.name,
30
30
  description: draft.description,
31
- attributes: (draft.attributes ?? []).map(a =>
31
+ attributes: (draft.attributes ?? []).map((a) =>
32
32
  this.attributeDefinitionFromAttributeDefinitionDraft(context, a)
33
33
  ),
34
34
  }
35
35
 
36
- this.save(context, resource)
36
+ this.saveNew(context, resource)
37
37
  return resource
38
38
  }
39
39
 
40
40
  attributeDefinitionFromAttributeDefinitionDraft = (
41
41
  _context: RepositoryContext,
42
42
  draft: AttributeDefinitionDraft
43
- ): AttributeDefinition => {
44
- return {
45
- ...draft,
46
- attributeConstraint: draft.attributeConstraint ?? 'None',
47
- inputHint: draft.inputHint ?? 'SingleLine',
48
- inputTip:
49
- draft.inputTip && Object.keys(draft.inputTip).length > 0
50
- ? draft.inputTip
51
- : undefined,
52
- isSearchable: draft.isSearchable ?? true,
53
- }
54
- }
43
+ ): AttributeDefinition => ({
44
+ ...draft,
45
+ attributeConstraint: draft.attributeConstraint ?? 'None',
46
+ inputHint: draft.inputHint ?? 'SingleLine',
47
+ inputTip:
48
+ draft.inputTip && Object.keys(draft.inputTip).length > 0
49
+ ? draft.inputTip
50
+ : undefined,
51
+ isSearchable: draft.isSearchable ?? true,
52
+ })
55
53
 
56
54
  getWithKey(context: RepositoryContext, key: string): ProductType | undefined {
57
55
  const result = this._storage.query(context.projectKey, this.getTypeId(), {
@@ -90,7 +88,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
90
88
  const updateAttributeType = (type: Writable<AttributeType>) => {
91
89
  switch (type.name) {
92
90
  case 'lenum':
93
- type.values.forEach(v => {
91
+ type.values.forEach((v) => {
94
92
  if (v.key === newValue.key) {
95
93
  v.label = newValue.label
96
94
  }
@@ -102,7 +100,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
102
100
  }
103
101
  }
104
102
 
105
- resource.attributes?.forEach(value => {
103
+ resource.attributes?.forEach((value) => {
106
104
  if (value.name === attributeName) {
107
105
  updateAttributeType(value.type)
108
106
  }
@@ -113,7 +111,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
113
111
  resource: Writable<ProductType>,
114
112
  { attributeName, label }: ProductTypeChangeLabelAction
115
113
  ) => {
116
- resource.attributes?.forEach(value => {
114
+ resource.attributes?.forEach((value) => {
117
115
  if (value.name === attributeName) {
118
116
  value.label = label
119
117
  }
@@ -133,11 +131,13 @@ export class ProductTypeRepository extends AbstractResourceRepository {
133
131
  resource: Writable<ProductType>,
134
132
  { attributes }: ProductTypeChangeAttributeOrderAction
135
133
  ) => {
136
- const attrs = new Map(resource.attributes?.map(item => [item.name, item]))
134
+ const attrs = new Map(
135
+ resource.attributes?.map((item) => [item.name, item])
136
+ )
137
137
  const result: AttributeDefinition[] = []
138
138
  let current = resource.attributes
139
139
 
140
- attributes.forEach(iAttr => {
140
+ attributes.forEach((iAttr) => {
141
141
  const attr = attrs.get(iAttr.name)
142
142
  if (attr === undefined) {
143
143
  throw new Error('New attr')
@@ -145,9 +145,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
145
145
  result.push(attr)
146
146
 
147
147
  // Remove from current items
148
- current = current?.filter(f => {
149
- return f.name !== iAttr.name
150
- })
148
+ current = current?.filter((f) => f.name !== iAttr.name)
151
149
  })
152
150
 
153
151
  resource.attributes = result
@@ -162,30 +160,27 @@ export class ProductTypeRepository extends AbstractResourceRepository {
162
160
  resource: Writable<ProductType>,
163
161
  { name }: ProductTypeRemoveAttributeDefinitionAction
164
162
  ) => {
165
- resource.attributes = resource.attributes?.filter(f => {
166
- return f.name !== name
167
- })
163
+ resource.attributes = resource.attributes?.filter((f) => f.name !== name)
168
164
  },
169
165
  removeEnumValues: (
170
166
  context: RepositoryContext,
171
167
  resource: Writable<ProductType>,
172
168
  { attributeName, keys }: ProductTypeRemoveEnumValuesAction
173
169
  ) => {
174
- resource.attributes?.forEach(attr => {
170
+ resource.attributes?.forEach((attr) => {
175
171
  if (attr.name == attributeName) {
176
172
  if (attr.type.name == 'enum') {
177
- attr.type.values = attr.type.values.filter(v => {
178
- return !keys.includes(v.key)
179
- })
173
+ attr.type.values = attr.type.values.filter(
174
+ (v) => !keys.includes(v.key)
175
+ )
180
176
  }
181
177
 
182
178
  if (attr.type.name == 'set') {
183
179
  if (attr.type.elementType.name == 'enum') {
184
- attr.type.elementType.values = attr.type.elementType.values.filter(
185
- v => {
186
- return !keys.includes(v.key)
187
- }
188
- )
180
+ attr.type.elementType.values =
181
+ attr.type.elementType.values.filter(
182
+ (v) => !keys.includes(v.key)
183
+ )
189
184
  }
190
185
  }
191
186
  }