@labdigital/commercetools-mock 0.5.11 → 0.5.14

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 (115) hide show
  1. package/README.md +30 -12
  2. package/dist/commercetools-mock.cjs.development.js +3190 -1574
  3. package/dist/commercetools-mock.cjs.development.js.map +1 -1
  4. package/dist/commercetools-mock.cjs.production.min.js +1 -1
  5. package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
  6. package/dist/commercetools-mock.esm.js +3190 -1574
  7. package/dist/commercetools-mock.esm.js.map +1 -1
  8. package/dist/ctMock.d.ts +1 -0
  9. package/dist/lib/filterParser.d.ts +1 -0
  10. package/dist/repositories/abstract.d.ts +11 -7
  11. package/dist/repositories/cart-discount.d.ts +9 -0
  12. package/dist/repositories/cart.d.ts +16 -3
  13. package/dist/repositories/category.d.ts +18 -0
  14. package/dist/repositories/channel.d.ts +6 -0
  15. package/dist/repositories/custom-object.d.ts +2 -2
  16. package/dist/repositories/customer-group.d.ts +11 -0
  17. package/dist/repositories/customer.d.ts +2 -2
  18. package/dist/repositories/discount-code.d.ts +8 -0
  19. package/dist/repositories/errors.d.ts +2 -2
  20. package/dist/repositories/extension.d.ts +8 -0
  21. package/dist/repositories/inventory-entry.d.ts +2 -2
  22. package/dist/repositories/order.d.ts +2 -2
  23. package/dist/repositories/payment.d.ts +4 -3
  24. package/dist/repositories/product-projection.d.ts +10 -0
  25. package/dist/repositories/product-type.d.ts +5 -4
  26. package/dist/repositories/product.d.ts +2 -2
  27. package/dist/repositories/project.d.ts +8 -0
  28. package/dist/repositories/shipping-method.d.ts +7 -5
  29. package/dist/repositories/shopping-list.d.ts +6 -0
  30. package/dist/repositories/state.d.ts +5 -4
  31. package/dist/repositories/store.d.ts +6 -3
  32. package/dist/repositories/subscription.d.ts +6 -0
  33. package/dist/repositories/tax-category.d.ts +6 -5
  34. package/dist/repositories/type.d.ts +5 -3
  35. package/dist/repositories/zone.d.ts +8 -0
  36. package/dist/services/abstract.d.ts +4 -3
  37. package/dist/services/cart-discount.d.ts +9 -0
  38. package/dist/services/category.d.ts +9 -0
  39. package/dist/services/channel.d.ts +9 -0
  40. package/dist/services/custom-object.d.ts +2 -0
  41. package/dist/services/customer-group.d.ts +9 -0
  42. package/dist/services/discount-code.d.ts +9 -0
  43. package/dist/services/extension.d.ts +9 -0
  44. package/dist/services/my-cart.d.ts +11 -0
  45. package/dist/services/product-projection.d.ts +11 -0
  46. package/dist/services/project.d.ts +11 -0
  47. package/dist/services/shopping-list.d.ts +9 -0
  48. package/dist/services/subscription.d.ts +9 -0
  49. package/dist/services/zone.d.ts +9 -0
  50. package/dist/storage.d.ts +27 -16
  51. package/dist/types.d.ts +9 -4
  52. package/package.json +20 -20
  53. package/src/ctMock.ts +49 -15
  54. package/src/lib/filterParser.test.ts +15 -0
  55. package/src/lib/filterParser.ts +17 -0
  56. package/src/oauth/server.ts +3 -1
  57. package/src/repositories/abstract.ts +49 -34
  58. package/src/repositories/cart-discount.ts +140 -0
  59. package/src/repositories/cart.ts +247 -3
  60. package/src/repositories/category.ts +140 -0
  61. package/src/repositories/channel.ts +23 -0
  62. package/src/repositories/custom-object.ts +5 -2
  63. package/src/repositories/customer-group.ts +42 -0
  64. package/src/repositories/customer.ts +2 -2
  65. package/src/repositories/discount-code.ts +143 -0
  66. package/src/repositories/errors.ts +7 -2
  67. package/src/repositories/extension.ts +65 -0
  68. package/src/repositories/inventory-entry.ts +2 -2
  69. package/src/repositories/order.ts +2 -2
  70. package/src/repositories/payment.ts +10 -6
  71. package/src/repositories/product-projection.ts +74 -0
  72. package/src/repositories/product-type.ts +57 -4
  73. package/src/repositories/product.ts +4 -2
  74. package/src/repositories/project.ts +150 -0
  75. package/src/repositories/shipping-method.ts +149 -13
  76. package/src/repositories/shopping-list.ts +50 -0
  77. package/src/repositories/state.ts +48 -4
  78. package/src/repositories/store.ts +69 -4
  79. package/src/repositories/subscription.ts +50 -0
  80. package/src/repositories/tax-category.ts +80 -6
  81. package/src/repositories/type.ts +133 -3
  82. package/src/repositories/zone.ts +77 -0
  83. package/src/server.ts +6 -1
  84. package/src/services/abstract.ts +26 -18
  85. package/src/services/cart-discount.ts +17 -0
  86. package/src/services/cart.test.ts +314 -3
  87. package/src/services/category.test.ts +37 -0
  88. package/src/services/category.ts +17 -0
  89. package/src/services/channel.ts +17 -0
  90. package/src/services/custom-object.test.ts +53 -3
  91. package/src/services/custom-object.ts +30 -0
  92. package/src/services/customer-group.ts +17 -0
  93. package/src/services/discount-code.ts +17 -0
  94. package/src/services/extension.ts +17 -0
  95. package/src/services/inventory-entry.test.ts +3 -3
  96. package/src/services/my-cart.test.ts +93 -0
  97. package/src/services/my-cart.ts +44 -0
  98. package/src/services/my-payment.test.ts +2 -2
  99. package/src/services/my-payment.ts +1 -1
  100. package/src/services/order.test.ts +4 -4
  101. package/src/services/payment.test.ts +2 -2
  102. package/src/services/product-projection.test.ts +105 -0
  103. package/src/services/product-projection.ts +29 -0
  104. package/src/services/product-type.test.ts +2 -2
  105. package/src/services/product.test.ts +6 -2
  106. package/src/services/project.ts +42 -0
  107. package/src/services/shipping-method.test.ts +3 -3
  108. package/src/services/shopping-list.ts +17 -0
  109. package/src/services/state.test.ts +2 -2
  110. package/src/services/store.test.ts +60 -0
  111. package/src/services/subscription.ts +17 -0
  112. package/src/services/tax-category.test.ts +3 -3
  113. package/src/services/zone.ts +17 -0
  114. package/src/storage.ts +127 -31
  115. package/src/types.ts +9 -3
@@ -0,0 +1,65 @@
1
+ import {
2
+ Extension,
3
+ ExtensionChangeDestinationAction,
4
+ ExtensionChangeTriggersAction,
5
+ ExtensionDraft,
6
+ ExtensionSetKeyAction,
7
+ ExtensionSetTimeoutInMsAction,
8
+ ExtensionUpdateAction,
9
+ ReferenceTypeId,
10
+ } from '@commercetools/platform-sdk'
11
+ import { Writable } from '../types'
12
+ import { getBaseResourceProperties } from '../helpers'
13
+ import { AbstractResourceRepository } from './abstract'
14
+
15
+ export class ExtensionRepository extends AbstractResourceRepository {
16
+ getTypeId(): ReferenceTypeId {
17
+ return 'extension'
18
+ }
19
+
20
+ create(projectKey: string, draft: ExtensionDraft): Extension {
21
+ const resource: Extension = {
22
+ ...getBaseResourceProperties(),
23
+ key: draft.key,
24
+ timeoutInMs: draft.timeoutInMs,
25
+ destination: draft.destination,
26
+ triggers: draft.triggers,
27
+ }
28
+ this.save(projectKey, resource)
29
+ return resource
30
+ }
31
+
32
+ actions: Record<
33
+ ExtensionUpdateAction['action'],
34
+ (projectKey: string, resource: Writable<Extension>, action: any) => void
35
+ > = {
36
+ setKey: (
37
+ projectKey: string,
38
+ resource: Writable<Extension>,
39
+ { key }: ExtensionSetKeyAction
40
+ ) => {
41
+ resource.key = key
42
+ },
43
+ setTimeoutInMs: (
44
+ projectKey: string,
45
+ resource: Writable<Extension>,
46
+ { timeoutInMs }: ExtensionSetTimeoutInMsAction
47
+ ) => {
48
+ resource.timeoutInMs = timeoutInMs
49
+ },
50
+ changeTriggers: (
51
+ projectKey: string,
52
+ resource: Writable<Extension>,
53
+ { triggers }: ExtensionChangeTriggersAction
54
+ ) => {
55
+ resource.triggers = triggers
56
+ },
57
+ changeDestination: (
58
+ projectKey: string,
59
+ resource: Writable<Extension>,
60
+ { destination }: ExtensionChangeDestinationAction
61
+ ) => {
62
+ resource.destination = destination
63
+ },
64
+ }
65
+ }
@@ -9,11 +9,11 @@ import {
9
9
  ReferenceTypeId,
10
10
  } from '@commercetools/platform-sdk'
11
11
  import { getBaseResourceProperties } from '../helpers'
12
- import AbstractRepository from './abstract'
12
+ import { AbstractResourceRepository } from './abstract'
13
13
  import { createCustomFields } from './helpers'
14
14
  import { Writable } from '../types'
15
15
 
16
- export class InventoryEntryRepository extends AbstractRepository {
16
+ export class InventoryEntryRepository extends AbstractResourceRepository {
17
17
  getTypeId(): ReferenceTypeId {
18
18
  return 'inventory-entry'
19
19
  }
@@ -26,7 +26,7 @@ import {
26
26
  ReferenceTypeId,
27
27
  Store,
28
28
  } from '@commercetools/platform-sdk'
29
- import AbstractRepository, { QueryParams } from './abstract'
29
+ import { AbstractResourceRepository, QueryParams } from './abstract'
30
30
  import {
31
31
  createCustomFields,
32
32
  createPrice,
@@ -37,7 +37,7 @@ import { Writable } from '../types'
37
37
  import { getBaseResourceProperties } from '../helpers'
38
38
  import { CommercetoolsError } from '../exceptions'
39
39
 
40
- export class OrderRepository extends AbstractRepository {
40
+ export class OrderRepository extends AbstractResourceRepository {
41
41
  getTypeId(): ReferenceTypeId {
42
42
  return 'order'
43
43
  }
@@ -8,7 +8,7 @@ import {
8
8
  StateReference,
9
9
  TransactionDraft,
10
10
  } from '@commercetools/platform-sdk'
11
- import AbstractRepository from './abstract'
11
+ import { AbstractResourceRepository } from './abstract'
12
12
  import {
13
13
  createCustomFields,
14
14
  createTypedMoney,
@@ -18,7 +18,7 @@ import { getBaseResourceProperties } from '../helpers'
18
18
  import { v4 as uuidv4 } from 'uuid'
19
19
  import { Writable } from '../types'
20
20
 
21
- export class PaymentRepository extends AbstractRepository {
21
+ export class PaymentRepository extends AbstractResourceRepository {
22
22
  getTypeId(): ReferenceTypeId {
23
23
  return 'payment'
24
24
  }
@@ -40,8 +40,8 @@ export class PaymentRepository extends AbstractRepository {
40
40
  : undefined,
41
41
  }
42
42
  : {},
43
- transactions: (draft.transactions || []).map(
44
- this.transactionFromTransactionDraft
43
+ transactions: (draft.transactions || []).map(t =>
44
+ this.transactionFromTransactionDraft(t, projectKey)
45
45
  ),
46
46
  interfaceInteractions: (draft.interfaceInteractions || []).map(
47
47
  interaction =>
@@ -54,10 +54,14 @@ export class PaymentRepository extends AbstractRepository {
54
54
  return resource
55
55
  }
56
56
 
57
- transactionFromTransactionDraft = (draft: TransactionDraft) => ({
57
+ transactionFromTransactionDraft = (
58
+ draft: TransactionDraft,
59
+ projectKey: string
60
+ ) => ({
58
61
  ...draft,
59
62
  id: uuidv4(),
60
63
  amount: createTypedMoney(draft.amount),
64
+ custom: createCustomFields(draft.custom, projectKey, this._storage),
61
65
  })
62
66
 
63
67
  actions = {
@@ -104,7 +108,7 @@ export class PaymentRepository extends AbstractRepository {
104
108
  ) => {
105
109
  resource.transactions = [
106
110
  ...resource.transactions,
107
- this.transactionFromTransactionDraft(transaction),
111
+ this.transactionFromTransactionDraft(transaction, projectKey),
108
112
  ]
109
113
  },
110
114
  // addInterfaceInteraction: () => {},
@@ -0,0 +1,74 @@
1
+ import { ParsedQs } from 'qs'
2
+ import {
3
+ ProductDraft,
4
+ ProductProjection,
5
+ ProductVariant,
6
+ ProductVariantDraft,
7
+ } from '@commercetools/platform-sdk'
8
+ import { getBaseResourceProperties } from '../helpers'
9
+ import { AbstractResourceRepository } from './abstract'
10
+ import { RepositoryTypes } from '../types'
11
+ import { parseFilterExpression } from '../lib/filterParser'
12
+
13
+ export class ProductProjectionRepository extends AbstractResourceRepository {
14
+ getTypeId(): RepositoryTypes {
15
+ return 'product-projection'
16
+ }
17
+
18
+ create(projectKey: string, draft: ProductDraft): ProductProjection {
19
+ if (!draft.masterVariant) {
20
+ throw new Error(
21
+ `must provider mastervariant for product projection with key ${draft.key}`
22
+ )
23
+ }
24
+
25
+ if (!draft.productType.id) {
26
+ throw new Error(
27
+ `must provider product type id for product projection with key ${draft.key}`
28
+ )
29
+ }
30
+
31
+ const resource: ProductProjection = {
32
+ ...getBaseResourceProperties(),
33
+ name: draft.name,
34
+ slug: draft.slug,
35
+ categories: [],
36
+ productType: { ...draft.productType, id: draft.productType.id! },
37
+ masterVariant: variantFromDraft(0, draft.masterVariant!),
38
+ variants:
39
+ draft.variants?.map((variant, index) => {
40
+ return variantFromDraft(index + 1, variant)
41
+ }) ?? [],
42
+
43
+ // @ts-ignore
44
+ searchKeywords: draft.searchKeywords,
45
+ }
46
+
47
+ this.save(projectKey, resource)
48
+
49
+ return resource
50
+ }
51
+
52
+ search(projectKey: string, query: ParsedQs) {
53
+ const wherePredicate = parseFilterExpression(query.filter as any)
54
+
55
+ const results = this._storage.query(projectKey, this.getTypeId(), {
56
+ where: wherePredicate,
57
+ }) //TODO: this is a partial implementation, but I don't really have the time to implement an actual search API right now
58
+
59
+ return results
60
+ }
61
+
62
+ actions = {}
63
+ }
64
+
65
+ const variantFromDraft = (
66
+ variantId: number,
67
+ variant: ProductVariantDraft
68
+ ): ProductVariant => {
69
+ return {
70
+ id: variantId,
71
+ sku: variant?.sku,
72
+ attributes: variant?.attributes,
73
+ }
74
+ }
@@ -2,13 +2,18 @@ import { getBaseResourceProperties } from '../helpers'
2
2
  import {
3
3
  AttributeDefinition,
4
4
  AttributeDefinitionDraft,
5
+ AttributeType,
5
6
  ProductType,
7
+ ProductTypeChangeLabelAction,
8
+ ProductTypeChangeLocalizedEnumValueLabelAction,
6
9
  ProductTypeDraft,
10
+ ProductTypeUpdateAction,
7
11
  ReferenceTypeId,
8
12
  } from '@commercetools/platform-sdk'
9
- import AbstractRepository from './abstract'
13
+ import { AbstractResourceRepository } from './abstract'
14
+ import { Writable } from 'types'
10
15
 
11
- export class ProductTypeRepository extends AbstractRepository {
16
+ export class ProductTypeRepository extends AbstractResourceRepository {
12
17
  getTypeId(): ReferenceTypeId {
13
18
  return 'product-type'
14
19
  }
@@ -16,7 +21,9 @@ export class ProductTypeRepository extends AbstractRepository {
16
21
  create(projectKey: string, draft: ProductTypeDraft): ProductType {
17
22
  const resource: ProductType = {
18
23
  ...getBaseResourceProperties(),
19
- ...draft,
24
+ key: draft.key,
25
+ name: draft.name,
26
+ description: draft.description,
20
27
  attributes: (draft.attributes ?? []).map(a =>
21
28
  this.attributeDefinitionFromAttributeDefinitionDraft(projectKey, a)
22
29
  ),
@@ -52,5 +59,51 @@ export class ProductTypeRepository extends AbstractRepository {
52
59
  return
53
60
  }
54
61
 
55
- actions = {}
62
+ actions: Partial<
63
+ Record<
64
+ ProductTypeUpdateAction['action'],
65
+ (projectKey: string, resource: Writable<ProductType>, action: any) => void
66
+ >
67
+ > = {
68
+ changeLocalizedEnumValueLabel: (
69
+ projectKey: string,
70
+ resource: Writable<ProductType>,
71
+ {
72
+ attributeName,
73
+ newValue,
74
+ }: ProductTypeChangeLocalizedEnumValueLabelAction
75
+ ) => {
76
+ const updateAttributeType = (type: Writable<AttributeType>) => {
77
+ switch (type.name) {
78
+ case 'lenum':
79
+ type.values.forEach(v => {
80
+ if (v.key == newValue.key) {
81
+ v.label = newValue.label
82
+ }
83
+ })
84
+ return
85
+ case 'set':
86
+ updateAttributeType(type.elementType)
87
+ return
88
+ }
89
+ }
90
+
91
+ resource.attributes?.forEach(value => {
92
+ if (value.name == attributeName) {
93
+ updateAttributeType(value.type)
94
+ }
95
+ })
96
+ },
97
+ changeLabel: (
98
+ projectKey: string,
99
+ resource: Writable<ProductType>,
100
+ { attributeName, label }: ProductTypeChangeLabelAction
101
+ ) => {
102
+ resource.attributes?.forEach(value => {
103
+ if (value.name == attributeName) {
104
+ value.label = label
105
+ }
106
+ })
107
+ },
108
+ }
56
109
  }
@@ -1,4 +1,5 @@
1
1
  import {
2
+ Price,
2
3
  Product,
3
4
  ProductData,
4
5
  ProductDraft,
@@ -9,10 +10,10 @@ import {
9
10
  ReferenceTypeId,
10
11
  } from '@commercetools/platform-sdk'
11
12
  import { getBaseResourceProperties } from '../helpers'
12
- import AbstractRepository from './abstract'
13
+ import { AbstractResourceRepository } from './abstract'
13
14
  import { Writable } from '../types'
14
15
 
15
- export class ProductRepository extends AbstractRepository {
16
+ export class ProductRepository extends AbstractResourceRepository {
16
17
  getTypeId(): ReferenceTypeId {
17
18
  return 'product'
18
19
  }
@@ -207,5 +208,6 @@ const variantFromDraft = (
207
208
  id: variantId,
208
209
  sku: variant?.sku,
209
210
  attributes: variant?.attributes,
211
+ prices: variant?.prices as Price[],
210
212
  }
211
213
  }
@@ -0,0 +1,150 @@
1
+ import {
2
+ Project,
3
+ ProjectChangeCartsConfigurationAction,
4
+ ProjectChangeCountriesAction,
5
+ ProjectChangeCountryTaxRateFallbackEnabledAction,
6
+ ProjectChangeCurrenciesAction,
7
+ ProjectChangeLanguagesAction,
8
+ ProjectChangeMessagesEnabledAction,
9
+ ProjectChangeNameAction,
10
+ ProjectChangeOrderSearchStatusAction,
11
+ ProjectChangeProductSearchIndexingEnabledAction,
12
+ ProjectSetExternalOAuthAction,
13
+ ProjectSetShippingRateInputTypeAction,
14
+ ProjectUpdateAction,
15
+ } from '@commercetools/platform-sdk'
16
+ import { InvalidOperationError } from '@commercetools/platform-sdk'
17
+ import { Writable } from 'types'
18
+ import { checkConcurrentModification } from './errors'
19
+ import { CommercetoolsError } from '../exceptions'
20
+ import { AbstractRepository } from './abstract'
21
+
22
+ export class ProjectRepository extends AbstractRepository {
23
+ get(projectKey: string): Project | null {
24
+ const data = this._storage.getProject(projectKey)
25
+ return this._storage.getProject(projectKey)
26
+ }
27
+
28
+ save(projectKey: string, resource: Project) {
29
+ const current = this.get(projectKey)
30
+
31
+ if (current) {
32
+ checkConcurrentModification(current, resource.version)
33
+ } else {
34
+ if (resource.version !== 0) {
35
+ throw new CommercetoolsError<InvalidOperationError>(
36
+ {
37
+ code: 'InvalidOperation',
38
+ message: 'version on create must be 0',
39
+ },
40
+ 400
41
+ )
42
+ }
43
+ }
44
+
45
+ // @ts-ignore
46
+ resource.version += 1
47
+ this._storage.saveProject(resource)
48
+ }
49
+
50
+ actions: Partial<
51
+ Record<
52
+ ProjectUpdateAction['action'],
53
+ (projectKey: string, resource: Writable<Project>, action: any) => void
54
+ >
55
+ > = {
56
+ changeName: (
57
+ projectKey: string,
58
+ resource: Writable<Project>,
59
+ { name }: ProjectChangeNameAction
60
+ ) => {
61
+ resource.name = name
62
+ },
63
+ changeCurrencies: (
64
+ projectKey: string,
65
+ resource: Writable<Project>,
66
+ { currencies }: ProjectChangeCurrenciesAction
67
+ ) => {
68
+ resource.currencies = currencies
69
+ },
70
+ changeCountries: (
71
+ projectKey: string,
72
+ resource: Writable<Project>,
73
+ { countries }: ProjectChangeCountriesAction
74
+ ) => {
75
+ resource.countries = countries
76
+ },
77
+ changeLanguages: (
78
+ projectKey: string,
79
+ resource: Writable<Project>,
80
+ { languages }: ProjectChangeLanguagesAction
81
+ ) => {
82
+ resource.languages = languages
83
+ },
84
+ changeMessagesEnabled: (
85
+ projectKey: string,
86
+ resource: Writable<Project>,
87
+ { messagesEnabled }: ProjectChangeMessagesEnabledAction
88
+ ) => {
89
+ resource.messages.enabled = messagesEnabled
90
+ },
91
+ changeProductSearchIndexingEnabled: (
92
+ projectKey: string,
93
+ resource: Writable<Project>,
94
+ { enabled }: ProjectChangeProductSearchIndexingEnabledAction
95
+ ) => {
96
+ if (!resource.searchIndexing?.products) {
97
+ throw new Error('Invalid project state')
98
+ }
99
+ resource.searchIndexing.products.status = enabled
100
+ ? 'Activated'
101
+ : 'Deactivated'
102
+ resource.searchIndexing.products.lastModifiedAt = new Date().toISOString()
103
+ },
104
+ changeOrderSearchStatus: (
105
+ projectKey: string,
106
+ resource: Writable<Project>,
107
+ { status }: ProjectChangeOrderSearchStatusAction
108
+ ) => {
109
+ if (!resource.searchIndexing?.orders) {
110
+ throw new Error('Invalid project state')
111
+ }
112
+ resource.searchIndexing.orders.status = status
113
+ resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString()
114
+ },
115
+ setShippingRateInputType: (
116
+ projectKey: string,
117
+ resource: Writable<Project>,
118
+ { shippingRateInputType }: ProjectSetShippingRateInputTypeAction
119
+ ) => {
120
+ resource.shippingRateInputType = shippingRateInputType
121
+ },
122
+ setExternalOAuth: (
123
+ projectKey: string,
124
+ resource: Writable<Project>,
125
+ { externalOAuth }: ProjectSetExternalOAuthAction
126
+ ) => {
127
+ resource.externalOAuth = externalOAuth
128
+ },
129
+ changeCountryTaxRateFallbackEnabled: (
130
+ projectKey: string,
131
+ resource: Writable<Project>,
132
+ {
133
+ countryTaxRateFallbackEnabled,
134
+ }: ProjectChangeCountryTaxRateFallbackEnabledAction
135
+ ) => {
136
+ resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled
137
+ },
138
+ changeCartsConfiguration: (
139
+ projectKey: string,
140
+ resource: Writable<Project>,
141
+ { cartsConfiguration }: ProjectChangeCartsConfigurationAction
142
+ ) => {
143
+ console.log(cartsConfiguration)
144
+ resource.carts = cartsConfiguration || {
145
+ countryTaxRateFallbackEnabled: false,
146
+ deleteDaysAfterLastModification: 90,
147
+ }
148
+ },
149
+ }
150
+ }
@@ -6,14 +6,30 @@ import {
6
6
  import {
7
7
  ReferenceTypeId,
8
8
  ShippingMethod,
9
+ ShippingMethodAddShippingRateAction,
10
+ ShippingMethodAddZoneAction,
11
+ ShippingMethodChangeIsDefaultAction,
12
+ ShippingMethodChangeNameAction,
9
13
  ShippingMethodDraft,
14
+ ShippingMethodRemoveZoneAction,
15
+ ShippingMethodSetDescriptionAction,
16
+ ShippingMethodSetKeyAction,
17
+ ShippingMethodSetLocalizedDescriptionAction,
18
+ ShippingMethodSetPredicateAction,
19
+ ShippingMethodUpdateAction,
20
+ ShippingRate,
21
+ ShippingRateDraft,
10
22
  ZoneRate,
11
23
  ZoneRateDraft,
24
+ ZoneReference,
12
25
  } from '@commercetools/platform-sdk'
13
26
  import { getBaseResourceProperties } from '../helpers'
14
- import AbstractRepository from './abstract'
27
+ import { AbstractResourceRepository } from './abstract'
28
+ import { Writable } from 'types'
29
+ import { _ } from 'ajv'
30
+ import deepEqual from 'deep-equal'
15
31
 
16
- export class ShippingMethodRepository extends AbstractRepository {
32
+ export class ShippingMethodRepository extends AbstractResourceRepository {
17
33
  getTypeId(): ReferenceTypeId {
18
34
  return 'shipping-method'
19
35
  }
@@ -27,8 +43,8 @@ export class ShippingMethodRepository extends AbstractRepository {
27
43
  projectKey,
28
44
  this._storage
29
45
  ),
30
- zoneRates: draft.zoneRates.map(z =>
31
- this.zoneRateFromZoneRateDraft(projectKey, z)
46
+ zoneRates: draft.zoneRates?.map(z =>
47
+ this._transformZoneRateDraft(projectKey, z)
32
48
  ),
33
49
  custom: createCustomFields(draft.custom, projectKey, this._storage),
34
50
  }
@@ -36,23 +52,143 @@ export class ShippingMethodRepository extends AbstractRepository {
36
52
  return resource
37
53
  }
38
54
 
39
- zoneRateFromZoneRateDraft = (
55
+ private _transformZoneRateDraft = (
40
56
  projectKey: string,
41
57
  draft: ZoneRateDraft
42
58
  ): ZoneRate => ({
43
59
  ...draft,
44
- zone: getReferenceFromResourceIdentifier(
60
+ zone: getReferenceFromResourceIdentifier<ZoneReference>(
45
61
  draft.zone,
46
62
  projectKey,
47
63
  this._storage
48
64
  ),
49
- shippingRates: draft.shippingRates.map(s => ({
50
- ...s,
51
- price: createTypedMoney(s.price),
52
- freeAbove: s.freeAbove && createTypedMoney(s.freeAbove),
53
- tiers: s.tiers || [],
54
- })),
65
+ shippingRates: draft.shippingRates?.map(this._transformShippingRate),
55
66
  })
56
67
 
57
- actions = {}
68
+ private _transformShippingRate = (rate: ShippingRateDraft): ShippingRate => {
69
+ return {
70
+ price: createTypedMoney(rate.price),
71
+ freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
72
+ tiers: rate.tiers || [],
73
+ }
74
+ }
75
+
76
+ actions: Partial<
77
+ Record<
78
+ ShippingMethodUpdateAction['action'],
79
+ (
80
+ projectKey: string,
81
+ resource: Writable<ShippingMethod>,
82
+ action: any
83
+ ) => void
84
+ >
85
+ > = {
86
+ addShippingRate: (
87
+ projectKey: string,
88
+ resource: Writable<ShippingMethod>,
89
+ { shippingRate, zone }: ShippingMethodAddShippingRateAction
90
+ ) => {
91
+ const rate = this._transformShippingRate(shippingRate)
92
+
93
+ resource.zoneRates.forEach(zoneRate => {
94
+ if (zoneRate.zone.id == zone.id) {
95
+ zoneRate.shippingRates.push(rate)
96
+ return
97
+ }
98
+ })
99
+ resource.zoneRates.push({
100
+ zone: {
101
+ typeId: 'zone',
102
+ id: zone.id!,
103
+ },
104
+ shippingRates: [rate],
105
+ })
106
+ },
107
+ removeShippingRate: (
108
+ projectKey: string,
109
+ resource: Writable<ShippingMethod>,
110
+ { shippingRate, zone }: ShippingMethodAddShippingRateAction
111
+ ) => {
112
+ const rate = this._transformShippingRate(shippingRate)
113
+
114
+ resource.zoneRates.forEach(zoneRate => {
115
+ if (zoneRate.zone.id == zone.id) {
116
+ zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
117
+ return !deepEqual(rate, otherRate)
118
+ })
119
+ }
120
+ })
121
+ },
122
+ addZone: (
123
+ projectKey: string,
124
+ resource: Writable<ShippingMethod>,
125
+ { zone }: ShippingMethodAddZoneAction
126
+ ) => {
127
+ const zoneReference = getReferenceFromResourceIdentifier<ZoneReference>(
128
+ zone,
129
+ projectKey,
130
+ this._storage
131
+ )
132
+
133
+ if (resource.zoneRates === undefined) {
134
+ resource.zoneRates = []
135
+ }
136
+
137
+ resource.zoneRates.push({
138
+ zone: zoneReference,
139
+ shippingRates: [],
140
+ })
141
+ },
142
+ removeZone: (
143
+ projectKey: string,
144
+ resource: Writable<ShippingMethod>,
145
+ { zone }: ShippingMethodRemoveZoneAction
146
+ ) => {
147
+ resource.zoneRates = resource.zoneRates.filter(zoneRate => {
148
+ return zoneRate.zone.id !== zone.id
149
+ })
150
+ },
151
+ setKey: (
152
+ projectKey: string,
153
+ resource: Writable<ShippingMethod>,
154
+ { key }: ShippingMethodSetKeyAction
155
+ ) => {
156
+ resource.key = key
157
+ },
158
+ setDescription: (
159
+ projectKey: string,
160
+ resource: Writable<ShippingMethod>,
161
+ { description }: ShippingMethodSetDescriptionAction
162
+ ) => {
163
+ resource.description = description
164
+ },
165
+ setLocalizedDescription: (
166
+ projectKey: string,
167
+ resource: Writable<ShippingMethod>,
168
+ { localizedDescription }: ShippingMethodSetLocalizedDescriptionAction
169
+ ) => {
170
+ resource.localizedDescription = localizedDescription
171
+ },
172
+ setPredicate: (
173
+ projectKey: string,
174
+ resource: Writable<ShippingMethod>,
175
+ { predicate }: ShippingMethodSetPredicateAction
176
+ ) => {
177
+ resource.predicate = predicate
178
+ },
179
+ changeIsDefault: (
180
+ projectKey: string,
181
+ resource: Writable<ShippingMethod>,
182
+ { isDefault }: ShippingMethodChangeIsDefaultAction
183
+ ) => {
184
+ resource.isDefault = isDefault
185
+ },
186
+ changeName: (
187
+ projectKey: string,
188
+ resource: Writable<ShippingMethod>,
189
+ { name }: ShippingMethodChangeNameAction
190
+ ) => {
191
+ resource.name = name
192
+ },
193
+ }
58
194
  }