@labdigital/commercetools-mock 0.6.5 → 0.8.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 (118) hide show
  1. package/dist/index.d.ts +409 -3
  2. package/dist/index.global.js +49983 -0
  3. package/dist/index.global.js.map +1 -0
  4. package/dist/index.js +4835 -6
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +4803 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +36 -17
  9. package/src/ctMock.ts +5 -0
  10. package/src/helpers.ts +39 -0
  11. package/src/lib/projectionSearchFilter.test.ts +183 -0
  12. package/src/lib/projectionSearchFilter.ts +347 -0
  13. package/src/priceSelector.test.ts +96 -0
  14. package/src/priceSelector.ts +109 -0
  15. package/src/product-projection-search.ts +345 -0
  16. package/src/projectAPI.ts +19 -20
  17. package/src/repositories/category.ts +36 -0
  18. package/src/repositories/channel.ts +104 -0
  19. package/src/repositories/customer-group.ts +37 -0
  20. package/src/repositories/discount-code.ts +37 -0
  21. package/src/repositories/helpers.ts +46 -4
  22. package/src/repositories/product-discount.ts +181 -0
  23. package/src/repositories/product-projection.ts +30 -59
  24. package/src/repositories/product-type.ts +88 -6
  25. package/src/repositories/product.ts +49 -9
  26. package/src/repositories/shipping-method.ts +31 -0
  27. package/src/repositories/store.ts +43 -3
  28. package/src/repositories/type.ts +19 -0
  29. package/src/services/custom-object.test.ts +2 -2
  30. package/src/services/product-discount.ts +33 -0
  31. package/src/services/product-projection.test.ts +329 -107
  32. package/src/services/product.test.ts +12 -0
  33. package/src/storage.ts +116 -58
  34. package/src/types.ts +9 -2
  35. package/dist/commercetools-mock.cjs.development.js +0 -4382
  36. package/dist/commercetools-mock.cjs.development.js.map +0 -1
  37. package/dist/commercetools-mock.cjs.production.min.js +0 -2
  38. package/dist/commercetools-mock.cjs.production.min.js.map +0 -1
  39. package/dist/commercetools-mock.esm.js +0 -4374
  40. package/dist/commercetools-mock.esm.js.map +0 -1
  41. package/dist/constants.d.ts +0 -2
  42. package/dist/ctMock.d.ts +0 -32
  43. package/dist/exceptions.d.ts +0 -12
  44. package/dist/helpers.d.ts +0 -6
  45. package/dist/lib/expandParser.d.ts +0 -15
  46. package/dist/lib/filterParser.d.ts +0 -1
  47. package/dist/lib/haversine.d.ts +0 -8
  48. package/dist/lib/masking.d.ts +0 -1
  49. package/dist/lib/predicateParser.d.ts +0 -11
  50. package/dist/lib/proxy.d.ts +0 -1
  51. package/dist/oauth/errors.d.ts +0 -8
  52. package/dist/oauth/helpers.d.ts +0 -2
  53. package/dist/oauth/server.d.ts +0 -12
  54. package/dist/oauth/store.d.ts +0 -14
  55. package/dist/projectAPI.d.ts +0 -12
  56. package/dist/repositories/abstract.d.ts +0 -33
  57. package/dist/repositories/cart-discount.d.ts +0 -9
  58. package/dist/repositories/cart.d.ts +0 -21
  59. package/dist/repositories/category.d.ts +0 -18
  60. package/dist/repositories/channel.d.ts +0 -6
  61. package/dist/repositories/custom-object.d.ts +0 -8
  62. package/dist/repositories/customer-group.d.ts +0 -11
  63. package/dist/repositories/customer.d.ts +0 -11
  64. package/dist/repositories/discount-code.d.ts +0 -8
  65. package/dist/repositories/errors.d.ts +0 -2
  66. package/dist/repositories/extension.d.ts +0 -8
  67. package/dist/repositories/helpers.d.ts +0 -10
  68. package/dist/repositories/inventory-entry.d.ts +0 -14
  69. package/dist/repositories/my-order.d.ts +0 -6
  70. package/dist/repositories/order.d.ts +0 -26
  71. package/dist/repositories/payment.d.ts +0 -23
  72. package/dist/repositories/product-projection.d.ts +0 -10
  73. package/dist/repositories/product-type.d.ts +0 -10
  74. package/dist/repositories/product.d.ts +0 -11
  75. package/dist/repositories/project.d.ts +0 -8
  76. package/dist/repositories/shipping-method.d.ts +0 -10
  77. package/dist/repositories/shopping-list.d.ts +0 -6
  78. package/dist/repositories/state.d.ts +0 -8
  79. package/dist/repositories/store.d.ts +0 -10
  80. package/dist/repositories/subscription.d.ts +0 -6
  81. package/dist/repositories/tax-category.d.ts +0 -10
  82. package/dist/repositories/type.d.ts +0 -8
  83. package/dist/repositories/zone.d.ts +0 -8
  84. package/dist/server.d.ts +0 -1
  85. package/dist/services/abstract.d.ts +0 -20
  86. package/dist/services/cart-discount.d.ts +0 -9
  87. package/dist/services/cart.d.ts +0 -12
  88. package/dist/services/category.d.ts +0 -9
  89. package/dist/services/channel.d.ts +0 -9
  90. package/dist/services/custom-object.d.ts +0 -13
  91. package/dist/services/customer-group.d.ts +0 -9
  92. package/dist/services/customer.d.ts +0 -10
  93. package/dist/services/discount-code.d.ts +0 -9
  94. package/dist/services/extension.d.ts +0 -9
  95. package/dist/services/inventory-entry.d.ts +0 -9
  96. package/dist/services/my-cart.d.ts +0 -11
  97. package/dist/services/my-customer.d.ts +0 -13
  98. package/dist/services/my-order.d.ts +0 -10
  99. package/dist/services/my-payment.d.ts +0 -9
  100. package/dist/services/order.d.ts +0 -12
  101. package/dist/services/payment.d.ts +0 -9
  102. package/dist/services/product-projection.d.ts +0 -11
  103. package/dist/services/product-type.d.ts +0 -11
  104. package/dist/services/product.d.ts +0 -9
  105. package/dist/services/project.d.ts +0 -11
  106. package/dist/services/shipping-method.d.ts +0 -10
  107. package/dist/services/shopping-list.d.ts +0 -9
  108. package/dist/services/state.d.ts +0 -9
  109. package/dist/services/store.d.ts +0 -11
  110. package/dist/services/subscription.d.ts +0 -9
  111. package/dist/services/tax-category.d.ts +0 -11
  112. package/dist/services/type.d.ts +0 -9
  113. package/dist/services/zone.d.ts +0 -9
  114. package/dist/storage.d.ts +0 -56
  115. package/dist/types.d.ts +0 -89
  116. package/dist/validate.d.ts +0 -7482
  117. package/src/lib/filterParser.test.ts +0 -15
  118. package/src/lib/filterParser.ts +0 -17
@@ -1,10 +1,20 @@
1
1
  import {
2
2
  Channel,
3
+ ChannelChangeDescriptionAction,
4
+ ChannelChangeKeyAction,
5
+ ChannelChangeNameAction,
3
6
  ChannelDraft,
7
+ ChannelSetAddressAction,
8
+ ChannelSetCustomFieldAction,
9
+ ChannelSetCustomTypeAction,
10
+ ChannelSetGeoLocationAction,
11
+ ChannelUpdateAction,
4
12
  ReferenceTypeId,
5
13
  } from '@commercetools/platform-sdk'
14
+ import { Writable } from 'types'
6
15
  import { getBaseResourceProperties } from '../helpers'
7
16
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
17
+ import { createAddress, createCustomFields } from './helpers'
8
18
 
9
19
  export class ChannelRepository extends AbstractResourceRepository {
10
20
  getTypeId(): ReferenceTypeId {
@@ -15,9 +25,103 @@ export class ChannelRepository extends AbstractResourceRepository {
15
25
  const resource: Channel = {
16
26
  ...getBaseResourceProperties(),
17
27
  key: draft.key,
28
+ name: draft.name,
29
+ description: draft.description,
18
30
  roles: draft.roles || [],
31
+ geoLocation: draft.geoLocation,
32
+ address: createAddress(draft.address, context.projectKey, this._storage),
33
+ custom: createCustomFields(
34
+ draft.custom,
35
+ context.projectKey,
36
+ this._storage
37
+ ),
19
38
  }
20
39
  this.save(context, resource)
21
40
  return resource
22
41
  }
42
+
43
+ actions: Partial<
44
+ Record<
45
+ ChannelUpdateAction['action'],
46
+ (
47
+ context: RepositoryContext,
48
+ resource: Writable<Channel>,
49
+ action: any
50
+ ) => void
51
+ >
52
+ > = {
53
+ changeKey: (
54
+ context: RepositoryContext,
55
+ resource: Writable<Channel>,
56
+ { key }: ChannelChangeKeyAction
57
+ ) => {
58
+ resource.key = key
59
+ },
60
+
61
+ changeName: (
62
+ context: RepositoryContext,
63
+ resource: Writable<Channel>,
64
+ { name }: ChannelChangeNameAction
65
+ ) => {
66
+ resource.name = name
67
+ },
68
+
69
+ changeDescription: (
70
+ context: RepositoryContext,
71
+ resource: Writable<Channel>,
72
+ { description }: ChannelChangeDescriptionAction
73
+ ) => {
74
+ resource.description = description
75
+ },
76
+
77
+ setAddress: (
78
+ context: RepositoryContext,
79
+ resource: Writable<Channel>,
80
+ { address }: ChannelSetAddressAction
81
+ ) => {
82
+ resource.address = createAddress(
83
+ address,
84
+ context.projectKey,
85
+ this._storage
86
+ )
87
+ },
88
+
89
+ setGeoLocation: (
90
+ context: RepositoryContext,
91
+ resource: Writable<Channel>,
92
+ { geoLocation }: ChannelSetGeoLocationAction
93
+ ) => {
94
+ resource.geoLocation = geoLocation
95
+ },
96
+
97
+ setCustomType: (
98
+ context: RepositoryContext,
99
+ resource: Writable<Channel>,
100
+ { type, fields }: ChannelSetCustomTypeAction
101
+ ) => {
102
+ if (type) {
103
+ resource.custom = createCustomFields(
104
+ { type, fields },
105
+ context.projectKey,
106
+ this._storage
107
+ )
108
+ } else {
109
+ resource.custom = undefined
110
+ }
111
+ },
112
+ setCustomField: (
113
+ context: RepositoryContext,
114
+ resource: Writable<Channel>,
115
+ { name, value }: ChannelSetCustomFieldAction
116
+ ) => {
117
+ if (!resource.custom) {
118
+ return
119
+ }
120
+ if (value === null) {
121
+ delete resource.custom.fields[name]
122
+ } else {
123
+ resource.custom.fields[name] = value
124
+ }
125
+ },
126
+ }
23
127
  }
@@ -2,12 +2,15 @@ import {
2
2
  CustomerGroup,
3
3
  CustomerGroupChangeNameAction,
4
4
  CustomerGroupDraft,
5
+ CustomerGroupSetCustomFieldAction,
6
+ CustomerGroupSetCustomTypeAction,
5
7
  CustomerGroupSetKeyAction,
6
8
  ReferenceTypeId,
7
9
  } from '@commercetools/platform-sdk'
8
10
  import { Writable } from 'types'
9
11
  import { getBaseResourceProperties } from '../helpers'
10
12
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
13
+ import { createCustomFields } from './helpers'
11
14
 
12
15
  export class CustomerGroupRepository extends AbstractResourceRepository {
13
16
  getTypeId(): ReferenceTypeId {
@@ -18,6 +21,11 @@ export class CustomerGroupRepository extends AbstractResourceRepository {
18
21
  ...getBaseResourceProperties(),
19
22
  key: draft.key,
20
23
  name: draft.groupName,
24
+ custom: createCustomFields(
25
+ draft.custom,
26
+ context.projectKey,
27
+ this._storage
28
+ ),
21
29
  }
22
30
  this.save(context, resource)
23
31
  return resource
@@ -38,5 +46,34 @@ export class CustomerGroupRepository extends AbstractResourceRepository {
38
46
  ) => {
39
47
  resource.name = name
40
48
  },
49
+ setCustomType: (
50
+ context: RepositoryContext,
51
+ resource: Writable<CustomerGroup>,
52
+ { type, fields }: CustomerGroupSetCustomTypeAction
53
+ ) => {
54
+ if (type) {
55
+ resource.custom = createCustomFields(
56
+ { type, fields },
57
+ context.projectKey,
58
+ this._storage
59
+ )
60
+ } else {
61
+ resource.custom = undefined
62
+ }
63
+ },
64
+ setCustomField: (
65
+ context: RepositoryContext,
66
+ resource: Writable<CustomerGroup>,
67
+ { name, value }: CustomerGroupSetCustomFieldAction
68
+ ) => {
69
+ if (!resource.custom) {
70
+ return
71
+ }
72
+ if (value === null) {
73
+ delete resource.custom.fields[name]
74
+ } else {
75
+ resource.custom.fields[name] = value
76
+ }
77
+ },
41
78
  }
42
79
  }
@@ -5,6 +5,8 @@ import {
5
5
  DiscountCodeChangeIsActiveAction,
6
6
  DiscountCodeDraft,
7
7
  DiscountCodeSetCartPredicateAction,
8
+ DiscountCodeSetCustomFieldAction,
9
+ DiscountCodeSetCustomTypeAction,
8
10
  DiscountCodeSetDescriptionAction,
9
11
  DiscountCodeSetMaxApplicationsAction,
10
12
  DiscountCodeSetMaxApplicationsPerCustomerAction,
@@ -18,6 +20,7 @@ import {
18
20
  import { Writable } from 'types'
19
21
  import { getBaseResourceProperties } from '../helpers'
20
22
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
23
+ import { createCustomFields } from './helpers'
21
24
 
22
25
  export class DiscountCodeRepository extends AbstractResourceRepository {
23
26
  getTypeId(): ReferenceTypeId {
@@ -45,6 +48,11 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
45
48
  validUntil: draft.validUntil,
46
49
  maxApplications: draft.maxApplications,
47
50
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer,
51
+ custom: createCustomFields(
52
+ draft.custom,
53
+ context.projectKey,
54
+ this._storage
55
+ ),
48
56
  }
49
57
  this.save(context, resource)
50
58
  return resource
@@ -138,5 +146,34 @@ export class DiscountCodeRepository extends AbstractResourceRepository {
138
146
  resource.validFrom = validFrom
139
147
  resource.validUntil = validUntil
140
148
  },
149
+ setCustomType: (
150
+ context: RepositoryContext,
151
+ resource: Writable<DiscountCode>,
152
+ { type, fields }: DiscountCodeSetCustomTypeAction
153
+ ) => {
154
+ if (type) {
155
+ resource.custom = createCustomFields(
156
+ { type, fields },
157
+ context.projectKey,
158
+ this._storage
159
+ )
160
+ } else {
161
+ resource.custom = undefined
162
+ }
163
+ },
164
+ setCustomField: (
165
+ context: RepositoryContext,
166
+ resource: Writable<DiscountCode>,
167
+ { name, value }: DiscountCodeSetCustomFieldAction
168
+ ) => {
169
+ if (!resource.custom) {
170
+ return
171
+ }
172
+ if (value === null) {
173
+ delete resource.custom.fields[name]
174
+ } else {
175
+ resource.custom.fields[name] = value
176
+ }
177
+ },
141
178
  }
142
179
  }
@@ -1,11 +1,15 @@
1
1
  import { v4 as uuidv4 } from 'uuid'
2
2
  import {
3
+ Address,
4
+ BaseAddress,
3
5
  CustomFields,
4
6
  CustomFieldsDraft,
7
+ InvalidJsonInputError,
5
8
  Money,
6
9
  Price,
7
10
  PriceDraft,
8
11
  Reference,
12
+ ReferencedResourceNotFoundError,
9
13
  ResourceIdentifier,
10
14
  Store,
11
15
  StoreKeyReference,
@@ -13,9 +17,26 @@ import {
13
17
  Type,
14
18
  TypedMoney,
15
19
  } from '@commercetools/platform-sdk'
20
+ import { Request } from 'express'
16
21
  import { AbstractStorage } from '../storage'
17
22
  import { RepositoryContext } from './abstract'
18
- import { Request } from 'express'
23
+ import { CommercetoolsError } from '../exceptions'
24
+
25
+ export const createAddress = (
26
+ base: BaseAddress | undefined,
27
+ projectKey: string,
28
+ storage: AbstractStorage
29
+ ): Address | undefined => {
30
+ if (!base) return undefined
31
+
32
+ if (!base?.country) {
33
+ throw new Error('Country is required')
34
+ }
35
+
36
+ return {
37
+ ...base,
38
+ }
39
+ }
19
40
 
20
41
  export const createCustomFields = (
21
42
  draft: CustomFieldsDraft | undefined,
@@ -84,14 +105,35 @@ export const getReferenceFromResourceIdentifier = <T extends Reference>(
84
105
  projectKey: string,
85
106
  storage: AbstractStorage
86
107
  ): T => {
108
+ if (!resourceIdentifier.id && !resourceIdentifier.key) {
109
+ throw new CommercetoolsError<InvalidJsonInputError>(
110
+ {
111
+ code: 'InvalidJsonInput',
112
+ message: `${resourceIdentifier.typeId}: ResourceIdentifier requires an 'id' xor a 'key'`,
113
+ },
114
+ 400
115
+ )
116
+ }
117
+
87
118
  const resource = storage.getByResourceIdentifier(
88
119
  projectKey,
89
120
  resourceIdentifier
90
121
  )
91
- if (!resource)
92
- throw new Error(
93
- `resource type ${resourceIdentifier.typeId} with id ${resourceIdentifier.id} and key ${resourceIdentifier.key} not found`
122
+ if (!resource) {
123
+ const errIdentifier = resourceIdentifier.key
124
+ ? `key '${resourceIdentifier.key}'`
125
+ : `identifier '${resourceIdentifier.key}'`
126
+
127
+ throw new CommercetoolsError<ReferencedResourceNotFoundError>(
128
+ {
129
+ code: 'ReferencedResourceNotFound',
130
+ // @ts-ignore
131
+ typeId: resourceIdentifier.typeId,
132
+ message: `The referenced object of type '${resourceIdentifier.typeId}' with '${errIdentifier}' was not found. It either doesn't exist, or it can't be accessed from this endpoint (e.g., if the endpoint filters by store or customer account).`,
133
+ },
134
+ 400
94
135
  )
136
+ }
95
137
 
96
138
  return ({
97
139
  typeId: resourceIdentifier.typeId,
@@ -0,0 +1,181 @@
1
+ import {
2
+ ProductDiscount,
3
+ ProductDiscountChangeIsActiveAction,
4
+ ProductDiscountChangeNameAction,
5
+ ProductDiscountChangePredicateAction,
6
+ ProductDiscountChangeSortOrderAction,
7
+ ProductDiscountChangeValueAction,
8
+ ProductDiscountDraft,
9
+ ProductDiscountSetDescriptionAction,
10
+ ProductDiscountSetKeyAction,
11
+ ProductDiscountSetValidFromAction,
12
+ ProductDiscountSetValidFromAndUntilAction,
13
+ ProductDiscountSetValidUntilAction,
14
+ ProductDiscountUpdateAction,
15
+ ProductDiscountValue,
16
+ ProductDiscountValueAbsolute,
17
+ ProductDiscountValueDraft,
18
+ ProductDiscountValueExternal,
19
+ ProductDiscountValueRelative,
20
+ ReferenceTypeId,
21
+ } from '@commercetools/platform-sdk'
22
+ import { Writable } from 'types'
23
+ import { getBaseResourceProperties } from '../helpers'
24
+ import { AbstractResourceRepository, RepositoryContext } from './abstract'
25
+ import { createTypedMoney } from './helpers'
26
+
27
+ export class ProductDiscountRepository extends AbstractResourceRepository {
28
+ getTypeId(): ReferenceTypeId {
29
+ return 'product-discount'
30
+ }
31
+
32
+ create(
33
+ context: RepositoryContext,
34
+ draft: ProductDiscountDraft
35
+ ): ProductDiscount {
36
+ const resource: ProductDiscount = {
37
+ ...getBaseResourceProperties(),
38
+ key: draft.key,
39
+ name: draft.name,
40
+ description: draft.description,
41
+ value: this.transformValueDraft(draft.value),
42
+ predicate: draft.predicate,
43
+ sortOrder: draft.sortOrder,
44
+ isActive: draft.isActive || false,
45
+ validFrom: draft.validFrom,
46
+ validUntil: draft.validUntil,
47
+ references: [],
48
+ }
49
+ this.save(context, resource)
50
+ return resource
51
+ }
52
+
53
+ private transformValueDraft(
54
+ value: ProductDiscountValueDraft
55
+ ): ProductDiscountValue {
56
+ switch (value.type) {
57
+ case 'absolute': {
58
+ return {
59
+ type: 'absolute',
60
+ money: value.money.map(createTypedMoney),
61
+ } as ProductDiscountValueAbsolute
62
+ }
63
+ case 'external': {
64
+ return {
65
+ type: 'external',
66
+ } as ProductDiscountValueExternal
67
+ }
68
+ case 'relative': {
69
+ return {
70
+ ...value,
71
+ } as ProductDiscountValueRelative
72
+ }
73
+ }
74
+ }
75
+
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
+ actions: Partial<
96
+ Record<
97
+ ProductDiscountUpdateAction['action'],
98
+ (
99
+ context: RepositoryContext,
100
+ resource: Writable<ProductDiscount>,
101
+ action: any
102
+ ) => void
103
+ >
104
+ > = {
105
+ setKey: (
106
+ context: RepositoryContext,
107
+ resource: Writable<ProductDiscount>,
108
+ { key }: ProductDiscountSetKeyAction
109
+ ) => {
110
+ resource.key = key
111
+ },
112
+ setDescription: (
113
+ context: RepositoryContext,
114
+ resource: Writable<ProductDiscount>,
115
+ { description }: ProductDiscountSetDescriptionAction
116
+ ) => {
117
+ if (description && Object.keys(description).length > 0) {
118
+ resource.description = description
119
+ } else {
120
+ resource.description = undefined
121
+ }
122
+ },
123
+ changeName: (
124
+ context: RepositoryContext,
125
+ resource: Writable<ProductDiscount>,
126
+ { name }: ProductDiscountChangeNameAction
127
+ ) => {
128
+ resource.name = name
129
+ },
130
+ changeValue: (
131
+ context: RepositoryContext,
132
+ resource: Writable<ProductDiscount>,
133
+ { value }: ProductDiscountChangeValueAction
134
+ ) => {
135
+ resource.value = this.transformValueDraft(value)
136
+ },
137
+ changePredicate: (
138
+ context: RepositoryContext,
139
+ resource: Writable<ProductDiscount>,
140
+ { predicate }: ProductDiscountChangePredicateAction
141
+ ) => {
142
+ resource.predicate = predicate
143
+ },
144
+ changeSortOrder: (
145
+ context: RepositoryContext,
146
+ resource: Writable<ProductDiscount>,
147
+ { sortOrder }: ProductDiscountChangeSortOrderAction
148
+ ) => {
149
+ resource.sortOrder = sortOrder
150
+ },
151
+ changeIsActive: (
152
+ context: RepositoryContext,
153
+ resource: Writable<ProductDiscount>,
154
+ { isActive }: ProductDiscountChangeIsActiveAction
155
+ ) => {
156
+ resource.isActive = isActive
157
+ },
158
+ setValidFrom: (
159
+ context: RepositoryContext,
160
+ resource: Writable<ProductDiscount>,
161
+ { validFrom }: ProductDiscountSetValidFromAction
162
+ ) => {
163
+ resource.validFrom = validFrom
164
+ },
165
+ setValidUntil: (
166
+ context: RepositoryContext,
167
+ resource: Writable<ProductDiscount>,
168
+ { validUntil }: ProductDiscountSetValidUntilAction
169
+ ) => {
170
+ resource.validUntil = validUntil
171
+ },
172
+ setValidFromAndUntil: (
173
+ context: RepositoryContext,
174
+ resource: Writable<ProductDiscount>,
175
+ { validFrom, validUntil }: ProductDiscountSetValidFromAndUntilAction
176
+ ) => {
177
+ resource.validFrom = validFrom
178
+ resource.validUntil = validUntil
179
+ },
180
+ }
181
+ }
@@ -1,81 +1,52 @@
1
1
  import { ParsedQs } from 'qs'
2
+ import { ProductDraft, ProductProjection } from '@commercetools/platform-sdk'
2
3
  import {
3
- ProductDraft,
4
- ProductProjection,
5
- ProductVariant,
6
- ProductVariantDraft,
7
- } from '@commercetools/platform-sdk'
8
- import { getBaseResourceProperties } from '../helpers'
9
- import { AbstractResourceRepository, RepositoryContext } from './abstract'
4
+ AbstractResourceRepository,
5
+ QueryParams,
6
+ RepositoryContext,
7
+ } from './abstract'
10
8
  import { RepositoryTypes } from '../types'
11
- import { parseFilterExpression } from '../lib/filterParser'
9
+ import { AbstractStorage } from '../storage'
10
+ import { ProductProjectionSearch } from '../product-projection-search'
11
+ import { QueryParamsAsArray } from '../helpers'
12
12
 
13
13
  export class ProductProjectionRepository extends AbstractResourceRepository {
14
+ protected _searchService: ProductProjectionSearch
15
+
16
+ constructor(storage: AbstractStorage) {
17
+ super(storage)
18
+ this._searchService = new ProductProjectionSearch(storage)
19
+ }
20
+
14
21
  getTypeId(): RepositoryTypes {
15
22
  return 'product-projection'
16
23
  }
17
24
 
18
25
  create(context: RepositoryContext, draft: ProductDraft): ProductProjection {
19
- if (!draft.masterVariant) {
20
- throw new Error(
21
- `must provider mastervariant for product projection with key ${draft.key}`
22
- )
23
- }
24
-
25
- if (!draft.productType.id) {
26
- throw new Error(
27
- `must provider product type id for product projection with key ${draft.key}`
28
- )
29
- }
30
-
31
- const resource: ProductProjection = {
32
- ...getBaseResourceProperties(),
33
- name: draft.name,
34
- slug: draft.slug,
35
- categories: [],
36
- productType: { ...draft.productType, id: draft.productType.id! },
37
- masterVariant: variantFromDraft(0, draft.masterVariant!),
38
- variants:
39
- draft.variants?.map((variant, index) => {
40
- return variantFromDraft(index + 1, variant)
41
- }) ?? [],
42
-
43
- // @ts-ignore
44
- searchKeywords: draft.searchKeywords,
45
- }
46
-
47
- this.save(context, resource)
26
+ throw new Error('No valid action')
27
+ }
48
28
 
49
- return resource
29
+ query(context: RepositoryContext, params: QueryParams = {}) {
30
+ return this._storage.query(context.projectKey, 'product', {
31
+ expand: params.expand,
32
+ where: params.where,
33
+ offset: params.offset,
34
+ limit: params.limit,
35
+ })
50
36
  }
51
37
 
52
38
  search(context: RepositoryContext, query: ParsedQs) {
53
- const expand = query.expand
54
- ? (query.expand as string | string[])
55
- : undefined
56
- const filter = (query['filter.query'] ?? query.filter) as any
57
- const wherePredicate = filter ? parseFilterExpression(filter) : undefined
58
-
59
- const results = this._storage.query(context.projectKey, this.getTypeId(), {
60
- where: wherePredicate,
39
+ const results = this._searchService.search(context.projectKey, {
40
+ filter: QueryParamsAsArray(query.filter),
41
+ 'filter.query': QueryParamsAsArray(query['filter.query']),
42
+ facet: QueryParamsAsArray(query.facet),
61
43
  offset: query.offset ? Number(query.offset) : undefined,
62
44
  limit: query.limit ? Number(query.limit) : undefined,
63
- expand,
64
- }) //TODO: this is a partial implementation, but I don't really have the time to implement an actual search API right now
45
+ expand: QueryParamsAsArray(query.expand),
46
+ })
65
47
 
66
48
  return results
67
49
  }
68
50
 
69
51
  actions = {}
70
52
  }
71
-
72
- const variantFromDraft = (
73
- variantId: number,
74
- variant: ProductVariantDraft
75
- ): ProductVariant => {
76
- return {
77
- id: variantId,
78
- sku: variant?.sku,
79
- attributes: variant?.attributes,
80
- }
81
- }