@labdigital/commercetools-mock 0.5.12 → 0.5.15

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 (107) hide show
  1. package/README.md +6 -2
  2. package/dist/commercetools-mock.cjs.development.js +3153 -1689
  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 +3153 -1689
  7. package/dist/commercetools-mock.esm.js.map +1 -1
  8. package/dist/ctMock.d.ts +1 -0
  9. package/dist/lib/masking.d.ts +1 -0
  10. package/dist/repositories/abstract.d.ts +10 -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 +2 -2
  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 +2 -2
  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/customer-group.d.ts +9 -0
  41. package/dist/services/discount-code.d.ts +9 -0
  42. package/dist/services/extension.d.ts +9 -0
  43. package/dist/services/my-cart.d.ts +11 -0
  44. package/dist/services/project.d.ts +11 -0
  45. package/dist/services/subscription.d.ts +9 -0
  46. package/dist/services/zone.d.ts +9 -0
  47. package/dist/storage.d.ts +12 -1
  48. package/dist/types.d.ts +2 -1
  49. package/package.json +20 -20
  50. package/src/ctMock.ts +44 -17
  51. package/src/lib/masking.ts +22 -0
  52. package/src/oauth/server.ts +3 -1
  53. package/src/repositories/abstract.ts +47 -33
  54. package/src/repositories/cart-discount.ts +140 -0
  55. package/src/repositories/cart.ts +247 -3
  56. package/src/repositories/category.ts +140 -0
  57. package/src/repositories/channel.ts +23 -0
  58. package/src/repositories/custom-object.ts +2 -2
  59. package/src/repositories/customer-group.ts +42 -0
  60. package/src/repositories/customer.ts +2 -2
  61. package/src/repositories/discount-code.ts +143 -0
  62. package/src/repositories/errors.ts +7 -2
  63. package/src/repositories/extension.ts +65 -0
  64. package/src/repositories/inventory-entry.ts +2 -2
  65. package/src/repositories/order.ts +2 -2
  66. package/src/repositories/payment.ts +10 -6
  67. package/src/repositories/product-projection.ts +2 -2
  68. package/src/repositories/product-type.ts +57 -4
  69. package/src/repositories/product.ts +4 -2
  70. package/src/repositories/project.ts +156 -0
  71. package/src/repositories/shipping-method.ts +149 -13
  72. package/src/repositories/shopping-list.ts +2 -2
  73. package/src/repositories/state.ts +48 -4
  74. package/src/repositories/store.ts +69 -4
  75. package/src/repositories/subscription.ts +50 -0
  76. package/src/repositories/tax-category.ts +80 -6
  77. package/src/repositories/type.ts +133 -3
  78. package/src/repositories/zone.ts +77 -0
  79. package/src/server.ts +6 -1
  80. package/src/services/abstract.ts +26 -18
  81. package/src/services/cart-discount.ts +17 -0
  82. package/src/services/cart.test.ts +314 -3
  83. package/src/services/category.test.ts +37 -0
  84. package/src/services/category.ts +17 -0
  85. package/src/services/channel.ts +17 -0
  86. package/src/services/custom-object.test.ts +3 -3
  87. package/src/services/customer-group.ts +17 -0
  88. package/src/services/discount-code.ts +17 -0
  89. package/src/services/extension.ts +17 -0
  90. package/src/services/inventory-entry.test.ts +3 -3
  91. package/src/services/my-cart.test.ts +93 -0
  92. package/src/services/my-cart.ts +44 -0
  93. package/src/services/my-payment.test.ts +2 -2
  94. package/src/services/order.test.ts +4 -4
  95. package/src/services/payment.test.ts +2 -2
  96. package/src/services/product-projection.test.ts +12 -5
  97. package/src/services/product-type.test.ts +2 -2
  98. package/src/services/product.test.ts +6 -2
  99. package/src/services/project.ts +42 -0
  100. package/src/services/shipping-method.test.ts +3 -3
  101. package/src/services/state.test.ts +2 -2
  102. package/src/services/store.test.ts +60 -0
  103. package/src/services/subscription.ts +17 -0
  104. package/src/services/tax-category.test.ts +3 -3
  105. package/src/services/zone.ts +17 -0
  106. package/src/storage.ts +99 -1
  107. package/src/types.ts +2 -1
@@ -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: () => {},
@@ -6,11 +6,11 @@ import {
6
6
  ProductVariantDraft,
7
7
  } from '@commercetools/platform-sdk'
8
8
  import { getBaseResourceProperties } from '../helpers'
9
- import AbstractRepository from './abstract'
9
+ import { AbstractResourceRepository } from './abstract'
10
10
  import { RepositoryTypes } from '../types'
11
11
  import { parseFilterExpression } from '../lib/filterParser'
12
12
 
13
- export class ProductProjectionRepository extends AbstractRepository {
13
+ export class ProductProjectionRepository extends AbstractResourceRepository {
14
14
  getTypeId(): RepositoryTypes {
15
15
  return 'product-projection'
16
16
  }
@@ -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,156 @@
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
+ import { maskSecretValue } from '../lib/masking'
22
+
23
+ export class ProjectRepository extends AbstractRepository {
24
+ get(projectKey: string): Project | null {
25
+ const data = this._storage.getProject(projectKey)
26
+ const resource = this._storage.getProject(projectKey)
27
+ const masked = maskSecretValue<Project>(
28
+ resource,
29
+ 'externalOAuth.authorizationHeader'
30
+ )
31
+ return masked
32
+ }
33
+
34
+ save(projectKey: string, resource: Project) {
35
+ const current = this.get(projectKey)
36
+
37
+ if (current) {
38
+ checkConcurrentModification(current, resource.version)
39
+ } else {
40
+ if (resource.version !== 0) {
41
+ throw new CommercetoolsError<InvalidOperationError>(
42
+ {
43
+ code: 'InvalidOperation',
44
+ message: 'version on create must be 0',
45
+ },
46
+ 400
47
+ )
48
+ }
49
+ }
50
+
51
+ // @ts-ignore
52
+ resource.version += 1
53
+ this._storage.saveProject(resource)
54
+ }
55
+
56
+ actions: Partial<
57
+ Record<
58
+ ProjectUpdateAction['action'],
59
+ (projectKey: string, resource: Writable<Project>, action: any) => void
60
+ >
61
+ > = {
62
+ changeName: (
63
+ projectKey: string,
64
+ resource: Writable<Project>,
65
+ { name }: ProjectChangeNameAction
66
+ ) => {
67
+ resource.name = name
68
+ },
69
+ changeCurrencies: (
70
+ projectKey: string,
71
+ resource: Writable<Project>,
72
+ { currencies }: ProjectChangeCurrenciesAction
73
+ ) => {
74
+ resource.currencies = currencies
75
+ },
76
+ changeCountries: (
77
+ projectKey: string,
78
+ resource: Writable<Project>,
79
+ { countries }: ProjectChangeCountriesAction
80
+ ) => {
81
+ resource.countries = countries
82
+ },
83
+ changeLanguages: (
84
+ projectKey: string,
85
+ resource: Writable<Project>,
86
+ { languages }: ProjectChangeLanguagesAction
87
+ ) => {
88
+ resource.languages = languages
89
+ },
90
+ changeMessagesEnabled: (
91
+ projectKey: string,
92
+ resource: Writable<Project>,
93
+ { messagesEnabled }: ProjectChangeMessagesEnabledAction
94
+ ) => {
95
+ resource.messages.enabled = messagesEnabled
96
+ },
97
+ changeProductSearchIndexingEnabled: (
98
+ projectKey: string,
99
+ resource: Writable<Project>,
100
+ { enabled }: ProjectChangeProductSearchIndexingEnabledAction
101
+ ) => {
102
+ if (!resource.searchIndexing?.products) {
103
+ throw new Error('Invalid project state')
104
+ }
105
+ resource.searchIndexing.products.status = enabled
106
+ ? 'Activated'
107
+ : 'Deactivated'
108
+ resource.searchIndexing.products.lastModifiedAt = new Date().toISOString()
109
+ },
110
+ changeOrderSearchStatus: (
111
+ projectKey: string,
112
+ resource: Writable<Project>,
113
+ { status }: ProjectChangeOrderSearchStatusAction
114
+ ) => {
115
+ if (!resource.searchIndexing?.orders) {
116
+ throw new Error('Invalid project state')
117
+ }
118
+ resource.searchIndexing.orders.status = status
119
+ resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString()
120
+ },
121
+ setShippingRateInputType: (
122
+ projectKey: string,
123
+ resource: Writable<Project>,
124
+ { shippingRateInputType }: ProjectSetShippingRateInputTypeAction
125
+ ) => {
126
+ resource.shippingRateInputType = shippingRateInputType
127
+ },
128
+ setExternalOAuth: (
129
+ projectKey: string,
130
+ resource: Writable<Project>,
131
+ { externalOAuth }: ProjectSetExternalOAuthAction
132
+ ) => {
133
+ resource.externalOAuth = externalOAuth
134
+ },
135
+ changeCountryTaxRateFallbackEnabled: (
136
+ projectKey: string,
137
+ resource: Writable<Project>,
138
+ {
139
+ countryTaxRateFallbackEnabled,
140
+ }: ProjectChangeCountryTaxRateFallbackEnabledAction
141
+ ) => {
142
+ resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled
143
+ },
144
+ changeCartsConfiguration: (
145
+ projectKey: string,
146
+ resource: Writable<Project>,
147
+ { cartsConfiguration }: ProjectChangeCartsConfigurationAction
148
+ ) => {
149
+ console.log(cartsConfiguration)
150
+ resource.carts = cartsConfiguration || {
151
+ countryTaxRateFallbackEnabled: false,
152
+ deleteDaysAfterLastModification: 90,
153
+ }
154
+ },
155
+ }
156
+ }
@@ -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
  }
@@ -5,13 +5,13 @@ import {
5
5
  ShoppingListDraft,
6
6
  } from '@commercetools/platform-sdk'
7
7
  import { getBaseResourceProperties } from '../helpers'
8
- import AbstractRepository from './abstract'
8
+ import { AbstractResourceRepository } from './abstract'
9
9
  import {
10
10
  createCustomFields,
11
11
  getReferenceFromResourceIdentifier,
12
12
  } from './helpers'
13
13
 
14
- export class ShoppingListRepository extends AbstractRepository {
14
+ export class ShoppingListRepository extends AbstractResourceRepository {
15
15
  getTypeId(): ReferenceTypeId {
16
16
  return 'shopping-list'
17
17
  }