@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
@@ -6,8 +6,10 @@ import {
6
6
  ProductDraft,
7
7
  ProductPublishAction,
8
8
  ProductSetAttributeAction,
9
- ProductType,
9
+ ProductSetDescriptionAction,
10
+ ProductSetKeyAction,
10
11
  ProductTypeReference,
12
+ ProductUpdateAction,
11
13
  ProductVariant,
12
14
  ProductVariantDraft,
13
15
  ReferenceTypeId,
@@ -17,6 +19,7 @@ import { getBaseResourceProperties } from '../helpers'
17
19
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
18
20
  import { Writable } from '../types'
19
21
  import { getReferenceFromResourceIdentifier } from './helpers'
22
+ import deepEqual from 'deep-equal'
20
23
 
21
24
  export class ProductRepository extends AbstractResourceRepository {
22
25
  getTypeId(): ReferenceTypeId {
@@ -52,97 +55,136 @@ export class ProductRepository extends AbstractResourceRepository {
52
55
  categories: [],
53
56
  masterVariant: variantFromDraft(1, draft.masterVariant),
54
57
  variants:
55
- draft.variants?.map((variant, index) => {
56
- return variantFromDraft(index + 2, variant)
57
- }) ?? [],
58
+ draft.variants?.map((variant, index) =>
59
+ variantFromDraft(index + 2, variant)
60
+ ) ?? [],
58
61
 
59
- // @ts-ignore
60
- searchKeywords: draft.searchKeywords,
62
+ searchKeywords: draft.searchKeywords ?? {},
61
63
  }
62
64
 
63
65
  const resource: Product = {
64
66
  ...getBaseResourceProperties(),
67
+ key: draft.key,
65
68
  productType: productType,
66
69
  masterData: {
67
- // @ts-ignore
68
- current: draft.publish ? productData : undefined,
69
- // @ts-ignore
70
- staged: draft.publish ? undefined : productData,
71
- hasStagedChanges: draft.publish ?? true,
70
+ current: productData,
71
+ staged: productData,
72
+ hasStagedChanges: false,
72
73
  published: draft.publish ?? false,
73
74
  },
74
75
  }
75
76
 
76
- this.save(context, resource)
77
+ this.saveNew(context, resource)
77
78
 
78
79
  return resource
79
80
  }
80
81
 
81
- actions = {
82
+ actions: Partial<
83
+ Record<
84
+ ProductUpdateAction['action'],
85
+ (
86
+ context: RepositoryContext,
87
+ resource: Writable<Product>,
88
+ action: any
89
+ ) => void
90
+ >
91
+ > = {
82
92
  publish: (
83
93
  context: RepositoryContext,
84
94
  resource: Writable<Product>,
85
95
  { scope }: ProductPublishAction
86
96
  ) => {
87
- if (resource.masterData.staged) {
88
- resource.masterData.current = resource.masterData.staged
89
- // @ts-ignore
90
- resource.masterData.staged = undefined
91
- }
92
- resource.masterData.hasStagedChanges = false
97
+ resource.masterData.current = resource.masterData.staged
93
98
  resource.masterData.published = true
99
+ checkForStagedChanges(resource)
100
+ },
101
+ unpublish: (
102
+ context: RepositoryContext,
103
+ resource: Writable<Product>,
104
+ // { action }: ProductUnpublishAction
105
+ ) => {
106
+ resource.masterData.published = false
107
+ checkForStagedChanges(resource)
94
108
  },
95
109
  setAttribute: (
96
110
  context: RepositoryContext,
97
111
  resource: Writable<Product>,
98
112
  { variantId, sku, name, value, staged }: ProductSetAttributeAction
99
113
  ) => {
100
- const isStaged = staged !== undefined ? staged : false
101
- const productData = getProductData(resource, isStaged)
102
- const { variant, isMasterVariant, variantIndex } = getVariant(
103
- productData,
104
- variantId,
105
- sku
106
- )
107
- if (!variant) {
108
- throw new Error(
109
- `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
114
+ const setAttr = (data: Writable<ProductData>) => {
115
+ const { variant, isMasterVariant, variantIndex } = getVariant(
116
+ data,
117
+ variantId,
118
+ sku
110
119
  )
111
- }
120
+ if (!variant) {
121
+ throw new Error(
122
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
123
+ )
124
+ }
112
125
 
113
- if (!variant.attributes) {
114
- variant.attributes = []
115
- }
126
+ if (!variant.attributes) {
127
+ variant.attributes = []
128
+ }
116
129
 
117
- const existingAttr = variant.attributes.find(attr => attr.name === name)
118
- if (existingAttr) {
119
- existingAttr.value = value
120
- } else {
121
- variant.attributes.push({
122
- name,
123
- value,
124
- })
125
- }
126
- if (isStaged) {
127
- resource.masterData.staged = productData
128
- if (isMasterVariant) {
129
- resource.masterData.staged.masterVariant = variant
130
+ const existingAttr = variant.attributes.find(
131
+ (attr) => attr.name === name
132
+ )
133
+ if (existingAttr) {
134
+ existingAttr.value = value
130
135
  } else {
131
- resource.masterData.staged.variants[variantIndex] = variant
136
+ variant.attributes.push({
137
+ name,
138
+ value,
139
+ })
132
140
  }
133
- resource.masterData.hasStagedChanges = true
134
- } else {
135
- resource.masterData.current = productData
136
141
  if (isMasterVariant) {
137
- resource.masterData.current.masterVariant = variant
142
+ data.masterVariant = variant
138
143
  } else {
139
- resource.masterData.current.variants[variantIndex] = variant
144
+ data.variants[variantIndex] = variant
140
145
  }
141
146
  }
147
+
148
+ // If true, only the staged Attribute is set. If false, both current and
149
+ // staged Attribute is set. Default is true
150
+ const onlyStaged = staged !== undefined ? staged : true
151
+
152
+ // Write the attribute to the staged data
153
+ setAttr(resource.masterData.staged)
154
+
155
+ // Also write to published data is isStaged = false
156
+ // if isStaged is false we set the attribute on both the staged and
157
+ // published data.
158
+ if (!onlyStaged) {
159
+ setAttr(resource.masterData.current)
160
+ }
161
+ checkForStagedChanges(resource)
162
+
163
+ return resource
164
+ },
165
+ 'setDescription': (
166
+ context: RepositoryContext,
167
+ resource: Writable<Product>,
168
+ { description, staged }: ProductSetDescriptionAction
169
+ ) => {
170
+ const onlyStaged = staged !== undefined ? staged : true
171
+
172
+ resource.masterData.staged.description = description
173
+ if (!onlyStaged) {
174
+ resource.masterData.current.description = description
175
+ }
176
+ checkForStagedChanges(resource)
177
+ return resource
178
+ },
179
+ 'setKey': (
180
+ context: RepositoryContext,
181
+ resource: Writable<Product>,
182
+ { key }: ProductSetKeyAction
183
+ ) => {
184
+ resource.key = key
185
+ return resource
142
186
  },
143
- // 'setKey': () => {},
144
187
  // 'changeName': () => {},
145
- // 'setDescription': () => {},
146
188
  // 'changeSlug': () => {},
147
189
  // 'addVariant': () => {},
148
190
  // 'removeVariant': () => {},
@@ -181,16 +223,22 @@ export class ProductRepository extends AbstractResourceRepository {
181
223
  // 'setMetaKeywords': () => {},
182
224
  // 'revertStagedChanges': () => {},
183
225
  // 'revertStagedVariantChanges': () => {},
184
- // 'unpublish': () => {},
185
226
  // 'transitionState': () => {},
186
227
  }
187
228
  }
188
229
 
189
- const getProductData = (product: Product, staged: boolean) => {
190
- if (!staged && product.masterData.current) {
191
- return product.masterData.current
230
+ // Check if the product still has staged data that is different from the
231
+ // current data.
232
+ const checkForStagedChanges = (product: Writable<Product>) => {
233
+ if (!product.masterData.staged) {
234
+ product.masterData.staged = product.masterData.current
235
+ }
236
+
237
+ if (deepEqual(product.masterData.current, product.masterData.staged)) {
238
+ product.masterData.hasStagedChanges = false
239
+ } else {
240
+ product.masterData.hasStagedChanges = true
192
241
  }
193
- return product.masterData.staged
194
242
  }
195
243
 
196
244
  interface VariantResult {
@@ -229,25 +277,21 @@ const getVariant = (
229
277
  const variantFromDraft = (
230
278
  variantId: number,
231
279
  variant: ProductVariantDraft
232
- ): ProductVariant => {
233
- return {
234
- id: variantId,
235
- sku: variant?.sku,
236
- attributes: variant?.attributes ?? [],
237
- prices: variant?.prices?.map(priceFromDraft),
238
- assets: [],
239
- images: [],
240
- }
241
- }
280
+ ): ProductVariant => ({
281
+ id: variantId,
282
+ sku: variant?.sku,
283
+ attributes: variant?.attributes ?? [],
284
+ prices: variant?.prices?.map(priceFromDraft),
285
+ assets: [],
286
+ images: [],
287
+ })
242
288
 
243
- const priceFromDraft = (draft: PriceDraft): Price => {
244
- return {
245
- id: uuidv4(),
246
- value: {
247
- currencyCode: draft.value.currencyCode,
248
- centAmount: draft.value.centAmount,
249
- fractionDigits: 2,
250
- type: 'centPrecision',
251
- },
252
- }
253
- }
289
+ const priceFromDraft = (draft: PriceDraft): Price => ({
290
+ id: uuidv4(),
291
+ value: {
292
+ currencyCode: draft.value.currencyCode,
293
+ centAmount: draft.value.centAmount,
294
+ fractionDigits: 2,
295
+ type: 'centPrecision',
296
+ },
297
+ })
@@ -13,10 +13,7 @@ import {
13
13
  ProjectSetShippingRateInputTypeAction,
14
14
  ProjectUpdateAction,
15
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'
16
+ import { Resource, Writable } from 'types'
20
17
  import { AbstractRepository, RepositoryContext } from './abstract'
21
18
  import { maskSecretValue } from '../lib/masking'
22
19
 
@@ -26,34 +23,19 @@ export class ProjectRepository extends AbstractRepository {
26
23
  return this.postProcessResource(resource)
27
24
  }
28
25
 
29
- postProcessResource(resource: any): any {
26
+ postProcessResource<T extends Resource>(resource: T): T {
30
27
  if (resource) {
31
- return maskSecretValue(
32
- resource, 'externalOAuth.authorizationHeader')
28
+ return maskSecretValue(resource, 'externalOAuth.authorizationHeader')
33
29
  }
34
30
  return resource
35
-
36
31
  }
37
32
 
38
- save(context: RepositoryContext, resource: Project) {
39
- const current = this.get(context)
40
-
41
- if (current) {
42
- checkConcurrentModification(current, resource.version)
43
- } else {
44
- if (resource.version !== 0) {
45
- throw new CommercetoolsError<InvalidOperationError>(
46
- {
47
- code: 'InvalidOperation',
48
- message: 'version on create must be 0',
49
- },
50
- 400
51
- )
52
- }
53
- }
33
+ saveNew(context: RepositoryContext, resource: Writable<Project>) {
34
+ resource.version = 1
35
+ this._storage.saveProject(resource)
36
+ }
54
37
 
55
- // @ts-ignore
56
- resource.version += 1
38
+ saveUpdate(context: RepositoryContext, version: number, resource: Project) {
57
39
  this._storage.saveProject(resource)
58
40
  }
59
41
 
@@ -147,7 +129,8 @@ export class ProjectRepository extends AbstractRepository {
147
129
  countryTaxRateFallbackEnabled,
148
130
  }: ProjectChangeCountryTaxRateFallbackEnabledAction
149
131
  ) => {
150
- resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled
132
+ resource.carts.countryTaxRateFallbackEnabled =
133
+ countryTaxRateFallbackEnabled
151
134
  },
152
135
  changeCartsConfiguration: (
153
136
  context: RepositoryContext,
@@ -47,7 +47,7 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
47
47
  context.projectKey,
48
48
  this._storage
49
49
  ),
50
- zoneRates: draft.zoneRates?.map(z =>
50
+ zoneRates: draft.zoneRates?.map((z) =>
51
51
  this._transformZoneRateDraft(context, z)
52
52
  ),
53
53
  custom: createCustomFields(
@@ -56,7 +56,7 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
56
56
  this._storage
57
57
  ),
58
58
  }
59
- this.save(context, resource)
59
+ this.saveNew(context, resource)
60
60
  return resource
61
61
  }
62
62
 
@@ -73,13 +73,11 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
73
73
  shippingRates: draft.shippingRates?.map(this._transformShippingRate),
74
74
  })
75
75
 
76
- private _transformShippingRate = (rate: ShippingRateDraft): ShippingRate => {
77
- return {
78
- price: createTypedMoney(rate.price),
79
- freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
80
- tiers: rate.tiers || [],
81
- }
82
- }
76
+ private _transformShippingRate = (rate: ShippingRateDraft): ShippingRate => ({
77
+ price: createTypedMoney(rate.price),
78
+ freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
79
+ tiers: rate.tiers || [],
80
+ })
83
81
 
84
82
  actions: Partial<
85
83
  Record<
@@ -98,7 +96,7 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
98
96
  ) => {
99
97
  const rate = this._transformShippingRate(shippingRate)
100
98
 
101
- resource.zoneRates.forEach(zoneRate => {
99
+ resource.zoneRates.forEach((zoneRate) => {
102
100
  if (zoneRate.zone.id === zone.id) {
103
101
  zoneRate.shippingRates.push(rate)
104
102
  return
@@ -119,11 +117,11 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
119
117
  ) => {
120
118
  const rate = this._transformShippingRate(shippingRate)
121
119
 
122
- resource.zoneRates.forEach(zoneRate => {
120
+ resource.zoneRates.forEach((zoneRate) => {
123
121
  if (zoneRate.zone.id === zone.id) {
124
- zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
125
- return !deepEqual(rate, otherRate)
126
- })
122
+ zoneRate.shippingRates = zoneRate.shippingRates.filter(
123
+ (otherRate) => !deepEqual(rate, otherRate)
124
+ )
127
125
  }
128
126
  })
129
127
  },
@@ -152,9 +150,9 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
152
150
  resource: Writable<ShippingMethod>,
153
151
  { zone }: ShippingMethodRemoveZoneAction
154
152
  ) => {
155
- resource.zoneRates = resource.zoneRates.filter(zoneRate => {
156
- return zoneRate.zone.id !== zone.id
157
- })
153
+ resource.zoneRates = resource.zoneRates.filter(
154
+ (zoneRate) => zoneRate.zone.id !== zone.id
155
+ )
158
156
  },
159
157
  setKey: (
160
158
  _context: RepositoryContext,
@@ -27,7 +27,7 @@ export class ShoppingListRepository extends AbstractResourceRepository {
27
27
  this._storage
28
28
  ),
29
29
  textLineItems: [],
30
- lineItems: draft.lineItems?.map(e => ({
30
+ lineItems: draft.lineItems?.map((e) => ({
31
31
  ...getBaseResourceProperties(),
32
32
  ...e,
33
33
  addedAt: e.addedAt ?? '',
@@ -48,7 +48,7 @@ export class ShoppingListRepository extends AbstractResourceRepository {
48
48
  ? { typeId: 'store', key: draft.store.key! }
49
49
  : undefined,
50
50
  }
51
- this.save(context, resource)
51
+ this.saveNew(context, resource)
52
52
  return resource
53
53
  }
54
54
  }
@@ -26,12 +26,12 @@ export class StateRepository extends AbstractResourceRepository {
26
26
  ...draft,
27
27
  builtIn: false,
28
28
  initial: draft.initial || false,
29
- transitions: (draft.transitions || []).map(t =>
29
+ transitions: (draft.transitions || []).map((t) =>
30
30
  getReferenceFromResourceIdentifier(t, context.projectKey, this._storage)
31
31
  ),
32
32
  }
33
33
 
34
- this.save(context, resource)
34
+ this.saveNew(context, resource)
35
35
  return resource
36
36
  }
37
37
 
@@ -78,12 +78,12 @@ export class StateRepository extends AbstractResourceRepository {
78
78
  resource: Writable<State>,
79
79
  { transitions }: StateSetTransitionsAction
80
80
  ) => {
81
- resource.transitions = transitions?.map((resourceId): StateReference => {
82
- return {
83
- id: resourceId.id || "",
84
- typeId: "state",
85
- }
86
- })
87
- }
81
+ resource.transitions = transitions?.map(
82
+ (resourceId): StateReference => ({
83
+ id: resourceId.id || '',
84
+ typeId: 'state',
85
+ })
86
+ )
87
+ },
88
88
  }
89
89
  }
@@ -42,7 +42,7 @@ export class StoreRepository extends AbstractResourceRepository {
42
42
  this._storage
43
43
  ),
44
44
  }
45
- this.save(context, resource)
45
+ this.saveNew(context, resource)
46
46
  return resource
47
47
  }
48
48
 
@@ -52,7 +52,7 @@ export class StoreRepository extends AbstractResourceRepository {
52
52
  ) {
53
53
  if (!channels) return []
54
54
 
55
- return channels.map(ref =>
55
+ return channels.map((ref) =>
56
56
  getReferenceFromResourceIdentifier<ChannelReference>(
57
57
  ref,
58
58
  context.projectKey,
@@ -44,7 +44,7 @@ export class SubscriptionRepository extends AbstractResourceRepository {
44
44
  messages: draft.messages || [],
45
45
  status: 'Healthy',
46
46
  }
47
- this.save(context, resource)
47
+ this.saveNew(context, resource)
48
48
  return resource
49
49
  }
50
50
  }
@@ -28,7 +28,7 @@ export class TaxCategoryRepository extends AbstractResourceRepository {
28
28
  ...draft,
29
29
  rates: draft.rates?.map(this.taxRateFromTaxRateDraft) || [],
30
30
  }
31
- this.save(context, resource)
31
+ this.saveNew(context, resource)
32
32
  return resource
33
33
  }
34
34
 
@@ -82,9 +82,9 @@ export class TaxCategoryRepository extends AbstractResourceRepository {
82
82
  if (resource.rates === undefined) {
83
83
  resource.rates = []
84
84
  }
85
- resource.rates = resource.rates.filter(taxRate => {
86
- return taxRate.id !== taxRateId
87
- })
85
+ resource.rates = resource.rates.filter(
86
+ (taxRate) => taxRate.id !== taxRateId
87
+ )
88
88
  },
89
89
  replaceTaxRate: (
90
90
  context: RepositoryContext,
@@ -33,7 +33,7 @@ export class TypeRepository extends AbstractResourceRepository {
33
33
  fieldDefinitions: draft.fieldDefinitions || [],
34
34
  description: draft.description,
35
35
  }
36
- this.save(context, resource)
36
+ this.saveNew(context, resource)
37
37
  return resource
38
38
  }
39
39
  actions: Partial<
@@ -58,9 +58,9 @@ export class TypeRepository extends AbstractResourceRepository {
58
58
  resource: Writable<Type>,
59
59
  { fieldName }: TypeRemoveFieldDefinitionAction
60
60
  ) => {
61
- resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
62
- return f.name !== fieldName
63
- })
61
+ resource.fieldDefinitions = resource.fieldDefinitions.filter(
62
+ (f) => f.name !== fieldName
63
+ )
64
64
  },
65
65
  setDescription: (
66
66
  context: RepositoryContext,
@@ -82,12 +82,12 @@ export class TypeRepository extends AbstractResourceRepository {
82
82
  { fieldNames }: TypeChangeFieldDefinitionOrderAction
83
83
  ) => {
84
84
  const fields = new Map(
85
- resource.fieldDefinitions.map(item => [item.name, item])
85
+ resource.fieldDefinitions.map((item) => [item.name, item])
86
86
  )
87
87
  const result: FieldDefinition[] = []
88
88
  let current = resource.fieldDefinitions
89
89
 
90
- fieldNames.forEach(fieldName => {
90
+ fieldNames.forEach((fieldName) => {
91
91
  const field = fields.get(fieldName)
92
92
  if (field === undefined) {
93
93
  throw new Error('New field')
@@ -95,15 +95,13 @@ export class TypeRepository extends AbstractResourceRepository {
95
95
  result.push(field)
96
96
 
97
97
  // Remove from current items
98
- current = current.filter(f => {
99
- return f.name !== fieldName
100
- })
98
+ current = current.filter((f) => f.name !== fieldName)
101
99
  })
102
100
 
103
101
  if (
104
102
  isEqual(
105
103
  fieldNames,
106
- resource.fieldDefinitions.map(item => item.name)
104
+ resource.fieldDefinitions.map((item) => item.name)
107
105
  )
108
106
  ) {
109
107
  throw new CommercetoolsError<InvalidOperationError>({
@@ -126,7 +124,7 @@ export class TypeRepository extends AbstractResourceRepository {
126
124
  resource: Writable<Type>,
127
125
  { fieldName, value }: TypeAddEnumValueAction
128
126
  ) => {
129
- resource.fieldDefinitions.forEach(field => {
127
+ resource.fieldDefinitions.forEach((field) => {
130
128
  if (field.name === fieldName) {
131
129
  // TODO, should be done better i suppose
132
130
  if (field.type.name === 'Enum') {
@@ -147,11 +145,11 @@ export class TypeRepository extends AbstractResourceRepository {
147
145
  resource: Writable<Type>,
148
146
  { fieldName, value }: TypeChangeEnumValueLabelAction
149
147
  ) => {
150
- resource.fieldDefinitions.forEach(field => {
148
+ resource.fieldDefinitions.forEach((field) => {
151
149
  if (field.name === fieldName) {
152
150
  // TODO, should be done better i suppose
153
151
  if (field.type.name === 'Enum') {
154
- field.type.values.forEach(v => {
152
+ field.type.values.forEach((v) => {
155
153
  if (v.key === value.key) {
156
154
  v.label = value.label
157
155
  }
@@ -160,7 +158,7 @@ export class TypeRepository extends AbstractResourceRepository {
160
158
  field.type.name === 'Set' &&
161
159
  field.type.elementType.name === 'Enum'
162
160
  ) {
163
- field.type.elementType.values.forEach(v => {
161
+ field.type.elementType.values.forEach((v) => {
164
162
  if (v.key === value.key) {
165
163
  v.label = value.label
166
164
  }
@@ -26,7 +26,7 @@ export class ZoneRepository extends AbstractResourceRepository {
26
26
  name: draft.name,
27
27
  description: draft.description,
28
28
  }
29
- this.save(context, resource)
29
+ this.saveNew(context, resource)
30
30
  return resource
31
31
  }
32
32
 
@@ -52,11 +52,10 @@ export class ZoneRepository extends AbstractResourceRepository {
52
52
  resource: Writable<Zone>,
53
53
  { location }: ZoneRemoveLocationAction
54
54
  ) => {
55
- resource.locations = resource.locations.filter(loc => {
56
- return !(
57
- loc.country === location.country && loc.state === location.state
58
- )
59
- })
55
+ resource.locations = resource.locations.filter(
56
+ (loc) =>
57
+ !(loc.country === location.country && loc.state === location.state)
58
+ )
60
59
  },
61
60
  changeName: (
62
61
  context: RepositoryContext,
package/src/server.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { CommercetoolsMock } from './index'
2
2
 
3
+ process.on('SIGINT', function() {
4
+ console.info("Stopping server...")
5
+ process.exit();
6
+ });
7
+
3
8
  const instance = new CommercetoolsMock()
4
9
 
5
10
  let port = 3000