@labdigital/commercetools-mock 2.17.1 → 2.18.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 (178) hide show
  1. package/dist/index.cjs +4186 -3974
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +266 -413
  4. package/dist/index.d.ts +266 -413
  5. package/dist/index.js +4186 -3974
  6. package/dist/index.js.map +1 -1
  7. package/package.json +44 -46
  8. package/src/constants.ts +2 -2
  9. package/src/ctMock.test.ts +11 -11
  10. package/src/ctMock.ts +141 -127
  11. package/src/deprecation.ts +8 -0
  12. package/src/exceptions.ts +17 -15
  13. package/src/helpers.ts +32 -32
  14. package/src/index.test.ts +128 -128
  15. package/src/index.ts +3 -3
  16. package/src/lib/expandParser.ts +13 -13
  17. package/src/lib/haversine.test.ts +9 -9
  18. package/src/lib/haversine.ts +11 -11
  19. package/src/lib/masking.ts +11 -11
  20. package/src/lib/parser.ts +2 -2
  21. package/src/lib/password.ts +23 -3
  22. package/src/lib/predicateParser.test.ts +185 -183
  23. package/src/lib/predicateParser.ts +234 -234
  24. package/src/lib/projectionSearchFilter.test.ts +103 -101
  25. package/src/lib/projectionSearchFilter.ts +152 -150
  26. package/src/lib/proxy.ts +5 -5
  27. package/src/oauth/errors.ts +4 -4
  28. package/src/oauth/helpers.ts +6 -6
  29. package/src/oauth/server.test.ts +86 -86
  30. package/src/oauth/server.ts +158 -144
  31. package/src/oauth/store.ts +44 -43
  32. package/src/priceSelector.test.ts +35 -35
  33. package/src/priceSelector.ts +30 -30
  34. package/src/product-projection-search.ts +136 -134
  35. package/src/projectAPI.test.ts +7 -7
  36. package/src/projectAPI.ts +24 -22
  37. package/src/repositories/abstract.ts +168 -116
  38. package/src/repositories/associate-role.ts +90 -77
  39. package/src/repositories/attribute-group.ts +51 -40
  40. package/src/repositories/business-unit.ts +168 -148
  41. package/src/repositories/cart/actions.ts +489 -0
  42. package/src/repositories/cart/helpers.ts +30 -0
  43. package/src/repositories/cart/index.ts +180 -0
  44. package/src/repositories/cart-discount/actions.ts +148 -0
  45. package/src/repositories/cart-discount/index.ts +86 -0
  46. package/src/repositories/category/actions.ts +231 -0
  47. package/src/repositories/category/index.ts +52 -0
  48. package/src/repositories/channel.ts +88 -90
  49. package/src/repositories/custom-object.ts +46 -45
  50. package/src/repositories/customer/actions.ts +165 -0
  51. package/src/repositories/customer/index.ts +79 -0
  52. package/src/repositories/customer-group.ts +66 -55
  53. package/src/repositories/discount-code/actions.ts +149 -0
  54. package/src/repositories/discount-code/index.ts +50 -0
  55. package/src/repositories/errors.ts +10 -10
  56. package/src/repositories/extension.ts +64 -62
  57. package/src/repositories/helpers.ts +117 -118
  58. package/src/repositories/index.ts +80 -79
  59. package/src/repositories/inventory-entry/actions.ts +84 -0
  60. package/src/repositories/inventory-entry/index.ts +44 -0
  61. package/src/repositories/my-customer.ts +114 -0
  62. package/src/repositories/my-order.ts +8 -8
  63. package/src/repositories/order/actions.ts +281 -0
  64. package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
  65. package/src/repositories/order/index.ts +260 -0
  66. package/src/repositories/order-edit.ts +10 -23
  67. package/src/repositories/payment/actions.ts +305 -0
  68. package/src/repositories/payment/helpers.ts +17 -0
  69. package/src/repositories/payment/index.ts +56 -0
  70. package/src/repositories/product/actions.ts +943 -0
  71. package/src/repositories/product/helpers.ts +98 -0
  72. package/src/repositories/product/index.ts +130 -0
  73. package/src/repositories/product-discount.ts +127 -117
  74. package/src/repositories/product-projection.ts +56 -62
  75. package/src/repositories/product-selection.ts +31 -28
  76. package/src/repositories/product-type.ts +136 -134
  77. package/src/repositories/project.ts +133 -118
  78. package/src/repositories/quote-request.ts +7 -19
  79. package/src/repositories/quote.ts +7 -22
  80. package/src/repositories/review.ts +13 -26
  81. package/src/repositories/shipping-method/actions.ts +198 -0
  82. package/src/repositories/shipping-method/helpers.ts +10 -0
  83. package/src/repositories/shipping-method/index.ts +138 -0
  84. package/src/repositories/shopping-list/actions.ts +295 -0
  85. package/src/repositories/shopping-list/index.ts +122 -0
  86. package/src/repositories/staged-quote.ts +7 -20
  87. package/src/repositories/standalone-price.ts +57 -44
  88. package/src/repositories/state.ts +113 -68
  89. package/src/repositories/store.ts +106 -94
  90. package/src/repositories/subscription.ts +46 -22
  91. package/src/repositories/tax-category/actions.ts +94 -0
  92. package/src/repositories/tax-category/helpers.ts +8 -0
  93. package/src/repositories/tax-category/index.ts +25 -0
  94. package/src/repositories/type/actions.ts +162 -0
  95. package/src/repositories/type/index.ts +24 -0
  96. package/src/repositories/zone.ts +62 -58
  97. package/src/server.ts +9 -9
  98. package/src/services/abstract.ts +75 -72
  99. package/src/services/associate-roles.test.ts +27 -27
  100. package/src/services/associate-roles.ts +7 -7
  101. package/src/services/attribute-group.ts +7 -7
  102. package/src/services/business-units.test.ts +28 -28
  103. package/src/services/business-units.ts +7 -7
  104. package/src/services/cart-discount.test.ts +199 -199
  105. package/src/services/cart-discount.ts +7 -7
  106. package/src/services/cart.test.ts +261 -261
  107. package/src/services/cart.ts +22 -21
  108. package/src/services/category.test.ts +121 -121
  109. package/src/services/category.ts +7 -7
  110. package/src/services/channel.ts +7 -7
  111. package/src/services/custom-object.test.ts +130 -130
  112. package/src/services/custom-object.ts +34 -31
  113. package/src/services/customer-group.ts +7 -7
  114. package/src/services/customer.test.ts +205 -205
  115. package/src/services/customer.ts +23 -36
  116. package/src/services/discount-code.ts +7 -7
  117. package/src/services/extension.ts +7 -7
  118. package/src/services/index.ts +85 -81
  119. package/src/services/inventory-entry.test.ts +106 -106
  120. package/src/services/inventory-entry.ts +7 -7
  121. package/src/services/my-cart.test.ts +56 -56
  122. package/src/services/my-cart.ts +20 -20
  123. package/src/services/my-customer.test.ts +155 -104
  124. package/src/services/my-customer.ts +61 -75
  125. package/src/services/my-order.ts +16 -16
  126. package/src/services/my-payment.test.ts +40 -40
  127. package/src/services/my-payment.ts +7 -7
  128. package/src/services/my-shopping-list.ts +7 -7
  129. package/src/services/order.test.ts +243 -243
  130. package/src/services/order.ts +23 -18
  131. package/src/services/payment.test.ts +40 -40
  132. package/src/services/payment.ts +7 -7
  133. package/src/services/product-discount.ts +7 -7
  134. package/src/services/product-projection.test.ts +190 -190
  135. package/src/services/product-projection.ts +34 -32
  136. package/src/services/product-selection.test.ts +19 -19
  137. package/src/services/product-selection.ts +7 -7
  138. package/src/services/product-type.test.ts +38 -38
  139. package/src/services/product-type.ts +7 -7
  140. package/src/services/product.test.ts +658 -656
  141. package/src/services/product.ts +7 -7
  142. package/src/services/project.test.ts +24 -24
  143. package/src/services/project.ts +17 -17
  144. package/src/services/reviews.ts +7 -7
  145. package/src/services/shipping-method.test.ts +78 -78
  146. package/src/services/shipping-method.ts +16 -16
  147. package/src/services/shopping-list.test.ts +170 -170
  148. package/src/services/shopping-list.ts +7 -7
  149. package/src/services/standalone-price.test.ts +112 -112
  150. package/src/services/standalone-price.ts +7 -7
  151. package/src/services/state.test.ts +30 -30
  152. package/src/services/state.ts +7 -7
  153. package/src/services/store.test.ts +40 -40
  154. package/src/services/store.ts +7 -7
  155. package/src/services/subscription.ts +7 -7
  156. package/src/services/tax-category.test.ts +43 -43
  157. package/src/services/tax-category.ts +7 -7
  158. package/src/services/type.ts +7 -7
  159. package/src/services/zone.ts +7 -7
  160. package/src/shippingCalculator.test.ts +43 -43
  161. package/src/shippingCalculator.ts +23 -23
  162. package/src/storage/abstract.ts +36 -34
  163. package/src/storage/in-memory.ts +237 -233
  164. package/src/storage/index.ts +2 -2
  165. package/src/types.ts +91 -91
  166. package/src/repositories/cart-discount.ts +0 -219
  167. package/src/repositories/cart.ts +0 -659
  168. package/src/repositories/category.ts +0 -256
  169. package/src/repositories/customer.ts +0 -228
  170. package/src/repositories/discount-code.ts +0 -181
  171. package/src/repositories/inventory-entry.ts +0 -109
  172. package/src/repositories/order.ts +0 -514
  173. package/src/repositories/payment.ts +0 -342
  174. package/src/repositories/product.ts +0 -1106
  175. package/src/repositories/shipping-method.ts +0 -312
  176. package/src/repositories/shopping-list.ts +0 -392
  177. package/src/repositories/tax-category.ts +0 -111
  178. package/src/repositories/type.ts +0 -172
@@ -1,256 +0,0 @@
1
- import type {
2
- Asset,
3
- AssetDraft,
4
- Category,
5
- CategoryAddAssetAction,
6
- CategoryChangeAssetNameAction,
7
- CategoryChangeNameAction,
8
- CategoryChangeParentAction,
9
- CategoryChangeSlugAction,
10
- CategoryDraft,
11
- CategoryRemoveAssetAction,
12
- CategorySetAssetDescriptionAction,
13
- CategorySetAssetSourcesAction,
14
- CategorySetCustomFieldAction,
15
- CategorySetCustomTypeAction,
16
- CategorySetDescriptionAction,
17
- CategorySetKeyAction,
18
- CategorySetMetaDescriptionAction,
19
- CategorySetMetaKeywordsAction,
20
- CategorySetMetaTitleAction,
21
- } from '@commercetools/platform-sdk'
22
- import { v4 as uuidv4 } from 'uuid'
23
- import { getBaseResourceProperties } from '../helpers.js'
24
- import type { Writable } from '../types.js'
25
- import {
26
- AbstractResourceRepository,
27
- type RepositoryContext,
28
- } from './abstract.js'
29
- import { createCustomFields } from './helpers.js'
30
-
31
- export class CategoryRepository extends AbstractResourceRepository<'category'> {
32
- getTypeId() {
33
- return 'category' as const
34
- }
35
-
36
- assetFromAssetDraft = (
37
- draft: AssetDraft,
38
- context: RepositoryContext
39
- ): Asset => ({
40
- ...draft,
41
- id: uuidv4(),
42
- custom: createCustomFields(draft.custom, context.projectKey, this._storage),
43
- })
44
-
45
- create(context: RepositoryContext, draft: CategoryDraft): Category {
46
- const resource: Category = {
47
- ...getBaseResourceProperties(),
48
- key: draft.key,
49
- name: draft.name,
50
- slug: draft.slug,
51
- orderHint: draft.orderHint || '',
52
- externalId: draft.externalId || '',
53
- parent: draft.parent
54
- ? { typeId: 'category', id: draft.parent.id! }
55
- : undefined,
56
- ancestors: [], // TODO
57
- assets:
58
- draft.assets?.map((d) => ({
59
- id: uuidv4(),
60
- name: d.name,
61
- description: d.description,
62
- sources: d.sources,
63
- tags: d.tags,
64
- key: d.key,
65
- custom: createCustomFields(
66
- draft.custom,
67
- context.projectKey,
68
- this._storage
69
- ),
70
- })) || [],
71
- custom: createCustomFields(
72
- draft.custom,
73
- context.projectKey,
74
- this._storage
75
- ),
76
- }
77
- this.saveNew(context, resource)
78
- return resource
79
- }
80
-
81
- actions = {
82
- changeAssetName: (
83
- context: RepositoryContext,
84
- resource: Writable<Category>,
85
- { assetId, assetKey, name }: CategoryChangeAssetNameAction
86
- ) => {
87
- resource.assets?.forEach((asset) => {
88
- if (assetId && assetId === asset.id) {
89
- asset.name = name
90
- }
91
- if (assetKey && assetKey === asset.key) {
92
- asset.name = name
93
- }
94
- })
95
- },
96
- changeSlug: (
97
- context: RepositoryContext,
98
- resource: Writable<Category>,
99
- { slug }: CategoryChangeSlugAction
100
- ) => {
101
- resource.slug = slug
102
- },
103
- changeName: (
104
- context: RepositoryContext,
105
- resource: Writable<Category>,
106
- { name }: CategoryChangeNameAction
107
- ) => {
108
- resource.name = name
109
- },
110
- changeParent: (
111
- context: RepositoryContext,
112
- resource: Writable<Category>,
113
- { parent }: CategoryChangeParentAction
114
- ) => {
115
- const category = this._storage.getByResourceIdentifier(
116
- context.projectKey,
117
- parent
118
- )
119
- if (!category) {
120
- throw new Error('No category found for reference')
121
- }
122
- resource.parent = {
123
- typeId: 'category',
124
- id: category.id,
125
- }
126
- },
127
- setKey: (
128
- context: RepositoryContext,
129
- resource: Writable<Category>,
130
- { key }: CategorySetKeyAction
131
- ) => {
132
- resource.key = key
133
- },
134
- setAssetDescription: (
135
- context: RepositoryContext,
136
- resource: Writable<Category>,
137
- { assetId, assetKey, description }: CategorySetAssetDescriptionAction
138
- ) => {
139
- resource.assets?.forEach((asset) => {
140
- if (assetId && assetId === asset.id) {
141
- asset.description = description
142
- }
143
- if (assetKey && assetKey === asset.key) {
144
- asset.description = description
145
- }
146
- })
147
- },
148
- setAssetSources: (
149
- context: RepositoryContext,
150
- resource: Writable<Category>,
151
- { assetId, assetKey, sources }: CategorySetAssetSourcesAction
152
- ) => {
153
- resource.assets?.forEach((asset) => {
154
- if (assetId && assetId === asset.id) {
155
- asset.sources = sources
156
- }
157
- if (assetKey && assetKey === asset.key) {
158
- asset.sources = sources
159
- }
160
- })
161
- },
162
- setDescription: (
163
- context: RepositoryContext,
164
- resource: Writable<Category>,
165
- { description }: CategorySetDescriptionAction
166
- ) => {
167
- resource.description = description
168
- },
169
- setMetaDescription: (
170
- context: RepositoryContext,
171
- resource: Writable<Category>,
172
- { metaDescription }: CategorySetMetaDescriptionAction
173
- ) => {
174
- resource.metaDescription = metaDescription
175
- },
176
- setMetaKeywords: (
177
- context: RepositoryContext,
178
- resource: Writable<Category>,
179
- { metaKeywords }: CategorySetMetaKeywordsAction
180
- ) => {
181
- resource.metaKeywords = metaKeywords
182
- },
183
- setMetaTitle: (
184
- context: RepositoryContext,
185
- resource: Writable<Category>,
186
- { metaTitle }: CategorySetMetaTitleAction
187
- ) => {
188
- resource.metaTitle = metaTitle
189
- },
190
- setCustomType: (
191
- context: RepositoryContext,
192
- resource: Writable<Category>,
193
- { type, fields }: CategorySetCustomTypeAction
194
- ) => {
195
- if (type) {
196
- resource.custom = createCustomFields(
197
- { type, fields },
198
- context.projectKey,
199
- this._storage
200
- )
201
- } else {
202
- resource.custom = undefined
203
- }
204
- },
205
- setCustomField: (
206
- context: RepositoryContext,
207
- resource: Writable<Category>,
208
- { name, value }: CategorySetCustomFieldAction
209
- ) => {
210
- if (!resource.custom) {
211
- return
212
- }
213
- if (value === null) {
214
- delete resource.custom.fields[name]
215
- } else {
216
- resource.custom.fields[name] = value
217
- }
218
- },
219
- removeAsset: (
220
- context: RepositoryContext,
221
- resource: Writable<Category>,
222
- { assetId, assetKey }: CategoryRemoveAssetAction
223
- ) => {
224
- if (!resource.assets) {
225
- return
226
- }
227
-
228
- if (assetId) {
229
- resource.assets = resource.assets.filter(function (obj) {
230
- return obj.id !== assetId
231
- })
232
-
233
- return
234
- }
235
-
236
- if (assetKey) {
237
- resource.assets = resource.assets.filter(function (obj) {
238
- return obj.key !== assetKey
239
- })
240
-
241
- return
242
- }
243
- },
244
- addAsset: (
245
- context: RepositoryContext,
246
- resource: Writable<Category>,
247
- { asset }: CategoryAddAssetAction
248
- ) => {
249
- if (!resource.assets) {
250
- resource.assets = [this.assetFromAssetDraft(asset, context)]
251
- } else {
252
- resource.assets.push(this.assetFromAssetDraft(asset, context))
253
- }
254
- },
255
- }
256
- }
@@ -1,228 +0,0 @@
1
- import type {
2
- Customer,
3
- CustomerChangeAddressAction,
4
- CustomerChangeEmailAction,
5
- CustomerDraft,
6
- CustomerSetAuthenticationModeAction,
7
- CustomerSetCompanyNameAction,
8
- CustomerSetCustomFieldAction,
9
- CustomerSetFirstNameAction,
10
- CustomerSetLastNameAction,
11
- CustomerSetVatIdAction,
12
- CustomerSetCustomerNumberAction,
13
- DuplicateFieldError,
14
- InvalidInputError,
15
- InvalidJsonInputError,
16
- } from '@commercetools/platform-sdk'
17
- import { CommercetoolsError } from '../exceptions.js'
18
- import { getBaseResourceProperties } from '../helpers.js'
19
- import type { Writable } from '../types.js'
20
- import {
21
- AbstractResourceRepository,
22
- type RepositoryContext,
23
- } from './abstract.js'
24
- import { hashPassword } from '../lib/password.js'
25
- import { createAddress } from './helpers.js'
26
-
27
- export class CustomerRepository extends AbstractResourceRepository<'customer'> {
28
- getTypeId() {
29
- return 'customer' as const
30
- }
31
-
32
- create(context: RepositoryContext, draft: CustomerDraft): Customer {
33
- // Check uniqueness
34
- const results = this._storage.query(context.projectKey, this.getTypeId(), {
35
- where: [`email="${draft.email.toLocaleLowerCase()}"`],
36
- })
37
- if (results.count > 0) {
38
- throw new CommercetoolsError<any>({
39
- code: 'CustomerAlreadyExists',
40
- statusCode: 400,
41
- message:
42
- 'There is already an existing customer with the provided email.',
43
- errors: [
44
- {
45
- code: 'DuplicateField',
46
- message: `Customer with email '${draft.email}' already exists.`,
47
- duplicateValue: draft.email,
48
- field: 'email',
49
- } as DuplicateFieldError,
50
- ],
51
- })
52
- }
53
-
54
- const resource: Customer = {
55
- ...getBaseResourceProperties(),
56
- authenticationMode: draft.authenticationMode || 'Password',
57
- email: draft.email.toLowerCase(),
58
- password: draft.password ? hashPassword(draft.password) : undefined,
59
- isEmailVerified: draft.isEmailVerified || false,
60
- addresses: [],
61
- customerNumber: draft.customerNumber,
62
- }
63
- this.saveNew(context, resource)
64
- return resource
65
- }
66
-
67
- getMe(context: RepositoryContext): Customer | undefined {
68
- // grab the first customer you can find for now. In the future we should
69
- // use the customer id from the scope of the token
70
- const results = this._storage.query(
71
- context.projectKey,
72
- this.getTypeId(),
73
- {}
74
- )
75
-
76
- if (results.count > 0) {
77
- return results.results[0] as Customer
78
- }
79
-
80
- return
81
- }
82
-
83
- deleteMe(context: RepositoryContext): Customer | undefined {
84
- // grab the first customer you can find for now. In the future we should
85
- // use the customer id from the scope of the token
86
- const results = this._storage.query(
87
- context.projectKey,
88
- this.getTypeId(),
89
- {}
90
- )
91
-
92
- if (results.count > 0) {
93
- return this.delete(context, results.results[0].id) as Customer
94
- }
95
-
96
- return
97
- }
98
-
99
- actions = {
100
- changeEmail: (
101
- _context: RepositoryContext,
102
- resource: Writable<Customer>,
103
- { email }: CustomerChangeEmailAction
104
- ) => {
105
- resource.email = email
106
- },
107
- setFirstName: (
108
- _context: RepositoryContext,
109
- resource: Writable<Customer>,
110
- { firstName }: CustomerSetFirstNameAction
111
- ) => {
112
- resource.firstName = firstName
113
- },
114
- setLastName: (
115
- _context: RepositoryContext,
116
- resource: Writable<Customer>,
117
- { lastName }: CustomerSetLastNameAction
118
- ) => {
119
- resource.lastName = lastName
120
- },
121
- setCompanyName: (
122
- _context: RepositoryContext,
123
- resource: Writable<Customer>,
124
- { companyName }: CustomerSetCompanyNameAction
125
- ) => {
126
- resource.companyName = companyName
127
- },
128
- setVatId: (
129
- _context: RepositoryContext,
130
- resource: Writable<Customer>,
131
- { vatId }: CustomerSetVatIdAction
132
- ) => {
133
- resource.vatId = vatId
134
- },
135
- changeAddress: (
136
- context: RepositoryContext,
137
- resource: Writable<Customer>,
138
- { addressId, addressKey, address }: CustomerChangeAddressAction
139
- ) => {
140
- const oldAddressIndex = resource.addresses.findIndex((a) => {
141
- if (a.id != undefined && addressId != undefined && a.id === addressId) {
142
- return true
143
- }
144
-
145
- return (
146
- a.key != undefined && addressKey != undefined && a.key === addressKey
147
- )
148
- })
149
-
150
- if (oldAddressIndex === -1) {
151
- throw new CommercetoolsError<InvalidInputError>(
152
- {
153
- code: 'InvalidInput',
154
- message: `Address with id '${addressId}' or key '${addressKey}' not found.`,
155
- },
156
- 400
157
- )
158
- }
159
-
160
- const newAddress = createAddress(
161
- address,
162
- context.projectKey,
163
- this._storage
164
- )
165
-
166
- if (newAddress) {
167
- resource.addresses[oldAddressIndex] = {
168
- id: addressId,
169
- ...newAddress,
170
- }
171
- }
172
- },
173
- setAuthenticationMode: (
174
- _context: RepositoryContext,
175
- resource: Writable<Customer>,
176
- { authMode, password }: CustomerSetAuthenticationModeAction
177
- ) => {
178
- if (resource.authenticationMode === authMode) {
179
- throw new CommercetoolsError<InvalidInputError>(
180
- {
181
- code: 'InvalidInput',
182
- message: `The customer is already using the '${resource.authenticationMode}' authentication mode.`,
183
- },
184
- 400
185
- )
186
- }
187
- resource.authenticationMode = authMode
188
- if (authMode === 'ExternalAuth') {
189
- delete resource.password
190
- return
191
- }
192
- if (authMode === 'Password') {
193
- resource.password = password ? hashPassword(password) : undefined
194
- return
195
- }
196
- throw new CommercetoolsError<InvalidJsonInputError>(
197
- {
198
- code: 'InvalidJsonInput',
199
- message: 'Request body does not contain valid JSON.',
200
- detailedErrorMessage: `actions -> authMode: Invalid enum value: '${authMode}'. Expected one of: 'Password','ExternalAuth'`,
201
- },
202
- 400
203
- )
204
- },
205
- setCustomField: (
206
- _context: RepositoryContext,
207
- resource: Writable<Customer>,
208
- { name, value }: CustomerSetCustomFieldAction
209
- ) => {
210
- if (!resource.custom) {
211
- throw new Error('Resource has no custom field')
212
- }
213
- resource.custom.fields[name] = value
214
- },
215
- setCustomerNumber: (
216
- _context: RepositoryContext,
217
- resource: Writable<Customer>,
218
- { customerNumber }: CustomerSetCustomerNumberAction
219
- ) => {
220
- if (resource.customerNumber) {
221
- throw new Error(
222
- 'A Customer number already exists and cannot be set again.'
223
- )
224
- }
225
- resource.customerNumber = customerNumber
226
- },
227
- }
228
- }
@@ -1,181 +0,0 @@
1
- import type {
2
- CartDiscountReference,
3
- DiscountCode,
4
- DiscountCodeChangeCartDiscountsAction,
5
- DiscountCodeChangeIsActiveAction,
6
- DiscountCodeDraft,
7
- DiscountCodeSetCartPredicateAction,
8
- DiscountCodeSetCustomFieldAction,
9
- DiscountCodeSetCustomTypeAction,
10
- DiscountCodeSetDescriptionAction,
11
- DiscountCodeSetMaxApplicationsAction,
12
- DiscountCodeSetMaxApplicationsPerCustomerAction,
13
- DiscountCodeSetNameAction,
14
- DiscountCodeSetValidFromAction,
15
- DiscountCodeSetValidFromAndUntilAction,
16
- DiscountCodeSetValidUntilAction,
17
- DiscountCodeUpdateAction,
18
- } from '@commercetools/platform-sdk'
19
- import { getBaseResourceProperties } from '../helpers.js'
20
- import type { Writable } from '../types.js'
21
- import {
22
- AbstractResourceRepository,
23
- type RepositoryContext,
24
- } from './abstract.js'
25
- import { createCustomFields } from './helpers.js'
26
-
27
- export class DiscountCodeRepository extends AbstractResourceRepository<'discount-code'> {
28
- getTypeId() {
29
- return 'discount-code' as const
30
- }
31
-
32
- create(context: RepositoryContext, draft: DiscountCodeDraft): DiscountCode {
33
- const resource: DiscountCode = {
34
- ...getBaseResourceProperties(),
35
- applicationVersion: 1,
36
- cartDiscounts: draft.cartDiscounts.map(
37
- (obj): CartDiscountReference => ({
38
- typeId: 'cart-discount',
39
- id: obj.id!,
40
- })
41
- ),
42
- cartPredicate: draft.cartPredicate,
43
- code: draft.code,
44
- description: draft.description,
45
- groups: draft.groups || [],
46
- isActive: draft.isActive || true,
47
- name: draft.name,
48
- references: [],
49
- validFrom: draft.validFrom,
50
- validUntil: draft.validUntil,
51
- maxApplications: draft.maxApplications,
52
- maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer,
53
- custom: createCustomFields(
54
- draft.custom,
55
- context.projectKey,
56
- this._storage
57
- ),
58
- }
59
- this.saveNew(context, resource)
60
- return resource
61
- }
62
-
63
- actions: Partial<
64
- Record<
65
- DiscountCodeUpdateAction['action'],
66
- (
67
- context: RepositoryContext,
68
- resource: Writable<DiscountCode>,
69
- action: any
70
- ) => void
71
- >
72
- > = {
73
- changeIsActive: (
74
- context: RepositoryContext,
75
- resource: Writable<DiscountCode>,
76
- { isActive }: DiscountCodeChangeIsActiveAction
77
- ) => {
78
- resource.isActive = isActive
79
- },
80
- changeCartDiscounts: (
81
- context: RepositoryContext,
82
- resource: Writable<DiscountCode>,
83
- { cartDiscounts }: DiscountCodeChangeCartDiscountsAction
84
- ) => {
85
- resource.cartDiscounts = cartDiscounts.map(
86
- (obj): CartDiscountReference => ({
87
- typeId: 'cart-discount',
88
- id: obj.id!,
89
- })
90
- )
91
- },
92
- setDescription: (
93
- context: RepositoryContext,
94
- resource: Writable<DiscountCode>,
95
- { description }: DiscountCodeSetDescriptionAction
96
- ) => {
97
- resource.description = description
98
- },
99
- setCartPredicate: (
100
- context: RepositoryContext,
101
- resource: Writable<DiscountCode>,
102
- { cartPredicate }: DiscountCodeSetCartPredicateAction
103
- ) => {
104
- resource.cartPredicate = cartPredicate
105
- },
106
- setName: (
107
- context: RepositoryContext,
108
- resource: Writable<DiscountCode>,
109
- { name }: DiscountCodeSetNameAction
110
- ) => {
111
- resource.name = name
112
- },
113
- setMaxApplications: (
114
- context: RepositoryContext,
115
- resource: Writable<DiscountCode>,
116
- { maxApplications }: DiscountCodeSetMaxApplicationsAction
117
- ) => {
118
- resource.maxApplications = maxApplications
119
- },
120
- setMaxApplicationsPerCustomer: (
121
- context: RepositoryContext,
122
- resource: Writable<DiscountCode>,
123
- {
124
- maxApplicationsPerCustomer,
125
- }: DiscountCodeSetMaxApplicationsPerCustomerAction
126
- ) => {
127
- resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer
128
- },
129
- setValidFrom: (
130
- context: RepositoryContext,
131
- resource: Writable<DiscountCode>,
132
- { validFrom }: DiscountCodeSetValidFromAction
133
- ) => {
134
- resource.validFrom = validFrom
135
- },
136
- setValidUntil: (
137
- context: RepositoryContext,
138
- resource: Writable<DiscountCode>,
139
- { validUntil }: DiscountCodeSetValidUntilAction
140
- ) => {
141
- resource.validUntil = validUntil
142
- },
143
- setValidFromAndUntil: (
144
- context: RepositoryContext,
145
- resource: Writable<DiscountCode>,
146
- { validFrom, validUntil }: DiscountCodeSetValidFromAndUntilAction
147
- ) => {
148
- resource.validFrom = validFrom
149
- resource.validUntil = validUntil
150
- },
151
- setCustomType: (
152
- context: RepositoryContext,
153
- resource: Writable<DiscountCode>,
154
- { type, fields }: DiscountCodeSetCustomTypeAction
155
- ) => {
156
- if (type) {
157
- resource.custom = createCustomFields(
158
- { type, fields },
159
- context.projectKey,
160
- this._storage
161
- )
162
- } else {
163
- resource.custom = undefined
164
- }
165
- },
166
- setCustomField: (
167
- context: RepositoryContext,
168
- resource: Writable<DiscountCode>,
169
- { name, value }: DiscountCodeSetCustomFieldAction
170
- ) => {
171
- if (!resource.custom) {
172
- return
173
- }
174
- if (value === null) {
175
- delete resource.custom.fields[name]
176
- } else {
177
- resource.custom.fields[name] = value
178
- }
179
- },
180
- }
181
- }