@labdigital/commercetools-mock 0.5.23 → 0.6.2

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 (77) hide show
  1. package/dist/commercetools-mock.cjs.development.js +364 -298
  2. package/dist/commercetools-mock.cjs.development.js.map +1 -1
  3. package/dist/commercetools-mock.cjs.production.min.js +1 -1
  4. package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
  5. package/dist/commercetools-mock.esm.js +364 -298
  6. package/dist/commercetools-mock.esm.js.map +1 -1
  7. package/dist/repositories/abstract.d.ts +13 -9
  8. package/dist/repositories/cart-discount.d.ts +3 -3
  9. package/dist/repositories/cart.d.ts +13 -13
  10. package/dist/repositories/category.d.ts +11 -11
  11. package/dist/repositories/channel.d.ts +2 -2
  12. package/dist/repositories/custom-object.d.ts +3 -3
  13. package/dist/repositories/customer-group.d.ts +4 -4
  14. package/dist/repositories/customer.d.ts +4 -4
  15. package/dist/repositories/discount-code.d.ts +3 -3
  16. package/dist/repositories/extension.d.ts +3 -3
  17. package/dist/repositories/helpers.d.ts +3 -0
  18. package/dist/repositories/inventory-entry.d.ts +7 -7
  19. package/dist/repositories/my-order.d.ts +6 -0
  20. package/dist/repositories/order.d.ts +18 -17
  21. package/dist/repositories/payment.d.ts +8 -8
  22. package/dist/repositories/product-projection.d.ts +3 -3
  23. package/dist/repositories/product-type.d.ts +5 -5
  24. package/dist/repositories/product.d.ts +4 -4
  25. package/dist/repositories/project.d.ts +4 -4
  26. package/dist/repositories/shipping-method.d.ts +3 -3
  27. package/dist/repositories/shopping-list.d.ts +2 -2
  28. package/dist/repositories/state.d.ts +3 -3
  29. package/dist/repositories/store.d.ts +4 -4
  30. package/dist/repositories/subscription.d.ts +2 -2
  31. package/dist/repositories/tax-category.d.ts +4 -4
  32. package/dist/repositories/type.d.ts +3 -3
  33. package/dist/repositories/zone.d.ts +3 -3
  34. package/dist/services/my-order.d.ts +2 -2
  35. package/package.json +5 -2
  36. package/src/ctMock.ts +6 -0
  37. package/src/repositories/abstract.ts +37 -17
  38. package/src/repositories/cart-discount.ts +11 -11
  39. package/src/repositories/cart.ts +88 -36
  40. package/src/repositories/category.ts +17 -13
  41. package/src/repositories/channel.ts +3 -3
  42. package/src/repositories/custom-object.ts +16 -8
  43. package/src/repositories/customer-group.ts +5 -5
  44. package/src/repositories/customer.ts +13 -7
  45. package/src/repositories/discount-code.ts +14 -14
  46. package/src/repositories/extension.ts +12 -8
  47. package/src/repositories/helpers.ts +9 -0
  48. package/src/repositories/inventory-entry.ts +17 -10
  49. package/src/repositories/my-order.ts +19 -0
  50. package/src/repositories/order.test.ts +79 -3
  51. package/src/repositories/order.ts +77 -37
  52. package/src/repositories/payment.ts +21 -17
  53. package/src/repositories/product-projection.ts +5 -5
  54. package/src/repositories/product-type.ts +14 -10
  55. package/src/repositories/product.ts +5 -5
  56. package/src/repositories/project.ts +21 -17
  57. package/src/repositories/shipping-method.ts +27 -20
  58. package/src/repositories/shopping-list.ts +10 -6
  59. package/src/repositories/state.ts +13 -9
  60. package/src/repositories/store.ts +18 -14
  61. package/src/repositories/subscription.ts +3 -3
  62. package/src/repositories/tax-category.ts +16 -12
  63. package/src/repositories/type.ts +15 -11
  64. package/src/repositories/zone.ts +13 -9
  65. package/src/services/abstract.ts +21 -10
  66. package/src/services/cart.test.ts +48 -8
  67. package/src/services/cart.ts +17 -11
  68. package/src/services/custom-object.ts +8 -4
  69. package/src/services/customer.ts +5 -2
  70. package/src/services/my-customer.ts +7 -3
  71. package/src/services/my-order.ts +3 -3
  72. package/src/services/order.ts +3 -2
  73. package/src/services/product-projection.ts +2 -1
  74. package/src/services/product-type.ts +2 -1
  75. package/src/services/project.ts +4 -3
  76. package/src/services/store.ts +2 -1
  77. package/src/services/tax-category.ts +2 -1
@@ -7,32 +7,32 @@ import {
7
7
  } from '@commercetools/platform-sdk'
8
8
  import { Writable } from 'types'
9
9
  import { getBaseResourceProperties } from '../helpers'
10
- import { AbstractResourceRepository } from './abstract'
10
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
11
11
 
12
12
  export class CustomerGroupRepository extends AbstractResourceRepository {
13
13
  getTypeId(): ReferenceTypeId {
14
14
  return 'customer'
15
15
  }
16
- create(projectKey: string, draft: CustomerGroupDraft): CustomerGroup {
16
+ create(context: RepositoryContext, draft: CustomerGroupDraft): CustomerGroup {
17
17
  const resource: CustomerGroup = {
18
18
  ...getBaseResourceProperties(),
19
19
  key: draft.key,
20
20
  name: draft.groupName,
21
21
  }
22
- this.save(projectKey, resource)
22
+ this.save(context, resource)
23
23
  return resource
24
24
  }
25
25
 
26
26
  actions = {
27
27
  setKey: (
28
- projectKey: string,
28
+ context: RepositoryContext,
29
29
  resource: Writable<CustomerGroup>,
30
30
  { key }: CustomerGroupSetKeyAction
31
31
  ) => {
32
32
  resource.key = key
33
33
  },
34
34
  changeName: (
35
- projectKey: string,
35
+ context: RepositoryContext,
36
36
  resource: Writable<CustomerGroup>,
37
37
  { name }: CustomerGroupChangeNameAction
38
38
  ) => {
@@ -6,27 +6,33 @@ import {
6
6
  } from '@commercetools/platform-sdk'
7
7
  import { Writable } from 'types'
8
8
  import { getBaseResourceProperties } from '../helpers'
9
- import { AbstractResourceRepository } from './abstract'
9
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
10
10
 
11
11
  export class CustomerRepository extends AbstractResourceRepository {
12
12
  getTypeId(): ReferenceTypeId {
13
13
  return 'customer'
14
14
  }
15
15
 
16
- create(projectKey: string, draft: CustomerDraft): Customer {
16
+ create(context: RepositoryContext, draft: CustomerDraft): Customer {
17
17
  const resource: Customer = {
18
18
  ...getBaseResourceProperties(),
19
19
  email: draft.email,
20
- password: Buffer.from(draft.password).toString('base64'),
20
+ password: draft.password
21
+ ? Buffer.from(draft.password).toString('base64')
22
+ : undefined,
21
23
  isEmailVerified: draft.isEmailVerified || false,
22
24
  addresses: [],
23
25
  }
24
- this.save(projectKey, resource)
26
+ this.save(context, resource)
25
27
  return resource
26
28
  }
27
29
 
28
- getMe(projectKey: string): Customer | undefined {
29
- const results = this._storage.query(projectKey, this.getTypeId(), {}) // grab the first customer you can find
30
+ getMe(context: RepositoryContext): Customer | undefined {
31
+ const results = this._storage.query(
32
+ context.projectKey,
33
+ this.getTypeId(),
34
+ {}
35
+ ) // grab the first customer you can find
30
36
  if (results.count > 0) {
31
37
  return results.results[0] as Customer
32
38
  }
@@ -36,7 +42,7 @@ export class CustomerRepository extends AbstractResourceRepository {
36
42
 
37
43
  actions = {
38
44
  changeEmail: (
39
- _projectKey: string,
45
+ _context: RepositoryContext,
40
46
  resource: Writable<Customer>,
41
47
  { email }: CustomerChangeEmailAction
42
48
  ) => {
@@ -17,14 +17,14 @@ import {
17
17
  } from '@commercetools/platform-sdk'
18
18
  import { Writable } from 'types'
19
19
  import { getBaseResourceProperties } from '../helpers'
20
- import { AbstractResourceRepository } from './abstract'
20
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
21
21
 
22
22
  export class DiscountCodeRepository extends AbstractResourceRepository {
23
23
  getTypeId(): ReferenceTypeId {
24
24
  return 'cart-discount'
25
25
  }
26
26
 
27
- create(projectKey: string, draft: DiscountCodeDraft): DiscountCode {
27
+ create(context: RepositoryContext, draft: DiscountCodeDraft): DiscountCode {
28
28
  const resource: DiscountCode = {
29
29
  ...getBaseResourceProperties(),
30
30
  applicationVersion: 1,
@@ -46,7 +46,7 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
46
46
  maxApplications: draft.maxApplications,
47
47
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer,
48
48
  }
49
- this.save(projectKey, resource)
49
+ this.save(context, resource)
50
50
  return resource
51
51
  }
52
52
 
@@ -54,21 +54,21 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
54
54
  Record<
55
55
  DiscountCodeUpdateAction['action'],
56
56
  (
57
- projectKey: string,
57
+ context: RepositoryContext,
58
58
  resource: Writable<DiscountCode>,
59
59
  action: any
60
60
  ) => void
61
61
  >
62
62
  > = {
63
63
  changeIsActive: (
64
- projectKey: string,
64
+ context: RepositoryContext,
65
65
  resource: Writable<DiscountCode>,
66
66
  { isActive }: DiscountCodeChangeIsActiveAction
67
67
  ) => {
68
68
  resource.isActive = isActive
69
69
  },
70
70
  changeCartDiscounts: (
71
- projectKey: string,
71
+ context: RepositoryContext,
72
72
  resource: Writable<DiscountCode>,
73
73
  { cartDiscounts }: DiscountCodeChangeCartDiscountsAction
74
74
  ) => {
@@ -80,35 +80,35 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
80
80
  )
81
81
  },
82
82
  setDescription: (
83
- projectKey: string,
83
+ context: RepositoryContext,
84
84
  resource: Writable<DiscountCode>,
85
85
  { description }: DiscountCodeSetDescriptionAction
86
86
  ) => {
87
87
  resource.description = description
88
88
  },
89
89
  setCartPredicate: (
90
- projectKey: string,
90
+ context: RepositoryContext,
91
91
  resource: Writable<DiscountCode>,
92
92
  { cartPredicate }: DiscountCodeSetCartPredicateAction
93
93
  ) => {
94
94
  resource.cartPredicate = cartPredicate
95
95
  },
96
96
  setName: (
97
- projectKey: string,
97
+ context: RepositoryContext,
98
98
  resource: Writable<DiscountCode>,
99
99
  { name }: DiscountCodeSetNameAction
100
100
  ) => {
101
101
  resource.name = name
102
102
  },
103
103
  setMaxApplications: (
104
- projectKey: string,
104
+ context: RepositoryContext,
105
105
  resource: Writable<DiscountCode>,
106
106
  { maxApplications }: DiscountCodeSetMaxApplicationsAction
107
107
  ) => {
108
108
  resource.maxApplications = maxApplications
109
109
  },
110
110
  setMaxApplicationsPerCustomer: (
111
- projectKey: string,
111
+ context: RepositoryContext,
112
112
  resource: Writable<DiscountCode>,
113
113
  {
114
114
  maxApplicationsPerCustomer,
@@ -117,21 +117,21 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
117
117
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer
118
118
  },
119
119
  setValidFrom: (
120
- projectKey: string,
120
+ context: RepositoryContext,
121
121
  resource: Writable<DiscountCode>,
122
122
  { validFrom }: DiscountCodeSetValidFromAction
123
123
  ) => {
124
124
  resource.validFrom = validFrom
125
125
  },
126
126
  setValidUntil: (
127
- projectKey: string,
127
+ context: RepositoryContext,
128
128
  resource: Writable<DiscountCode>,
129
129
  { validUntil }: DiscountCodeSetValidUntilAction
130
130
  ) => {
131
131
  resource.validUntil = validUntil
132
132
  },
133
133
  setValidFromAndUntil: (
134
- projectKey: string,
134
+ context: RepositoryContext,
135
135
  resource: Writable<DiscountCode>,
136
136
  { validFrom, validUntil }: DiscountCodeSetValidFromAndUntilAction
137
137
  ) => {
@@ -10,14 +10,14 @@ import {
10
10
  } from '@commercetools/platform-sdk'
11
11
  import { Writable } from '../types'
12
12
  import { getBaseResourceProperties } from '../helpers'
13
- import { AbstractResourceRepository } from './abstract'
13
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
14
14
 
15
15
  export class ExtensionRepository extends AbstractResourceRepository {
16
16
  getTypeId(): ReferenceTypeId {
17
17
  return 'extension'
18
18
  }
19
19
 
20
- create(projectKey: string, draft: ExtensionDraft): Extension {
20
+ create(context: RepositoryContext, draft: ExtensionDraft): Extension {
21
21
  const resource: Extension = {
22
22
  ...getBaseResourceProperties(),
23
23
  key: draft.key,
@@ -25,37 +25,41 @@ export class ExtensionRepository extends AbstractResourceRepository {
25
25
  destination: draft.destination,
26
26
  triggers: draft.triggers,
27
27
  }
28
- this.save(projectKey, resource)
28
+ this.save(context, resource)
29
29
  return resource
30
30
  }
31
31
 
32
32
  actions: Record<
33
33
  ExtensionUpdateAction['action'],
34
- (projectKey: string, resource: Writable<Extension>, action: any) => void
34
+ (
35
+ context: RepositoryContext,
36
+ resource: Writable<Extension>,
37
+ action: any
38
+ ) => void
35
39
  > = {
36
40
  setKey: (
37
- projectKey: string,
41
+ context: RepositoryContext,
38
42
  resource: Writable<Extension>,
39
43
  { key }: ExtensionSetKeyAction
40
44
  ) => {
41
45
  resource.key = key
42
46
  },
43
47
  setTimeoutInMs: (
44
- projectKey: string,
48
+ context: RepositoryContext,
45
49
  resource: Writable<Extension>,
46
50
  { timeoutInMs }: ExtensionSetTimeoutInMsAction
47
51
  ) => {
48
52
  resource.timeoutInMs = timeoutInMs
49
53
  },
50
54
  changeTriggers: (
51
- projectKey: string,
55
+ context: RepositoryContext,
52
56
  resource: Writable<Extension>,
53
57
  { triggers }: ExtensionChangeTriggersAction
54
58
  ) => {
55
59
  resource.triggers = triggers
56
60
  },
57
61
  changeDestination: (
58
- projectKey: string,
62
+ context: RepositoryContext,
59
63
  resource: Writable<Extension>,
60
64
  { destination }: ExtensionChangeDestinationAction
61
65
  ) => {
@@ -14,6 +14,8 @@ import {
14
14
  TypedMoney,
15
15
  } from '@commercetools/platform-sdk'
16
16
  import { AbstractStorage } from '../storage'
17
+ import { RepositoryContext } from './abstract'
18
+ import { Request } from 'express'
17
19
 
18
20
  export const createCustomFields = (
19
21
  draft: CustomFieldsDraft | undefined,
@@ -96,3 +98,10 @@ export const getReferenceFromResourceIdentifier = <T extends Reference>(
96
98
  id: resource?.id,
97
99
  } as unknown) as T
98
100
  }
101
+
102
+ export const getRepositoryContext = (request: Request): RepositoryContext => {
103
+ return {
104
+ projectKey: request.params.projectKey,
105
+ storeKey: request.params.storeKey,
106
+ }
107
+ }
@@ -9,7 +9,7 @@ import {
9
9
  ReferenceTypeId,
10
10
  } from '@commercetools/platform-sdk'
11
11
  import { getBaseResourceProperties } from '../helpers'
12
- import { AbstractResourceRepository } from './abstract'
12
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
13
13
  import { createCustomFields } from './helpers'
14
14
  import { Writable } from '../types'
15
15
 
@@ -18,7 +18,10 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
18
18
  return 'inventory-entry'
19
19
  }
20
20
 
21
- create(projectKey: string, draft: InventoryEntryDraft): InventoryEntry {
21
+ create(
22
+ context: RepositoryContext,
23
+ draft: InventoryEntryDraft
24
+ ): InventoryEntry {
22
25
  const resource: InventoryEntry = {
23
26
  ...getBaseResourceProperties(),
24
27
  sku: draft.sku,
@@ -31,15 +34,19 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
31
34
  typeId: 'channel',
32
35
  id: draft.supplyChannel?.id ?? '',
33
36
  },
34
- custom: createCustomFields(draft.custom, projectKey, this._storage),
37
+ custom: createCustomFields(
38
+ draft.custom,
39
+ context.projectKey,
40
+ this._storage
41
+ ),
35
42
  }
36
- this.save(projectKey, resource)
43
+ this.save(context, resource)
37
44
  return resource
38
45
  }
39
46
 
40
47
  actions = {
41
48
  changeQuantity: (
42
- projectKey: string,
49
+ context: RepositoryContext,
43
50
  resource: Writable<InventoryEntry>,
44
51
  { quantity }: InventoryEntryChangeQuantityAction
45
52
  ) => {
@@ -48,14 +55,14 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
48
55
  resource.availableQuantity = quantity
49
56
  },
50
57
  setExpectedDelivery: (
51
- projectKey: string,
58
+ context: RepositoryContext,
52
59
  resource: Writable<InventoryEntry>,
53
60
  { expectedDelivery }: InventoryEntrySetExpectedDeliveryAction
54
61
  ) => {
55
62
  resource.expectedDelivery = new Date(expectedDelivery!).toISOString()
56
63
  },
57
64
  setCustomField: (
58
- projectKey: string,
65
+ context: RepositoryContext,
59
66
  resource: InventoryEntry,
60
67
  { name, value }: InventoryEntrySetCustomFieldAction
61
68
  ) => {
@@ -65,7 +72,7 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
65
72
  resource.custom.fields[name] = value
66
73
  },
67
74
  setCustomType: (
68
- projectKey: string,
75
+ context: RepositoryContext,
69
76
  resource: Writable<InventoryEntry>,
70
77
  { type, fields }: InventoryEntrySetCustomTypeAction
71
78
  ) => {
@@ -73,7 +80,7 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
73
80
  resource.custom = undefined
74
81
  } else {
75
82
  const resolvedType = this._storage.getByResourceIdentifier(
76
- projectKey,
83
+ context.projectKey,
77
84
  type
78
85
  )
79
86
  if (!resolvedType) {
@@ -90,7 +97,7 @@ export class InventoryEntryRepository extends AbstractResourceRepository {
90
97
  }
91
98
  },
92
99
  setRestockableInDays: (
93
- projectKey: string,
100
+ context: RepositoryContext,
94
101
  resource: Writable<InventoryEntry>,
95
102
  { restockableInDays }: InventoryEntrySetRestockableInDaysAction
96
103
  ) => {
@@ -0,0 +1,19 @@
1
+ import assert from 'assert'
2
+ import {
3
+ CartReference,
4
+ MyOrderFromCartDraft,
5
+ Order,
6
+ } from '@commercetools/platform-sdk'
7
+ import { OrderRepository } from './order'
8
+ import { RepositoryContext } from './abstract'
9
+
10
+ export class MyOrderRepository extends OrderRepository {
11
+ create(context: RepositoryContext, draft: MyOrderFromCartDraft): Order {
12
+ assert(draft.id, 'draft.id is missing')
13
+ const cartIdentifier = {
14
+ id: draft.id,
15
+ typeId: 'cart',
16
+ } as CartReference
17
+ return this.createFromCart(context, cartIdentifier)
18
+ }
19
+ }
@@ -1,10 +1,86 @@
1
- import { OrderImportDraft } from '@commercetools/platform-sdk'
1
+ import { Cart, OrderImportDraft } from '@commercetools/platform-sdk'
2
2
  import { OrderRepository } from './order'
3
3
  import { InMemoryStorage } from '../storage'
4
4
 
5
- describe('Order Import', () => {
5
+ describe('Order repository', () => {
6
6
  const storage = new InMemoryStorage()
7
7
  const repository = new OrderRepository(storage)
8
+
9
+ test('create from cart', async () => {
10
+ const cart: Cart = {
11
+ id: 'b3875a58-4ab2-4aaa-b399-184ce7561c27',
12
+ version: 1,
13
+ createdAt: '2021-09-02T12:23:30.036Z',
14
+ lastModifiedAt: '2021-09-02T12:23:30.546Z',
15
+ lineItems: [],
16
+ customLineItems: [],
17
+ totalPrice: {
18
+ type: 'centPrecision',
19
+ currencyCode: 'EUR',
20
+ centAmount: 10000,
21
+ fractionDigits: 2,
22
+ },
23
+ cartState: 'Active',
24
+ taxMode: 'Platform',
25
+ taxRoundingMode: 'HalfEven',
26
+ taxCalculationMode: 'UnitPriceLevel',
27
+ refusedGifts: [],
28
+ origin: 'Customer',
29
+ }
30
+
31
+ storage.add('dummy', 'cart', cart)
32
+
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
+ )
43
+ expect(result.cart?.id).toBe(cart.id)
44
+ })
45
+
46
+ test('create from cart - in store', async () => {
47
+ const cart: Cart = {
48
+ id: 'b3875a58-4ab2-4aaa-b399-184ce7561c27',
49
+ version: 1,
50
+ createdAt: '2021-09-02T12:23:30.036Z',
51
+ lastModifiedAt: '2021-09-02T12:23:30.546Z',
52
+ lineItems: [],
53
+ customLineItems: [],
54
+ totalPrice: {
55
+ type: 'centPrecision',
56
+ currencyCode: 'EUR',
57
+ centAmount: 10000,
58
+ fractionDigits: 2,
59
+ },
60
+ cartState: 'Active',
61
+ taxMode: 'Platform',
62
+ taxRoundingMode: 'HalfEven',
63
+ taxCalculationMode: 'UnitPriceLevel',
64
+ refusedGifts: [],
65
+ origin: 'Customer',
66
+ }
67
+
68
+ storage.add('dummy', 'cart', cart)
69
+
70
+ const result = repository.create(
71
+ { projectKey: 'dummy', storeKey: 'some-store' },
72
+ {
73
+ cart: {
74
+ id: cart.id,
75
+ typeId: 'cart',
76
+ },
77
+ version: cart.version,
78
+ }
79
+ )
80
+ expect(result.cart?.id).toBe(cart.id)
81
+ expect(result.store?.key).toBe('some-store')
82
+ })
83
+
8
84
  test('import exiting product', async () => {
9
85
  storage.add('dummy', 'product', {
10
86
  id: '15fc56ba-a74e-4cf8-b4b0-bada5c101541',
@@ -150,7 +226,7 @@ describe('Order Import', () => {
150
226
  ],
151
227
  }
152
228
 
153
- repository.import('dummy', draft)
229
+ repository.import({ projectKey: 'dummy' }, draft)
154
230
  })
155
231
  /*
156
232
  test('import non exiting product', async () => {