@labdigital/commercetools-mock 0.9.1 → 0.10.1

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 (108) hide show
  1. package/README.md +8 -0
  2. package/dist/index.d.ts +354 -188
  3. package/dist/index.global.js +2346 -2209
  4. package/dist/index.global.js.map +1 -1
  5. package/dist/index.js +1968 -1829
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +2171 -2032
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +30 -21
  10. package/src/constants.ts +4 -2
  11. package/src/ctMock.ts +27 -86
  12. package/src/helpers.ts +10 -11
  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.ts +93 -92
  17. package/src/lib/projectionSearchFilter.test.ts +28 -36
  18. package/src/lib/projectionSearchFilter.ts +88 -103
  19. package/src/oauth/store.ts +3 -3
  20. package/src/priceSelector.test.ts +16 -35
  21. package/src/priceSelector.ts +6 -9
  22. package/src/product-projection-search.ts +49 -57
  23. package/src/projectAPI.test.ts +7 -0
  24. package/src/projectAPI.ts +17 -22
  25. package/src/repositories/abstract.ts +102 -51
  26. package/src/repositories/cart-discount.ts +4 -5
  27. package/src/repositories/cart.ts +56 -46
  28. package/src/repositories/category.ts +23 -26
  29. package/src/repositories/channel.ts +5 -6
  30. package/src/repositories/custom-object.ts +41 -32
  31. package/src/repositories/customer-group.ts +4 -5
  32. package/src/repositories/customer.ts +42 -5
  33. package/src/repositories/discount-code.ts +5 -6
  34. package/src/repositories/errors.ts +10 -14
  35. package/src/repositories/extension.ts +16 -15
  36. package/src/repositories/helpers.ts +10 -15
  37. package/src/repositories/index.ts +75 -0
  38. package/src/repositories/inventory-entry.ts +5 -6
  39. package/src/repositories/my-order.ts +2 -2
  40. package/src/repositories/order-edit.ts +39 -0
  41. package/src/repositories/order.test.ts +16 -11
  42. package/src/repositories/order.ts +21 -14
  43. package/src/repositories/payment.ts +9 -10
  44. package/src/repositories/product-discount.ts +5 -25
  45. package/src/repositories/product-projection.ts +12 -5
  46. package/src/repositories/product-selection.ts +40 -0
  47. package/src/repositories/product-type.ts +38 -60
  48. package/src/repositories/product.ts +128 -85
  49. package/src/repositories/project.ts +16 -33
  50. package/src/repositories/quote-request.ts +28 -0
  51. package/src/repositories/quote.ts +28 -0
  52. package/src/repositories/review.ts +34 -0
  53. package/src/repositories/shipping-method.ts +25 -28
  54. package/src/repositories/shopping-list.ts +6 -6
  55. package/src/repositories/staged-quote.ts +29 -0
  56. package/src/repositories/standalone-price.ts +36 -0
  57. package/src/repositories/state.ts +16 -17
  58. package/src/repositories/store.ts +13 -29
  59. package/src/repositories/subscription.ts +4 -5
  60. package/src/repositories/tax-category.ts +9 -26
  61. package/src/repositories/type.ts +24 -27
  62. package/src/repositories/zone.ts +9 -11
  63. package/src/server.ts +5 -0
  64. package/src/services/abstract.ts +43 -12
  65. package/src/services/cart-discount.ts +3 -4
  66. package/src/services/cart.test.ts +9 -11
  67. package/src/services/cart.ts +42 -38
  68. package/src/services/category.test.ts +1 -2
  69. package/src/services/category.ts +3 -4
  70. package/src/services/channel.ts +3 -4
  71. package/src/services/custom-object.test.ts +6 -6
  72. package/src/services/custom-object.ts +4 -5
  73. package/src/services/customer-group.ts +3 -4
  74. package/src/services/customer.test.ts +136 -0
  75. package/src/services/customer.ts +5 -6
  76. package/src/services/discount-code.ts +3 -4
  77. package/src/services/extension.ts +3 -4
  78. package/src/services/index.ts +74 -0
  79. package/src/services/inventory-entry.test.ts +9 -13
  80. package/src/services/inventory-entry.ts +3 -4
  81. package/src/services/my-cart.test.ts +2 -0
  82. package/src/services/my-cart.ts +4 -5
  83. package/src/services/my-customer.ts +3 -4
  84. package/src/services/my-order.ts +4 -5
  85. package/src/services/my-payment.ts +3 -4
  86. package/src/services/order.test.ts +28 -26
  87. package/src/services/order.ts +4 -5
  88. package/src/services/payment.ts +3 -4
  89. package/src/services/product-discount.ts +3 -20
  90. package/src/services/product-projection.test.ts +76 -8
  91. package/src/services/product-projection.ts +4 -5
  92. package/src/services/product-type.ts +3 -20
  93. package/src/services/product.test.ts +200 -90
  94. package/src/services/product.ts +3 -4
  95. package/src/services/project.ts +5 -6
  96. package/src/services/shipping-method.ts +3 -4
  97. package/src/services/shopping-list.ts +3 -4
  98. package/src/services/state.ts +3 -4
  99. package/src/services/store.test.ts +11 -2
  100. package/src/services/store.ts +4 -21
  101. package/src/services/subscription.ts +3 -4
  102. package/src/services/tax-category.ts +3 -20
  103. package/src/services/type.ts +3 -4
  104. package/src/services/zone.ts +3 -4
  105. package/src/storage/abstract.ts +82 -0
  106. package/src/{storage.ts → storage/in-memory.ts} +79 -147
  107. package/src/storage/index.ts +2 -0
  108. package/src/types.ts +52 -83
@@ -6,29 +6,30 @@ import {
6
6
  ExtensionSetKeyAction,
7
7
  ExtensionSetTimeoutInMsAction,
8
8
  ExtensionUpdateAction,
9
- ReferenceTypeId,
10
9
  } from '@commercetools/platform-sdk'
11
- import { Writable } from '../types'
12
10
  import { getBaseResourceProperties } from '../helpers'
13
- import { AbstractResourceRepository, GetParams, RepositoryContext } from './abstract'
14
11
  import { maskSecretValue } from '../lib/masking'
12
+ import { Writable } from '../types'
13
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
15
14
 
16
- export class ExtensionRepository extends AbstractResourceRepository {
17
- getTypeId(): ReferenceTypeId {
18
- return 'extension'
15
+ export class ExtensionRepository extends AbstractResourceRepository<'extension'> {
16
+ getTypeId() {
17
+ return 'extension' as const
19
18
  }
20
19
 
21
- postProcessResource(resource: Extension) {
20
+ postProcessResource(resource: Extension): Extension {
22
21
  if (resource) {
23
- if (resource.destination.type === "HTTP" &&
24
- resource.destination.authentication?.type === "AuthorizationHeader"
22
+ const extension = resource as Extension
23
+ if (
24
+ extension.destination.type === 'HTTP' &&
25
+ extension.destination.authentication?.type === 'AuthorizationHeader'
25
26
  ) {
26
27
  return maskSecretValue(
27
- resource, 'destination.authentication.headerValue')
28
- }
29
- else if (resource.destination.type == "AWSLambda") {
30
- return maskSecretValue(
31
- resource, 'destination.accessSecret')
28
+ extension,
29
+ 'destination.authentication.headerValue'
30
+ )
31
+ } else if (extension.destination.type == 'AWSLambda') {
32
+ return maskSecretValue(resource, 'destination.accessSecret')
32
33
  }
33
34
  }
34
35
  return resource
@@ -42,7 +43,7 @@ export class ExtensionRepository extends AbstractResourceRepository {
42
43
  destination: draft.destination,
43
44
  triggers: draft.triggers,
44
45
  }
45
- this.save(context, resource)
46
+ this.saveNew(context, resource)
46
47
  return resource
47
48
  }
48
49
 
@@ -1,4 +1,3 @@
1
- import { v4 as uuidv4 } from 'uuid'
2
1
  import {
3
2
  Address,
4
3
  BaseAddress,
@@ -18,9 +17,10 @@ import {
18
17
  TypedMoney,
19
18
  } from '@commercetools/platform-sdk'
20
19
  import { Request } from 'express'
20
+ import { v4 as uuidv4 } from 'uuid'
21
+ import { CommercetoolsError } from '../exceptions'
21
22
  import { AbstractStorage } from '../storage'
22
23
  import { RepositoryContext } from './abstract'
23
- import { CommercetoolsError } from '../exceptions'
24
24
 
25
25
  export const createAddress = (
26
26
  base: BaseAddress | undefined,
@@ -67,15 +67,12 @@ export const createCustomFields = (
67
67
  }
68
68
  }
69
69
 
70
- export const createPrice = (draft: PriceDraft): Price => {
71
- return {
72
- id: uuidv4(),
73
- value: createTypedMoney(draft.value),
74
- }
75
- }
70
+ export const createPrice = (draft: PriceDraft): Price => ({
71
+ id: uuidv4(),
72
+ value: createTypedMoney(draft.value),
73
+ })
76
74
 
77
75
  export const createTypedMoney = (value: Money): TypedMoney => {
78
-
79
76
  // Taken from https://docs.adyen.com/development-resources/currency-codes
80
77
  let fractionDigits = 2
81
78
  switch (value.currencyCode.toUpperCase()) {
@@ -175,9 +172,7 @@ export const getReferenceFromResourceIdentifier = <T extends Reference>(
175
172
  } as unknown as T
176
173
  }
177
174
 
178
- export const getRepositoryContext = (request: Request): RepositoryContext => {
179
- return {
180
- projectKey: request.params.projectKey,
181
- storeKey: request.params.storeKey,
182
- }
183
- }
175
+ export const getRepositoryContext = (request: Request): RepositoryContext => ({
176
+ projectKey: request.params.projectKey,
177
+ storeKey: request.params.storeKey,
178
+ })
@@ -0,0 +1,75 @@
1
+ import { AbstractStorage } from '../storage'
2
+ import { CartRepository } from './cart'
3
+ import { CartDiscountRepository } from './cart-discount'
4
+ import { CategoryRepository } from './category'
5
+ import { ChannelRepository } from './channel'
6
+ import { CustomObjectRepository } from './custom-object'
7
+ import { CustomerRepository } from './customer'
8
+ import { CustomerGroupRepository } from './customer-group'
9
+ import { DiscountCodeRepository } from './discount-code'
10
+ import { ExtensionRepository } from './extension'
11
+ import { InventoryEntryRepository } from './inventory-entry'
12
+ import { MyOrderRepository } from './my-order'
13
+ import { OrderRepository } from './order'
14
+ import { OrderEditRepository } from './order-edit'
15
+ import { PaymentRepository } from './payment'
16
+ import { ProductRepository } from './product'
17
+ import { ProductDiscountRepository } from './product-discount'
18
+ import { ProductProjectionRepository } from './product-projection'
19
+ import { ProductSelectionRepository } from './product-selection'
20
+ import { ProductTypeRepository } from './product-type'
21
+ import { ProjectRepository } from './project'
22
+ import { QuoteRepository } from './quote'
23
+ import { QuoteRequestRepository } from './quote-request'
24
+ import { ReviewRepository } from './review'
25
+ import { ShippingMethodRepository } from './shipping-method'
26
+ import { ShoppingListRepository } from './shopping-list'
27
+ import { StagedQuoteRepository } from './staged-quote'
28
+ import { StandAlonePriceRepository } from './standalone-price'
29
+ import { StateRepository } from './state'
30
+ import { StoreRepository } from './store'
31
+ import { SubscriptionRepository } from './subscription'
32
+ import { TaxCategoryRepository } from './tax-category'
33
+ import { TypeRepository } from './type'
34
+ import { ZoneRepository } from './zone'
35
+
36
+ export type RepositoryMap = ReturnType<typeof createRepositories>
37
+
38
+ export const createRepositories = (storage: AbstractStorage) => ({
39
+ category: new CategoryRepository(storage),
40
+ cart: new CartRepository(storage),
41
+ 'cart-discount': new CartDiscountRepository(storage),
42
+ customer: new CustomerRepository(storage),
43
+ channel: new ChannelRepository(storage),
44
+ 'customer-group': new CustomerGroupRepository(storage),
45
+ 'discount-code': new DiscountCodeRepository(storage),
46
+ extension: new ExtensionRepository(storage),
47
+ 'inventory-entry': new InventoryEntryRepository(storage),
48
+ 'key-value-document': new CustomObjectRepository(storage),
49
+ order: new OrderRepository(storage),
50
+ 'order-edit': new OrderEditRepository(storage),
51
+ payment: new PaymentRepository(storage),
52
+ 'my-cart': new CartRepository(storage),
53
+ 'my-order': new MyOrderRepository(storage),
54
+ 'my-customer': new CustomerRepository(storage),
55
+ 'my-payment': new PaymentRepository(storage),
56
+ product: new ProductRepository(storage),
57
+ 'product-type': new ProductTypeRepository(storage),
58
+ 'product-discount': new ProductDiscountRepository(storage),
59
+ 'product-projection': new ProductProjectionRepository(storage),
60
+ 'product-selection': new ProductSelectionRepository(storage),
61
+ project: new ProjectRepository(storage),
62
+ review: new ReviewRepository(storage),
63
+ quote: new QuoteRepository(storage),
64
+ 'quote-request': new QuoteRequestRepository(storage),
65
+ 'shipping-method': new ShippingMethodRepository(storage),
66
+ 'shopping-list': new ShoppingListRepository(storage),
67
+ 'staged-quote': new StagedQuoteRepository(storage),
68
+ 'standalone-price': new StandAlonePriceRepository(storage),
69
+ state: new StateRepository(storage),
70
+ store: new StoreRepository(storage),
71
+ subscription: new SubscriptionRepository(storage),
72
+ 'tax-category': new TaxCategoryRepository(storage),
73
+ type: new TypeRepository(storage),
74
+ zone: new ZoneRepository(storage),
75
+ })
@@ -6,16 +6,15 @@ import {
6
6
  InventoryEntrySetCustomTypeAction,
7
7
  InventoryEntrySetExpectedDeliveryAction,
8
8
  InventoryEntrySetRestockableInDaysAction,
9
- ReferenceTypeId,
10
9
  } from '@commercetools/platform-sdk'
11
10
  import { getBaseResourceProperties } from '../helpers'
11
+ import { Writable } from '../types'
12
12
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
13
13
  import { createCustomFields } from './helpers'
14
- import { Writable } from '../types'
15
14
 
16
- export class InventoryEntryRepository extends AbstractResourceRepository {
17
- getTypeId(): ReferenceTypeId {
18
- return 'inventory-entry'
15
+ export class InventoryEntryRepository extends AbstractResourceRepository<'inventory-entry'> {
16
+ getTypeId() {
17
+ return 'inventory-entry' as const
19
18
  }
20
19
 
21
20
  create(
@@ -40,7 +39,7 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
40
39
  this._storage
41
40
  ),
42
41
  }
43
- this.save(context, resource)
42
+ this.saveNew(context, resource)
44
43
  return resource
45
44
  }
46
45
 
@@ -1,11 +1,11 @@
1
- import assert from 'assert'
2
1
  import {
3
2
  CartReference,
4
3
  MyOrderFromCartDraft,
5
4
  Order,
6
5
  } from '@commercetools/platform-sdk'
7
- import { OrderRepository } from './order'
6
+ import assert from 'assert'
8
7
  import { RepositoryContext } from './abstract'
8
+ import { OrderRepository } from './order'
9
9
 
10
10
  export class MyOrderRepository extends OrderRepository {
11
11
  create(context: RepositoryContext, draft: MyOrderFromCartDraft): Order {
@@ -0,0 +1,39 @@
1
+ import {
2
+ OrderEdit,
3
+ OrderEditDraft,
4
+ OrderEditResult,
5
+ OrderEditUpdateAction,
6
+ } from '@commercetools/platform-sdk'
7
+ import { getBaseResourceProperties } from '../helpers'
8
+ import { Writable } from '../types'
9
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
10
+
11
+ export class OrderEditRepository extends AbstractResourceRepository<'order-edit'> {
12
+ getTypeId() {
13
+ return 'order-edit' as const
14
+ }
15
+
16
+ create(context: RepositoryContext, draft: OrderEditDraft): OrderEdit {
17
+ const resource: OrderEdit = {
18
+ ...getBaseResourceProperties(),
19
+ stagedActions: draft.stagedActions ?? [],
20
+ resource: draft.resource,
21
+ result: {
22
+ type: 'NotProcessed',
23
+ } as OrderEditResult,
24
+ }
25
+ this.saveNew(context, resource)
26
+ return resource
27
+ }
28
+
29
+ actions: Partial<
30
+ Record<
31
+ OrderEditUpdateAction['action'],
32
+ (
33
+ context: RepositoryContext,
34
+ resource: Writable<OrderEdit>,
35
+ action: any
36
+ ) => void
37
+ >
38
+ > = {}
39
+ }
@@ -1,6 +1,6 @@
1
1
  import { Cart, OrderImportDraft } from '@commercetools/platform-sdk'
2
- import { OrderRepository } from './order'
3
2
  import { InMemoryStorage } from '../storage'
3
+ import { OrderRepository } from './order'
4
4
 
5
5
  describe('Order repository', () => {
6
6
  const storage = new InMemoryStorage()
@@ -21,6 +21,8 @@ describe('Order repository', () => {
21
21
  fractionDigits: 2,
22
22
  },
23
23
  cartState: 'Active',
24
+ shippingMode: 'Single',
25
+ shipping: [],
24
26
  taxMode: 'Platform',
25
27
  taxRoundingMode: 'HalfEven',
26
28
  taxCalculationMode: 'UnitPriceLevel',
@@ -29,18 +31,19 @@ describe('Order repository', () => {
29
31
  }
30
32
 
31
33
  storage.add('dummy', 'cart', cart)
34
+ const ctx = { projectKey: 'dummy' }
32
35
 
33
- const result = repository.create(
34
- { projectKey: 'dummy' },
35
- {
36
- cart: {
37
- id: cart.id,
38
- typeId: 'cart',
39
- },
40
- version: cart.version,
41
- }
42
- )
36
+ const result = repository.create(ctx, {
37
+ cart: {
38
+ id: cart.id,
39
+ typeId: 'cart',
40
+ },
41
+ version: cart.version,
42
+ })
43
43
  expect(result.cart?.id).toBe(cart.id)
44
+
45
+ const items = repository.query(ctx)
46
+ expect(items.count).toBe(1)
44
47
  })
45
48
 
46
49
  test('create from cart - in store', async () => {
@@ -58,6 +61,8 @@ describe('Order repository', () => {
58
61
  fractionDigits: 2,
59
62
  },
60
63
  cartState: 'Active',
64
+ shippingMode: 'Single',
65
+ shipping: [],
61
66
  taxMode: 'Platform',
62
67
  taxRoundingMode: 'HalfEven',
63
68
  taxCalculationMode: 'UnitPriceLevel',
@@ -1,4 +1,3 @@
1
- import assert from 'assert'
2
1
  import {
3
2
  Cart,
4
3
  CartReference,
@@ -25,10 +24,13 @@ import {
25
24
  Product,
26
25
  ProductPagedQueryResponse,
27
26
  ProductVariant,
28
- ReferenceTypeId,
29
27
  State,
30
28
  Store,
31
29
  } from '@commercetools/platform-sdk'
30
+ import assert from 'assert'
31
+ import { CommercetoolsError } from '../exceptions'
32
+ import { getBaseResourceProperties } from '../helpers'
33
+ import { Writable } from '../types'
32
34
  import {
33
35
  AbstractResourceRepository,
34
36
  QueryParams,
@@ -40,13 +42,10 @@ import {
40
42
  createTypedMoney,
41
43
  resolveStoreReference,
42
44
  } from './helpers'
43
- import { Writable } from '../types'
44
- import { getBaseResourceProperties } from '../helpers'
45
- import { CommercetoolsError } from '../exceptions'
46
45
 
47
- export class OrderRepository extends AbstractResourceRepository {
48
- getTypeId(): ReferenceTypeId {
49
- return 'order'
46
+ export class OrderRepository extends AbstractResourceRepository<'order'> {
47
+ getTypeId() {
48
+ return 'order' as const
50
49
  }
51
50
 
52
51
  create(context: RepositoryContext, draft: OrderFromCartDraft): Order {
@@ -85,6 +84,8 @@ export class OrderRepository extends AbstractResourceRepository {
85
84
  refusedGifts: [],
86
85
  origin: 'Customer',
87
86
  syncInfo: [],
87
+ shippingMode: cart.shippingMode,
88
+ shipping: cart.shipping,
88
89
  store: context.storeKey
89
90
  ? {
90
91
  key: context.storeKey,
@@ -93,7 +94,7 @@ export class OrderRepository extends AbstractResourceRepository {
93
94
  : undefined,
94
95
  lastMessageSequenceNumber: 0,
95
96
  }
96
- this.save(context, resource)
97
+ this.saveNew(context, resource)
97
98
  return resource
98
99
  }
99
100
 
@@ -118,6 +119,9 @@ export class OrderRepository extends AbstractResourceRepository {
118
119
  origin: draft.origin || 'Customer',
119
120
  paymentState: draft.paymentState,
120
121
  refusedGifts: [],
122
+ shippingMode: 'Single',
123
+ shipping: [],
124
+
121
125
  store: resolveStoreReference(
122
126
  draft.store,
123
127
  context.projectKey,
@@ -126,11 +130,11 @@ export class OrderRepository extends AbstractResourceRepository {
126
130
  syncInfo: [],
127
131
 
128
132
  lineItems:
129
- draft.lineItems?.map(item =>
133
+ draft.lineItems?.map((item) =>
130
134
  this.lineItemFromImportDraft.bind(this)(context, item)
131
135
  ) || [],
132
136
  customLineItems:
133
- draft.customLineItems?.map(item =>
137
+ draft.customLineItems?.map((item) =>
134
138
  this.customLineItemFromImportDraft.bind(this)(context, item)
135
139
  ) || [],
136
140
 
@@ -140,7 +144,7 @@ export class OrderRepository extends AbstractResourceRepository {
140
144
  fractionDigits: 2,
141
145
  },
142
146
  }
143
- this.save(context, resource)
147
+ this.saveNew(context, resource)
144
148
  return resource
145
149
  }
146
150
 
@@ -157,7 +161,7 @@ export class OrderRepository extends AbstractResourceRepository {
157
161
  sku: draft.variant.sku,
158
162
  }
159
163
 
160
- var items = this._storage.query(context.projectKey, 'product', {
164
+ const items = this._storage.query(context.projectKey, 'product', {
161
165
  where: [
162
166
  `masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`,
163
167
  ],
@@ -175,7 +179,7 @@ export class OrderRepository extends AbstractResourceRepository {
175
179
  variant = product.masterData.current.masterVariant
176
180
  } else {
177
181
  variant = product.masterData.current.variants.find(
178
- v => v.sku === draft.variant.sku
182
+ (v) => v.sku === draft.variant.sku
179
183
  )
180
184
  }
181
185
  if (!variant) {
@@ -202,6 +206,8 @@ export class OrderRepository extends AbstractResourceRepository {
202
206
  quantity: draft.quantity,
203
207
  state: draft.state || [],
204
208
  taxRate: draft.taxRate,
209
+ taxedPricePortions: [],
210
+ perMethodTaxRate: [],
205
211
  totalPrice: createTypedMoney(draft.price.value),
206
212
  variant: {
207
213
  id: variant.id,
@@ -228,6 +234,7 @@ export class OrderRepository extends AbstractResourceRepository {
228
234
  money: createTypedMoney(draft.money),
229
235
  name: draft.name,
230
236
  quantity: draft.quantity,
237
+ priceMode: draft.priceMode,
231
238
  slug: draft.slug,
232
239
  state: [],
233
240
  totalPrice: createTypedMoney(draft.money),
@@ -6,25 +6,24 @@ import {
6
6
  PaymentSetCustomFieldAction,
7
7
  PaymentSetCustomTypeAction,
8
8
  PaymentTransitionStateAction,
9
- ReferenceTypeId,
10
9
  State,
11
10
  StateReference,
12
11
  Transaction,
13
12
  TransactionDraft,
14
13
  } from '@commercetools/platform-sdk'
14
+ import { v4 as uuidv4 } from 'uuid'
15
+ import { getBaseResourceProperties } from '../helpers'
16
+ import { Writable } from '../types'
15
17
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
16
18
  import {
17
19
  createCustomFields,
18
20
  createTypedMoney,
19
21
  getReferenceFromResourceIdentifier,
20
22
  } from './helpers'
21
- import { getBaseResourceProperties } from '../helpers'
22
- import { v4 as uuidv4 } from 'uuid'
23
- import { Writable } from '../types'
24
23
 
25
- export class PaymentRepository extends AbstractResourceRepository {
26
- getTypeId(): ReferenceTypeId {
27
- return 'payment'
24
+ export class PaymentRepository extends AbstractResourceRepository<'payment'> {
25
+ getTypeId() {
26
+ return 'payment' as const
28
27
  }
29
28
 
30
29
  create(context: RepositoryContext, draft: PaymentDraft): Payment {
@@ -44,11 +43,11 @@ export class PaymentRepository extends AbstractResourceRepository {
44
43
  : undefined,
45
44
  }
46
45
  : {},
47
- transactions: (draft.transactions || []).map(t =>
46
+ transactions: (draft.transactions || []).map((t) =>
48
47
  this.transactionFromTransactionDraft(t, context)
49
48
  ),
50
49
  interfaceInteractions: (draft.interfaceInteractions || []).map(
51
- interaction =>
50
+ (interaction) =>
52
51
  createCustomFields(interaction, context.projectKey, this._storage)!
53
52
  ),
54
53
  custom: createCustomFields(
@@ -58,7 +57,7 @@ export class PaymentRepository extends AbstractResourceRepository {
58
57
  ),
59
58
  }
60
59
 
61
- this.save(context, resource)
60
+ this.saveNew(context, resource)
62
61
  return resource
63
62
  }
64
63
 
@@ -17,16 +17,15 @@ import {
17
17
  ProductDiscountValueDraft,
18
18
  ProductDiscountValueExternal,
19
19
  ProductDiscountValueRelative,
20
- ReferenceTypeId,
21
20
  } from '@commercetools/platform-sdk'
22
- import { Writable } from 'types'
23
21
  import { getBaseResourceProperties } from '../helpers'
22
+ import { Writable } from '../types'
24
23
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
25
24
  import { createTypedMoney } from './helpers'
26
25
 
27
- export class ProductDiscountRepository extends AbstractResourceRepository {
28
- getTypeId(): ReferenceTypeId {
29
- return 'product-discount'
26
+ export class ProductDiscountRepository extends AbstractResourceRepository<'product-discount'> {
27
+ getTypeId() {
28
+ return 'product-discount' as const
30
29
  }
31
30
 
32
31
  create(
@@ -46,7 +45,7 @@ export class ProductDiscountRepository extends AbstractResourceRepository {
46
45
  validUntil: draft.validUntil,
47
46
  references: [],
48
47
  }
49
- this.save(context, resource)
48
+ this.saveNew(context, resource)
50
49
  return resource
51
50
  }
52
51
 
@@ -73,25 +72,6 @@ export class ProductDiscountRepository extends AbstractResourceRepository {
73
72
  }
74
73
  }
75
74
 
76
- getWithKey(
77
- context: RepositoryContext,
78
- key: string
79
- ): ProductDiscount | undefined {
80
- const result = this._storage.query(context.projectKey, this.getTypeId(), {
81
- where: [`key="${key}"`],
82
- })
83
- if (result.count === 1) {
84
- return result.results[0] as ProductDiscount
85
- }
86
-
87
- // Catch this for now, should be checked when creating/updating
88
- if (result.count > 1) {
89
- throw new Error('Duplicate product discount key')
90
- }
91
-
92
- return
93
- }
94
-
95
75
  actions: Partial<
96
76
  Record<
97
77
  ProductDiscountUpdateAction['action'],
@@ -5,12 +5,11 @@ import {
5
5
  QueryParams,
6
6
  RepositoryContext,
7
7
  } from './abstract'
8
- import { RepositoryTypes } from '../types'
9
8
  import { AbstractStorage } from '../storage'
10
9
  import { ProductProjectionSearch } from '../product-projection-search'
11
10
  import { QueryParamsAsArray } from '../helpers'
12
11
 
13
- export class ProductProjectionRepository extends AbstractResourceRepository {
12
+ export class ProductProjectionRepository extends AbstractResourceRepository<'product-projection'> {
14
13
  protected _searchService: ProductProjectionSearch
15
14
 
16
15
  constructor(storage: AbstractStorage) {
@@ -18,8 +17,8 @@ export class ProductProjectionRepository extends AbstractResourceRepository {
18
17
  this._searchService = new ProductProjectionSearch(storage)
19
18
  }
20
19
 
21
- getTypeId(): RepositoryTypes {
22
- return 'product-projection'
20
+ getTypeId() {
21
+ return 'product-projection' as const
23
22
  }
24
23
 
25
24
  create(context: RepositoryContext, draft: ProductDraft): ProductProjection {
@@ -27,12 +26,19 @@ export class ProductProjectionRepository extends AbstractResourceRepository {
27
26
  }
28
27
 
29
28
  query(context: RepositoryContext, params: QueryParams = {}) {
30
- return this._storage.query(context.projectKey, 'product', {
29
+ const response = this._storage.query(context.projectKey, 'product', {
31
30
  expand: params.expand,
32
31
  where: params.where,
33
32
  offset: params.offset,
34
33
  limit: params.limit,
35
34
  })
35
+
36
+ return {
37
+ ...response,
38
+ results: response.results.map((r) =>
39
+ this._searchService.transform(r, false)
40
+ ),
41
+ }
36
42
  }
37
43
 
38
44
  search(context: RepositoryContext, query: ParsedQs) {
@@ -43,6 +49,7 @@ export class ProductProjectionRepository extends AbstractResourceRepository {
43
49
  offset: query.offset ? Number(query.offset) : undefined,
44
50
  limit: query.limit ? Number(query.limit) : undefined,
45
51
  expand: QueryParamsAsArray(query.expand),
52
+ staged: query.staged === 'true',
46
53
  })
47
54
 
48
55
  return results
@@ -0,0 +1,40 @@
1
+ import {
2
+ ProductSelection,
3
+ ProductSelectionDraft,
4
+ Review,
5
+ ReviewUpdateAction,
6
+ } from '@commercetools/platform-sdk'
7
+ import { getBaseResourceProperties } from '../helpers'
8
+ import { Writable } from '../types'
9
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
10
+
11
+ export class ProductSelectionRepository extends AbstractResourceRepository<'product-selection'> {
12
+ getTypeId() {
13
+ return 'product-selection' as const
14
+ }
15
+
16
+ create(
17
+ context: RepositoryContext,
18
+ draft: ProductSelectionDraft
19
+ ): ProductSelection {
20
+ const resource: ProductSelection = {
21
+ ...getBaseResourceProperties(),
22
+ productCount: 0,
23
+ name: draft.name,
24
+ type: 'individual',
25
+ }
26
+ this.saveNew(context, resource)
27
+ return resource
28
+ }
29
+
30
+ actions: Partial<
31
+ Record<
32
+ ReviewUpdateAction['action'],
33
+ (
34
+ context: RepositoryContext,
35
+ resource: Writable<Review>,
36
+ action: any
37
+ ) => void
38
+ >
39
+ > = {}
40
+ }