@labdigital/commercetools-mock 0.5.13 → 0.5.16

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 (106) hide show
  1. package/README.md +6 -2
  2. package/dist/commercetools-mock.cjs.development.js +3087 -1642
  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 +3087 -1642
  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 +9 -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 +10 -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 +39 -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 +4 -3
  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 +16 -15
  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 +44 -6
  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/subscription.ts +17 -0
  103. package/src/services/tax-category.test.ts +3 -3
  104. package/src/services/zone.ts +17 -0
  105. package/src/storage.ts +77 -3
  106. package/src/types.ts +2 -1
@@ -1,9 +1,30 @@
1
- import { Cart, CartDraft, ReferenceTypeId } from '@commercetools/platform-sdk'
1
+ import {
2
+ Cart,
3
+ CartAddLineItemAction,
4
+ CartDraft,
5
+ CartRemoveLineItemAction,
6
+ CartSetBillingAddressAction,
7
+ CartSetCountryAction,
8
+ CartSetCustomerEmailAction,
9
+ CartSetCustomFieldAction,
10
+ CartSetCustomTypeAction,
11
+ CartSetLocaleAction,
12
+ CartSetShippingAddressAction,
13
+ GeneralError,
14
+ LineItem,
15
+ Product,
16
+ ProductPagedQueryResponse,
17
+ ProductVariant,
18
+ ReferenceTypeId,
19
+ } from '@commercetools/platform-sdk'
20
+ import { v4 as uuidv4 } from 'uuid'
2
21
  import { getBaseResourceProperties } from '../helpers'
3
- import AbstractRepository from './abstract'
22
+ import { AbstractResourceRepository } from './abstract'
4
23
  import { createCustomFields } from './helpers'
24
+ import { Writable } from '../types'
25
+ import { CommercetoolsError } from '../exceptions'
5
26
 
6
- export class CartRepository extends AbstractRepository {
27
+ export class CartRepository extends AbstractResourceRepository {
7
28
  getTypeId(): ReferenceTypeId {
8
29
  return 'cart'
9
30
  }
@@ -30,4 +51,227 @@ export class CartRepository extends AbstractRepository {
30
51
  this.save(projectKey, resource)
31
52
  return resource
32
53
  }
54
+
55
+ getActiveCart(projectKey: string): Cart | undefined {
56
+ // Get first active cart
57
+ const results = this._storage.query(projectKey, this.getTypeId(), {
58
+ where: [`cartState="Active"`],
59
+ })
60
+ if (results.count > 0) {
61
+ return results.results[0] as Cart
62
+ }
63
+
64
+ return
65
+ }
66
+
67
+ actions = {
68
+ addLineItem: (
69
+ projectKey: string,
70
+ resource: Writable<Cart>,
71
+ { productId, variantId, sku, quantity = 1 }: CartAddLineItemAction
72
+ ) => {
73
+ let product: Product | null = null
74
+ let variant: ProductVariant | undefined
75
+
76
+ if (productId && variantId) {
77
+ // Fetch product and variant by ID
78
+ product = this._storage.get(projectKey, 'product', productId, {})
79
+ } else if (sku) {
80
+ // Fetch product and variant by SKU
81
+ const items = this._storage.query(projectKey, 'product', {
82
+ where: [
83
+ `masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
84
+ ],
85
+ }) as ProductPagedQueryResponse
86
+
87
+ if (items.count === 1) {
88
+ product = items.results[0]
89
+ }
90
+ }
91
+
92
+ if (!product) {
93
+ // Check if product is found
94
+ throw new CommercetoolsError<GeneralError>({
95
+ code: 'General',
96
+ message: sku
97
+ ? `A product containing a variant with SKU '${sku}' not found.`
98
+ : `A product with ID '${productId}' not found.`,
99
+ })
100
+ }
101
+
102
+ // Find matching variant
103
+ variant = [
104
+ product.masterData.current.masterVariant,
105
+ ...product.masterData.current.variants,
106
+ ].find(x => {
107
+ if (sku) return x.sku === sku
108
+ if (variantId) return x.id === variantId
109
+ return false
110
+ })
111
+
112
+ if (!variant) {
113
+ // Check if variant is found
114
+ throw new CommercetoolsError<GeneralError>({
115
+ code: 'General',
116
+ message: sku
117
+ ? `A variant with SKU '${sku}' for product '${product.id}' not found.`
118
+ : `A variant with ID '${variantId}' for product '${product.id}' not found.`,
119
+ })
120
+ }
121
+
122
+ const alreadyAdded = resource.lineItems.some(
123
+ x => x.productId === product?.id && x.variant.id === variant?.id
124
+ )
125
+ if (alreadyAdded) {
126
+ // increase quantity and update total price
127
+ resource.lineItems.map(x => {
128
+ if (x.productId === product?.id && x.variant.id === variant?.id) {
129
+ x.quantity += quantity
130
+ x.totalPrice.centAmount = calculateLineItemTotalPrice(x)
131
+ }
132
+ return x
133
+ })
134
+ } else {
135
+ // add line item
136
+ if (!variant.prices?.length) {
137
+ throw new CommercetoolsError<GeneralError>({
138
+ code: 'General',
139
+ message: `A product with ID '${productId}' doesn't have any prices.`,
140
+ })
141
+ }
142
+
143
+ const price = variant.prices[0]
144
+ resource.lineItems.push({
145
+ id: uuidv4(),
146
+ productId: product.id,
147
+ productKey: product.key,
148
+ name: product.masterData.current.name,
149
+ productSlug: product.masterData.current.slug,
150
+ productType: product.productType,
151
+ variant,
152
+ price: price,
153
+ totalPrice: {
154
+ ...price.value,
155
+ centAmount: price.value.centAmount * quantity,
156
+ },
157
+ quantity,
158
+ discountedPricePerQuantity: [],
159
+ lineItemMode: 'Standard',
160
+ priceMode: 'Platform',
161
+ state: [],
162
+ })
163
+ }
164
+
165
+ // Update cart total price
166
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
167
+ },
168
+ removeLineItem: (
169
+ projectKey: string,
170
+ resource: Writable<Cart>,
171
+ { lineItemId, quantity }: CartRemoveLineItemAction
172
+ ) => {
173
+ const lineItem = resource.lineItems.find(x => x.id === lineItemId)
174
+ if (!lineItem) {
175
+ // Check if product is found
176
+ throw new CommercetoolsError<GeneralError>({
177
+ code: 'General',
178
+ message: `A line item with ID '${lineItemId}' not found.`,
179
+ })
180
+ }
181
+
182
+ const shouldDelete = !quantity || quantity >= lineItem.quantity
183
+ if (shouldDelete) {
184
+ // delete line item
185
+ resource.lineItems = resource.lineItems.filter(x => x.id !== lineItemId)
186
+ } else {
187
+ // decrease quantity and update total price
188
+ resource.lineItems.map(x => {
189
+ if (x.id === lineItemId && quantity) {
190
+ x.quantity -= quantity
191
+ x.totalPrice.centAmount = calculateLineItemTotalPrice(x)
192
+ }
193
+ return x
194
+ })
195
+ }
196
+
197
+ // Update cart total price
198
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
199
+ },
200
+ setBillingAddress: (
201
+ projectKey: string,
202
+ resource: Writable<Cart>,
203
+ { address }: CartSetBillingAddressAction
204
+ ) => {
205
+ resource.billingAddress = address
206
+ },
207
+ setCountry: (
208
+ projectKey: string,
209
+ resource: Writable<Cart>,
210
+ { country }: CartSetCountryAction
211
+ ) => {
212
+ resource.country = country
213
+ },
214
+ setCustomerEmail: (
215
+ projectKey: string,
216
+ resource: Writable<Cart>,
217
+ { email }: CartSetCustomerEmailAction
218
+ ) => {
219
+ resource.customerEmail = email
220
+ },
221
+ setCustomField: (
222
+ projectKey: string,
223
+ resource: Cart,
224
+ { name, value }: CartSetCustomFieldAction
225
+ ) => {
226
+ if (!resource.custom) {
227
+ throw new Error('Resource has no custom field')
228
+ }
229
+ resource.custom.fields[name] = value
230
+ },
231
+ setCustomType: (
232
+ projectKey: string,
233
+ resource: Writable<Cart>,
234
+ { type, fields }: CartSetCustomTypeAction
235
+ ) => {
236
+ if (!type) {
237
+ resource.custom = undefined
238
+ } else {
239
+ const resolvedType = this._storage.getByResourceIdentifier(
240
+ projectKey,
241
+ type
242
+ )
243
+ if (!resolvedType) {
244
+ throw new Error(`Type ${type} not found`)
245
+ }
246
+
247
+ resource.custom = {
248
+ type: {
249
+ typeId: 'type',
250
+ id: resolvedType.id,
251
+ },
252
+ fields: fields || [],
253
+ }
254
+ }
255
+ },
256
+ setLocale: (
257
+ projectKey: string,
258
+ resource: Writable<Cart>,
259
+ { locale }: CartSetLocaleAction
260
+ ) => {
261
+ resource.locale = locale
262
+ },
263
+ setShippingAddress: (
264
+ projectKey: string,
265
+ resource: Writable<Cart>,
266
+ { address }: CartSetShippingAddressAction
267
+ ) => {
268
+ resource.shippingAddress = address
269
+ },
270
+ }
33
271
  }
272
+
273
+ const calculateLineItemTotalPrice = (lineItem: LineItem): number =>
274
+ lineItem.price!.value.centAmount * lineItem.quantity
275
+
276
+ const calculateCartTotalPrice = (cart: Cart): number =>
277
+ cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0)
@@ -0,0 +1,140 @@
1
+ import { v4 as uuidv4 } from 'uuid'
2
+ import {
3
+ Category,
4
+ CategoryChangeAssetNameAction,
5
+ CategoryChangeSlugAction,
6
+ CategoryDraft,
7
+ CategorySetAssetDescriptionAction,
8
+ CategorySetAssetSourcesAction,
9
+ CategorySetDescriptionAction,
10
+ CategorySetKeyAction,
11
+ CategorySetMetaDescriptionAction,
12
+ CategorySetMetaKeywordsAction,
13
+ CategorySetMetaTitleAction,
14
+ ReferenceTypeId,
15
+ } from '@commercetools/platform-sdk'
16
+ import { Writable } from 'types'
17
+ import { getBaseResourceProperties } from '../helpers'
18
+ import { createCustomFields } from './helpers'
19
+ import { AbstractResourceRepository } from './abstract'
20
+
21
+ export class CategoryRepository extends AbstractResourceRepository {
22
+ getTypeId(): ReferenceTypeId {
23
+ return 'category'
24
+ }
25
+
26
+ create(projectKey: string, draft: CategoryDraft): Category {
27
+ const resource: Category = {
28
+ ...getBaseResourceProperties(),
29
+ key: draft.key,
30
+ name: draft.name,
31
+ slug: draft.slug,
32
+ orderHint: draft.orderHint || '',
33
+ parent: draft.parent
34
+ ? { typeId: 'category', id: draft.parent.id! }
35
+ : undefined,
36
+ ancestors: [], // TODO
37
+ assets:
38
+ draft.assets?.map(d => {
39
+ return {
40
+ id: uuidv4(),
41
+ name: d.name,
42
+ description: d.description,
43
+ sources: d.sources,
44
+ tags: d.tags,
45
+ key: d.key,
46
+ custom: createCustomFields(draft.custom, projectKey, this._storage),
47
+ }
48
+ }) || [],
49
+ }
50
+ this.save(projectKey, resource)
51
+ return resource
52
+ }
53
+
54
+ actions = {
55
+ changeAssetName: (
56
+ projectKey: string,
57
+ resource: Writable<Category>,
58
+ { assetId, assetKey, name }: CategoryChangeAssetNameAction
59
+ ) => {
60
+ resource.assets?.forEach(asset => {
61
+ if (assetId && assetId == asset.id) {
62
+ asset.name = name
63
+ }
64
+ if (assetKey && assetKey == asset.key) {
65
+ asset.name = name
66
+ }
67
+ })
68
+ },
69
+ changeSlug: (
70
+ projectKey: string,
71
+ resource: Writable<Category>,
72
+ { slug }: CategoryChangeSlugAction
73
+ ) => {
74
+ resource.slug = slug
75
+ },
76
+ setKey: (
77
+ projectKey: string,
78
+ resource: Writable<Category>,
79
+ { key }: CategorySetKeyAction
80
+ ) => {
81
+ resource.key = key
82
+ },
83
+ setAssetDescription: (
84
+ projectKey: string,
85
+ resource: Writable<Category>,
86
+ { assetId, assetKey, description }: CategorySetAssetDescriptionAction
87
+ ) => {
88
+ resource.assets?.forEach(asset => {
89
+ if (assetId && assetId == asset.id) {
90
+ asset.description = description
91
+ }
92
+ if (assetKey && assetKey == asset.key) {
93
+ asset.description = description
94
+ }
95
+ })
96
+ },
97
+ setAssetSources: (
98
+ projectKey: string,
99
+ resource: Writable<Category>,
100
+ { assetId, assetKey, sources }: CategorySetAssetSourcesAction
101
+ ) => {
102
+ resource.assets?.forEach(asset => {
103
+ if (assetId && assetId == asset.id) {
104
+ asset.sources = sources
105
+ }
106
+ if (assetKey && assetKey == asset.key) {
107
+ asset.sources = sources
108
+ }
109
+ })
110
+ },
111
+ setDescription: (
112
+ projectKey: string,
113
+ resource: Writable<Category>,
114
+ { description }: CategorySetDescriptionAction
115
+ ) => {
116
+ resource.description = description
117
+ },
118
+ setMetaDescription: (
119
+ projectKey: string,
120
+ resource: Writable<Category>,
121
+ { metaDescription }: CategorySetMetaDescriptionAction
122
+ ) => {
123
+ resource.metaDescription = metaDescription
124
+ },
125
+ setMetaKeywords: (
126
+ projectKey: string,
127
+ resource: Writable<Category>,
128
+ { metaKeywords }: CategorySetMetaKeywordsAction
129
+ ) => {
130
+ resource.metaKeywords = metaKeywords
131
+ },
132
+ setMetaTitle: (
133
+ projectKey: string,
134
+ resource: Writable<Category>,
135
+ { metaTitle }: CategorySetMetaTitleAction
136
+ ) => {
137
+ resource.metaTitle = metaTitle
138
+ },
139
+ }
140
+ }
@@ -0,0 +1,23 @@
1
+ import {
2
+ Channel,
3
+ ChannelDraft,
4
+ ReferenceTypeId,
5
+ } from '@commercetools/platform-sdk'
6
+ import { getBaseResourceProperties } from '../helpers'
7
+ import { AbstractResourceRepository } from './abstract'
8
+
9
+ export class ChannelRepository extends AbstractResourceRepository {
10
+ getTypeId(): ReferenceTypeId {
11
+ return 'channel'
12
+ }
13
+
14
+ create(projectKey: string, draft: ChannelDraft): Channel {
15
+ const resource: Channel = {
16
+ ...getBaseResourceProperties(),
17
+ key: draft.key,
18
+ roles: draft.roles || [],
19
+ }
20
+ this.save(projectKey, resource)
21
+ return resource
22
+ }
23
+ }
@@ -4,11 +4,11 @@ import {
4
4
  ReferenceTypeId,
5
5
  } from '@commercetools/platform-sdk'
6
6
  import { checkConcurrentModification } from './errors'
7
- import AbstractRepository from './abstract'
7
+ import { AbstractResourceRepository } from './abstract'
8
8
  import { Writable } from '../types'
9
9
  import { getBaseResourceProperties } from '../helpers'
10
10
 
11
- export class CustomObjectRepository extends AbstractRepository {
11
+ export class CustomObjectRepository extends AbstractResourceRepository {
12
12
  getTypeId(): ReferenceTypeId {
13
13
  return 'key-value-document'
14
14
  }
@@ -0,0 +1,42 @@
1
+ import {
2
+ CustomerGroup,
3
+ CustomerGroupChangeNameAction,
4
+ CustomerGroupDraft,
5
+ CustomerGroupSetKeyAction,
6
+ ReferenceTypeId,
7
+ } from '@commercetools/platform-sdk'
8
+ import { Writable } from 'types'
9
+ import { getBaseResourceProperties } from '../helpers'
10
+ import { AbstractResourceRepository } from './abstract'
11
+
12
+ export class CustomerGroupRepository extends AbstractResourceRepository {
13
+ getTypeId(): ReferenceTypeId {
14
+ return 'customer'
15
+ }
16
+ create(projectKey: string, draft: CustomerGroupDraft): CustomerGroup {
17
+ const resource: CustomerGroup = {
18
+ ...getBaseResourceProperties(),
19
+ key: draft.key,
20
+ name: draft.groupName,
21
+ }
22
+ this.save(projectKey, resource)
23
+ return resource
24
+ }
25
+
26
+ actions = {
27
+ setKey: (
28
+ projectKey: string,
29
+ resource: Writable<CustomerGroup>,
30
+ { key }: CustomerGroupSetKeyAction
31
+ ) => {
32
+ resource.key = key
33
+ },
34
+ changeName: (
35
+ projectKey: string,
36
+ resource: Writable<CustomerGroup>,
37
+ { name }: CustomerGroupChangeNameAction
38
+ ) => {
39
+ resource.name = name
40
+ },
41
+ }
42
+ }
@@ -4,9 +4,9 @@ import {
4
4
  ReferenceTypeId,
5
5
  } from '@commercetools/platform-sdk'
6
6
  import { getBaseResourceProperties } from '../helpers'
7
- import AbstractRepository from './abstract'
7
+ import { AbstractResourceRepository } from './abstract'
8
8
 
9
- export class CustomerRepository extends AbstractRepository {
9
+ export class CustomerRepository extends AbstractResourceRepository {
10
10
  getTypeId(): ReferenceTypeId {
11
11
  return 'customer'
12
12
  }
@@ -0,0 +1,143 @@
1
+ import {
2
+ CartDiscountReference,
3
+ DiscountCode,
4
+ DiscountCodeChangeCartDiscountsAction,
5
+ DiscountCodeChangeIsActiveAction,
6
+ DiscountCodeDraft,
7
+ DiscountCodeSetCartPredicateAction,
8
+ DiscountCodeSetDescriptionAction,
9
+ DiscountCodeSetMaxApplicationsAction,
10
+ DiscountCodeSetMaxApplicationsPerCustomerAction,
11
+ DiscountCodeSetNameAction,
12
+ DiscountCodeSetValidFromAction,
13
+ DiscountCodeSetValidFromAndUntilAction,
14
+ DiscountCodeSetValidUntilAction,
15
+ DiscountCodeUpdateAction,
16
+ ReferenceTypeId,
17
+ } from '@commercetools/platform-sdk'
18
+ import { Writable } from 'types'
19
+ import { getBaseResourceProperties } from '../helpers'
20
+ import { AbstractResourceRepository } from './abstract'
21
+
22
+ export class DiscountCodeRepository extends AbstractResourceRepository {
23
+ getTypeId(): ReferenceTypeId {
24
+ return 'cart-discount'
25
+ }
26
+
27
+ create(projectKey: string, draft: DiscountCodeDraft): DiscountCode {
28
+ console.log(draft)
29
+ const resource: DiscountCode = {
30
+ ...getBaseResourceProperties(),
31
+ applicationVersion: 1,
32
+ cartDiscounts: draft.cartDiscounts.map(
33
+ (obj): CartDiscountReference => ({
34
+ typeId: 'cart-discount',
35
+ id: obj.id!,
36
+ })
37
+ ),
38
+ cartPredicate: draft.cartPredicate,
39
+ code: draft.code,
40
+ description: draft.description,
41
+ groups: draft.groups || [],
42
+ isActive: draft.isActive || true,
43
+ name: draft.name,
44
+ references: [],
45
+ validFrom: draft.validFrom,
46
+ validUntil: draft.validUntil,
47
+ maxApplications: draft.maxApplications,
48
+ maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer,
49
+ }
50
+ this.save(projectKey, resource)
51
+ return resource
52
+ }
53
+
54
+ actions: Partial<
55
+ Record<
56
+ DiscountCodeUpdateAction['action'],
57
+ (
58
+ projectKey: string,
59
+ resource: Writable<DiscountCode>,
60
+ action: any
61
+ ) => void
62
+ >
63
+ > = {
64
+ changeIsActive: (
65
+ projectKey: string,
66
+ resource: Writable<DiscountCode>,
67
+ { isActive }: DiscountCodeChangeIsActiveAction
68
+ ) => {
69
+ resource.isActive = isActive
70
+ },
71
+ changeCartDiscounts: (
72
+ projectKey: string,
73
+ resource: Writable<DiscountCode>,
74
+ { cartDiscounts }: DiscountCodeChangeCartDiscountsAction
75
+ ) => {
76
+ resource.cartDiscounts = cartDiscounts.map(
77
+ (obj): CartDiscountReference => ({
78
+ typeId: 'cart-discount',
79
+ id: obj.id!,
80
+ })
81
+ )
82
+ },
83
+ setDescription: (
84
+ projectKey: string,
85
+ resource: Writable<DiscountCode>,
86
+ { description }: DiscountCodeSetDescriptionAction
87
+ ) => {
88
+ resource.description = description
89
+ },
90
+ setCartPredicate: (
91
+ projectKey: string,
92
+ resource: Writable<DiscountCode>,
93
+ { cartPredicate }: DiscountCodeSetCartPredicateAction
94
+ ) => {
95
+ resource.cartPredicate = cartPredicate
96
+ },
97
+ setName: (
98
+ projectKey: string,
99
+ resource: Writable<DiscountCode>,
100
+ { name }: DiscountCodeSetNameAction
101
+ ) => {
102
+ resource.name = name
103
+ },
104
+ setMaxApplications: (
105
+ projectKey: string,
106
+ resource: Writable<DiscountCode>,
107
+ { maxApplications }: DiscountCodeSetMaxApplicationsAction
108
+ ) => {
109
+ resource.maxApplications = maxApplications
110
+ },
111
+ setMaxApplicationsPerCustomer: (
112
+ projectKey: string,
113
+ resource: Writable<DiscountCode>,
114
+ {
115
+ maxApplicationsPerCustomer,
116
+ }: DiscountCodeSetMaxApplicationsPerCustomerAction
117
+ ) => {
118
+ resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer
119
+ },
120
+ setValidFrom: (
121
+ projectKey: string,
122
+ resource: Writable<DiscountCode>,
123
+ { validFrom }: DiscountCodeSetValidFromAction
124
+ ) => {
125
+ resource.validFrom = validFrom
126
+ },
127
+ setValidUntil: (
128
+ projectKey: string,
129
+ resource: Writable<DiscountCode>,
130
+ { validUntil }: DiscountCodeSetValidUntilAction
131
+ ) => {
132
+ resource.validUntil = validUntil
133
+ },
134
+ setValidFromAndUntil: (
135
+ projectKey: string,
136
+ resource: Writable<DiscountCode>,
137
+ { validFrom, validUntil }: DiscountCodeSetValidFromAndUntilAction
138
+ ) => {
139
+ resource.validFrom = validFrom
140
+ resource.validUntil = validUntil
141
+ },
142
+ }
143
+ }
@@ -1,18 +1,23 @@
1
1
  import {
2
2
  BaseResource,
3
3
  ConcurrentModificationError,
4
+ Project,
4
5
  } from '@commercetools/platform-sdk'
5
6
  import { CommercetoolsError } from '../exceptions'
6
7
 
7
8
  export const checkConcurrentModification = (
8
- resource: BaseResource,
9
+ resource: BaseResource | Project,
9
10
  expectedVersion: number
10
11
  ) => {
11
12
  if (resource.version === expectedVersion) return
12
13
 
14
+ const identifier = (<BaseResource>resource).id
15
+ ? (<BaseResource>resource).id
16
+ : (<Project>resource).key
17
+
13
18
  throw new CommercetoolsError<ConcurrentModificationError>(
14
19
  {
15
- message: `Object ${resource.id} has a different version than expected. Expected: ${expectedVersion} - Actual: ${resource.version}.`,
20
+ message: `Object ${identifier} has a different version than expected. Expected: ${expectedVersion} - Actual: ${resource.version}.`,
16
21
  currentVersion: resource.version,
17
22
  code: 'ConcurrentModification',
18
23
  },