@labdigital/commercetools-mock 2.17.0 → 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 +4219 -3989
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +268 -415
  4. package/dist/index.d.ts +268 -415
  5. package/dist/index.js +4219 -3989
  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 +110 -67
  30. package/src/oauth/server.ts +161 -141
  31. package/src/oauth/store.ts +49 -44
  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 +31 -29
  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,109 +0,0 @@
1
- import type {
2
- InventoryEntry,
3
- InventoryEntryChangeQuantityAction,
4
- InventoryEntryDraft,
5
- InventoryEntrySetCustomFieldAction,
6
- InventoryEntrySetCustomTypeAction,
7
- InventoryEntrySetExpectedDeliveryAction,
8
- InventoryEntrySetRestockableInDaysAction,
9
- } from '@commercetools/platform-sdk'
10
- import { getBaseResourceProperties } from '../helpers.js'
11
- import type { Writable } from '../types.js'
12
- import {
13
- AbstractResourceRepository,
14
- type RepositoryContext,
15
- } from './abstract.js'
16
- import { createCustomFields } from './helpers.js'
17
-
18
- export class InventoryEntryRepository extends AbstractResourceRepository<'inventory-entry'> {
19
- getTypeId() {
20
- return 'inventory-entry' as const
21
- }
22
-
23
- create(
24
- context: RepositoryContext,
25
- draft: InventoryEntryDraft
26
- ): InventoryEntry {
27
- const resource: InventoryEntry = {
28
- ...getBaseResourceProperties(),
29
- sku: draft.sku,
30
- quantityOnStock: draft.quantityOnStock,
31
- availableQuantity: draft.quantityOnStock,
32
- expectedDelivery: draft.expectedDelivery,
33
- restockableInDays: draft.restockableInDays,
34
- supplyChannel: {
35
- ...draft.supplyChannel,
36
- typeId: 'channel',
37
- id: draft.supplyChannel?.id ?? '',
38
- },
39
- custom: createCustomFields(
40
- draft.custom,
41
- context.projectKey,
42
- this._storage
43
- ),
44
- }
45
- this.saveNew(context, resource)
46
- return resource
47
- }
48
-
49
- actions = {
50
- changeQuantity: (
51
- context: RepositoryContext,
52
- resource: Writable<InventoryEntry>,
53
- { quantity }: InventoryEntryChangeQuantityAction
54
- ) => {
55
- resource.quantityOnStock = quantity
56
- // don't know active reservations so just set to same value
57
- resource.availableQuantity = quantity
58
- },
59
- setExpectedDelivery: (
60
- context: RepositoryContext,
61
- resource: Writable<InventoryEntry>,
62
- { expectedDelivery }: InventoryEntrySetExpectedDeliveryAction
63
- ) => {
64
- resource.expectedDelivery = new Date(expectedDelivery!).toISOString()
65
- },
66
- setCustomField: (
67
- context: RepositoryContext,
68
- resource: InventoryEntry,
69
- { name, value }: InventoryEntrySetCustomFieldAction
70
- ) => {
71
- if (!resource.custom) {
72
- throw new Error('Resource has no custom field')
73
- }
74
- resource.custom.fields[name] = value
75
- },
76
- setCustomType: (
77
- context: RepositoryContext,
78
- resource: Writable<InventoryEntry>,
79
- { type, fields }: InventoryEntrySetCustomTypeAction
80
- ) => {
81
- if (!type) {
82
- resource.custom = undefined
83
- } else {
84
- const resolvedType = this._storage.getByResourceIdentifier(
85
- context.projectKey,
86
- type
87
- )
88
- if (!resolvedType) {
89
- throw new Error(`Type ${type} not found`)
90
- }
91
-
92
- resource.custom = {
93
- type: {
94
- typeId: 'type',
95
- id: resolvedType.id,
96
- },
97
- fields: fields || {},
98
- }
99
- }
100
- },
101
- setRestockableInDays: (
102
- context: RepositoryContext,
103
- resource: Writable<InventoryEntry>,
104
- { restockableInDays }: InventoryEntrySetRestockableInDaysAction
105
- ) => {
106
- resource.restockableInDays = restockableInDays
107
- },
108
- }
109
- }
@@ -1,514 +0,0 @@
1
- import type {
2
- Cart,
3
- CartReference,
4
- CustomLineItem,
5
- CustomLineItemImportDraft,
6
- CustomLineItemReturnItem,
7
- GeneralError,
8
- LineItem,
9
- LineItemImportDraft,
10
- LineItemReturnItem,
11
- Order,
12
- OrderAddPaymentAction,
13
- OrderAddReturnInfoAction,
14
- OrderChangeOrderStateAction,
15
- OrderChangePaymentStateAction,
16
- OrderFromCartDraft,
17
- OrderImportDraft,
18
- OrderUpdateSyncInfoAction,
19
- OrderSetBillingAddressAction,
20
- OrderSetCustomerEmailAction,
21
- OrderSetCustomFieldAction,
22
- OrderSetCustomTypeAction,
23
- OrderSetLocaleAction,
24
- OrderSetOrderNumberAction,
25
- OrderSetShippingAddressAction,
26
- OrderSetStoreAction,
27
- OrderTransitionStateAction,
28
- Product,
29
- ProductPagedQueryResponse,
30
- ProductVariant,
31
- ReturnInfo,
32
- State,
33
- Store,
34
- SyncInfo,
35
- } from '@commercetools/platform-sdk'
36
- import assert from 'assert'
37
- import { CommercetoolsError } from '../exceptions.js'
38
- import { getBaseResourceProperties } from '../helpers.js'
39
- import type { Writable } from '../types.js'
40
- import {
41
- AbstractResourceRepository,
42
- type QueryParams,
43
- RepositoryContext,
44
- } from './abstract.js'
45
- import {
46
- createAddress,
47
- createCentPrecisionMoney,
48
- createCustomFields,
49
- createPrice,
50
- createTypedMoney,
51
- resolveStoreReference,
52
- } from './helpers.js'
53
-
54
- export class OrderRepository extends AbstractResourceRepository<'order'> {
55
- getTypeId() {
56
- return 'order' as const
57
- }
58
-
59
- create(context: RepositoryContext, draft: OrderFromCartDraft): Order {
60
- assert(draft.cart, 'draft.cart is missing')
61
- return this.createFromCart(
62
- context,
63
- {
64
- id: draft.cart.id!,
65
- typeId: 'cart',
66
- },
67
- draft.orderNumber
68
- )
69
- }
70
-
71
- createFromCart(
72
- context: RepositoryContext,
73
- cartReference: CartReference,
74
- orderNumber?: string
75
- ) {
76
- const cart = this._storage.getByResourceIdentifier(
77
- context.projectKey,
78
- cartReference
79
- ) as Cart | null
80
- if (!cart) {
81
- throw new Error('Cannot find cart')
82
- }
83
-
84
- const resource: Order = {
85
- ...getBaseResourceProperties(),
86
- orderNumber,
87
- cart: cartReference,
88
- orderState: 'Open',
89
- lineItems: [],
90
- customLineItems: [],
91
- totalPrice: cart.totalPrice,
92
- refusedGifts: [],
93
- origin: 'Customer',
94
- syncInfo: [],
95
- shippingMode: cart.shippingMode,
96
- shipping: cart.shipping,
97
- store: context.storeKey
98
- ? {
99
- key: context.storeKey,
100
- typeId: 'store',
101
- }
102
- : undefined,
103
- lastMessageSequenceNumber: 0,
104
- }
105
- this.saveNew(context, resource)
106
- return resource
107
- }
108
-
109
- import(context: RepositoryContext, draft: OrderImportDraft): Order {
110
- // TODO: Check if order with given orderNumber already exists
111
- assert(this, 'OrderRepository not valid')
112
- const resource: Order = {
113
- ...getBaseResourceProperties(),
114
-
115
- billingAddress: createAddress(
116
- draft.billingAddress,
117
- context.projectKey,
118
- this._storage
119
- ),
120
- shippingAddress: createAddress(
121
- draft.shippingAddress,
122
- context.projectKey,
123
- this._storage
124
- ),
125
-
126
- custom: createCustomFields(
127
- draft.custom,
128
- context.projectKey,
129
- this._storage
130
- ),
131
- customerEmail: draft.customerEmail,
132
- lastMessageSequenceNumber: 0,
133
- orderNumber: draft.orderNumber,
134
- orderState: draft.orderState || 'Open',
135
- origin: draft.origin || 'Customer',
136
- paymentState: draft.paymentState,
137
- refusedGifts: [],
138
- shippingMode: 'Single',
139
- shipping: [],
140
-
141
- store: resolveStoreReference(
142
- draft.store,
143
- context.projectKey,
144
- this._storage
145
- ),
146
- syncInfo: [],
147
-
148
- lineItems:
149
- draft.lineItems?.map((item) =>
150
- this.lineItemFromImportDraft.bind(this)(context, item)
151
- ) || [],
152
- customLineItems:
153
- draft.customLineItems?.map((item) =>
154
- this.customLineItemFromImportDraft.bind(this)(context, item)
155
- ) || [],
156
-
157
- totalPrice: createCentPrecisionMoney(draft.totalPrice),
158
- }
159
- this.saveNew(context, resource)
160
- return resource
161
- }
162
-
163
- private lineItemFromImportDraft(
164
- context: RepositoryContext,
165
- draft: LineItemImportDraft
166
- ): LineItem {
167
- let product: Product
168
- let variant: ProductVariant | undefined
169
-
170
- if (draft.variant.sku) {
171
- variant = {
172
- id: 0,
173
- sku: draft.variant.sku,
174
- }
175
-
176
- const items = this._storage.query(context.projectKey, 'product', {
177
- where: [
178
- `masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`,
179
- ],
180
- }) as ProductPagedQueryResponse
181
-
182
- if (items.count !== 1) {
183
- throw new CommercetoolsError<GeneralError>({
184
- code: 'General',
185
- message: `A product containing a variant with SKU '${draft.variant.sku}' not found.`,
186
- })
187
- }
188
-
189
- product = items.results[0]
190
- if (product.masterData.current.masterVariant.sku === draft.variant.sku) {
191
- variant = product.masterData.current.masterVariant
192
- } else {
193
- variant = product.masterData.current.variants.find(
194
- (v) => v.sku === draft.variant.sku
195
- )
196
- }
197
- if (!variant) {
198
- throw new Error('Internal state error')
199
- }
200
- } else {
201
- throw new Error('No product found')
202
- }
203
-
204
- const lineItem: LineItem = {
205
- ...getBaseResourceProperties(),
206
- custom: createCustomFields(
207
- draft.custom,
208
- context.projectKey,
209
- this._storage
210
- ),
211
- discountedPricePerQuantity: [],
212
- lineItemMode: 'Standard',
213
- name: draft.name,
214
- price: createPrice(draft.price),
215
- priceMode: 'Platform',
216
- productId: product.id,
217
- productType: product.productType,
218
- quantity: draft.quantity,
219
- state: draft.state || [],
220
- taxRate: draft.taxRate,
221
- taxedPricePortions: [],
222
- perMethodTaxRate: [],
223
- totalPrice: createCentPrecisionMoney(draft.price.value),
224
- variant: {
225
- id: variant.id,
226
- sku: variant.sku,
227
- price: createPrice(draft.price),
228
- },
229
- }
230
-
231
- return lineItem
232
- }
233
-
234
- private customLineItemFromImportDraft(
235
- context: RepositoryContext,
236
- draft: CustomLineItemImportDraft
237
- ): CustomLineItem {
238
- const lineItem: CustomLineItem = {
239
- ...getBaseResourceProperties(),
240
- custom: createCustomFields(
241
- draft.custom,
242
- context.projectKey,
243
- this._storage
244
- ),
245
- discountedPricePerQuantity: [],
246
- money: createTypedMoney(draft.money),
247
- name: draft.name,
248
- quantity: draft.quantity ?? 0,
249
- perMethodTaxRate: [],
250
- priceMode: draft.priceMode ?? 'Standard',
251
- slug: draft.slug,
252
- state: [],
253
- totalPrice: createCentPrecisionMoney(draft.money),
254
- taxedPricePortions: [],
255
- }
256
-
257
- return lineItem
258
- }
259
-
260
- getWithOrderNumber(
261
- context: RepositoryContext,
262
- orderNumber: string,
263
- params: QueryParams = {}
264
- ): Order | undefined {
265
- const result = this._storage.query(context.projectKey, this.getTypeId(), {
266
- ...params,
267
- where: [`orderNumber="${orderNumber}"`],
268
- })
269
- if (result.count === 1) {
270
- return result.results[0] as Order
271
- }
272
-
273
- // Catch this for now, should be checked when creating/updating
274
- if (result.count > 1) {
275
- throw new Error('Duplicate order numbers')
276
- }
277
-
278
- return
279
- }
280
-
281
- actions = {
282
- addPayment: (
283
- context: RepositoryContext,
284
- resource: Writable<Order>,
285
- { payment }: OrderAddPaymentAction
286
- ) => {
287
- const resolvedPayment = this._storage.getByResourceIdentifier(
288
- context.projectKey,
289
- payment
290
- )
291
- if (!resolvedPayment) {
292
- throw new Error(`Payment ${payment.id} not found`)
293
- }
294
-
295
- if (!resource.paymentInfo) {
296
- resource.paymentInfo = {
297
- payments: [],
298
- }
299
- }
300
-
301
- resource.paymentInfo.payments.push({
302
- typeId: 'payment',
303
- id: payment.id!,
304
- })
305
- },
306
-
307
- addReturnInfo: (
308
- context: RepositoryContext,
309
- resource: Writable<Order>,
310
- info: OrderAddReturnInfoAction
311
- ) => {
312
- if (!resource.returnInfo) {
313
- resource.returnInfo = []
314
- }
315
-
316
- const resolved: ReturnInfo = {
317
- items: info.items.map((item) => {
318
- const common = {
319
- ...getBaseResourceProperties(),
320
- quantity: item.quantity,
321
- paymentState: 'Initial',
322
- shipmentState: 'Initial',
323
- comment: item.comment,
324
- }
325
- if (item.customLineItemId) {
326
- return {
327
- ...common,
328
- type: 'CustomLineItemReturnItem',
329
- customLineItemId: item.customLineItemId,
330
- } as CustomLineItemReturnItem
331
- }
332
- return {
333
- ...common,
334
- type: 'LineItemReturnItem',
335
- lineItemId: item.customLineItemId || item.lineItemId,
336
- } as LineItemReturnItem
337
- }),
338
- returnTrackingId: info.returnTrackingId,
339
- returnDate: info.returnDate,
340
- }
341
-
342
- resource.returnInfo.push(resolved)
343
- },
344
- changeOrderState: (
345
- context: RepositoryContext,
346
- resource: Writable<Order>,
347
- { orderState }: OrderChangeOrderStateAction
348
- ) => {
349
- resource.orderState = orderState
350
- },
351
- changePaymentState: (
352
- context: RepositoryContext,
353
- resource: Writable<Order>,
354
- { paymentState }: OrderChangePaymentStateAction
355
- ) => {
356
- resource.paymentState = paymentState
357
- },
358
- transitionState: (
359
- context: RepositoryContext,
360
- resource: Writable<Order>,
361
- { state }: OrderTransitionStateAction
362
- ) => {
363
- const resolvedType = this._storage.getByResourceIdentifier(
364
- context.projectKey,
365
- state
366
- ) as State | null
367
-
368
- if (!resolvedType) {
369
- throw new Error(
370
- `No state found with key=${state.key} or id=${state.key}`
371
- )
372
- }
373
-
374
- resource.state = {
375
- typeId: 'state',
376
- id: resolvedType.id,
377
- obj: { ...resolvedType, key: state.key ?? '' },
378
- }
379
- },
380
- setBillingAddress: (
381
- context: RepositoryContext,
382
- resource: Writable<Order>,
383
- { address }: OrderSetBillingAddressAction
384
- ) => {
385
- resource.billingAddress = createAddress(
386
- address,
387
- context.projectKey,
388
- this._storage
389
- )
390
- },
391
- setCustomerEmail: (
392
- context: RepositoryContext,
393
- resource: Writable<Order>,
394
- { email }: OrderSetCustomerEmailAction
395
- ) => {
396
- resource.customerEmail = email
397
- },
398
- setCustomField: (
399
- context: RepositoryContext,
400
- resource: Order,
401
- { name, value }: OrderSetCustomFieldAction
402
- ) => {
403
- if (!resource.custom) {
404
- throw new Error('Resource has no custom field')
405
- }
406
- resource.custom.fields[name] = value
407
- },
408
- setCustomType: (
409
- context: RepositoryContext,
410
- resource: Writable<Order>,
411
- { type, fields }: OrderSetCustomTypeAction
412
- ) => {
413
- if (!type) {
414
- resource.custom = undefined
415
- } else {
416
- const resolvedType = this._storage.getByResourceIdentifier(
417
- context.projectKey,
418
- type
419
- )
420
- if (!resolvedType) {
421
- throw new Error(`Type ${type} not found`)
422
- }
423
-
424
- resource.custom = {
425
- type: {
426
- typeId: 'type',
427
- id: resolvedType.id,
428
- },
429
- fields: fields || {},
430
- }
431
- }
432
- },
433
- setLocale: (
434
- context: RepositoryContext,
435
- resource: Writable<Order>,
436
- { locale }: OrderSetLocaleAction
437
- ) => {
438
- resource.locale = locale
439
- },
440
- setOrderNumber: (
441
- context: RepositoryContext,
442
- resource: Writable<Order>,
443
- { orderNumber }: OrderSetOrderNumberAction
444
- ) => {
445
- resource.orderNumber = orderNumber
446
- },
447
- setShippingAddress: (
448
- context: RepositoryContext,
449
- resource: Writable<Order>,
450
- { address }: OrderSetShippingAddressAction
451
- ) => {
452
- resource.shippingAddress = createAddress(
453
- address,
454
- context.projectKey,
455
- this._storage
456
- )
457
- },
458
- setStore: (
459
- context: RepositoryContext,
460
- resource: Writable<Order>,
461
- { store }: OrderSetStoreAction
462
- ) => {
463
- if (!store) return
464
- const resolvedType = this._storage.getByResourceIdentifier(
465
- context.projectKey,
466
- store
467
- )
468
- if (!resolvedType) {
469
- throw new Error(`No store found with key=${store.key}`)
470
- }
471
-
472
- const storeReference = resolvedType as Store
473
- resource.store = {
474
- typeId: 'store',
475
- key: storeReference.key,
476
- }
477
- },
478
- updateSyncInfo: (
479
- context: RepositoryContext,
480
- resource: Writable<Order>,
481
- { channel, externalId, syncedAt }: OrderUpdateSyncInfoAction
482
- ) => {
483
- if (!channel) return
484
- const resolvedType = this._storage.getByResourceIdentifier(
485
- context.projectKey,
486
- channel
487
- )
488
- if (!resolvedType) {
489
- throw new Error(`Channel ${channel} not found`)
490
- }
491
-
492
- const syncData: SyncInfo = {
493
- channel: {
494
- typeId: 'channel',
495
- id: resolvedType.id,
496
- },
497
- externalId,
498
- syncedAt: syncedAt ?? new Date().toISOString(),
499
- }
500
-
501
- if (!resource.syncInfo?.length) {
502
- resource.syncInfo = [syncData]
503
- } else {
504
- const lastSyncInfo = resource.syncInfo[resource.syncInfo.length - 1]
505
- if (
506
- lastSyncInfo.channel.id !== syncData.channel.id ||
507
- lastSyncInfo.externalId !== syncData.externalId
508
- ) {
509
- resource.syncInfo.push(syncData)
510
- }
511
- }
512
- },
513
- }
514
- }