@labdigital/commercetools-mock 2.66.0 → 3.0.0-beta.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.
- package/README.md +31 -8
- package/dist/abstract-BKFcva6S.mjs +1044 -0
- package/dist/abstract-BKFcva6S.mjs.map +1 -0
- package/dist/config-BcNSzPZz.d.mts +1718 -0
- package/dist/index.d.mts +50 -1633
- package/dist/index.mjs +3769 -2653
- package/dist/index.mjs.map +1 -1
- package/dist/storage/sqlite.d.mts +59 -0
- package/dist/storage/sqlite.mjs +234 -0
- package/dist/storage/sqlite.mjs.map +1 -0
- package/package.json +26 -29
- package/src/ctMock.ts +125 -136
- package/src/helpers.ts +14 -6
- package/src/index.ts +5 -0
- package/src/lib/masking.ts +4 -5
- package/src/lib/product-review-statistics.test.ts +257 -294
- package/src/lib/review-statistics.ts +17 -4
- package/src/oauth/helpers.ts +7 -4
- package/src/oauth/server.test.ts +102 -62
- package/src/oauth/server.ts +215 -213
- package/src/oauth/store.ts +20 -6
- package/src/orderSearch.ts +3 -3
- package/src/product-projection-search.ts +38 -20
- package/src/product-search-availability.test.ts +31 -52
- package/src/product-search.ts +19 -10
- package/src/projectAPI.ts +6 -22
- package/src/repositories/abstract.ts +182 -48
- package/src/repositories/as-associate.test.ts +19 -19
- package/src/repositories/associate-role.ts +12 -23
- package/src/repositories/attribute-group.test.ts +23 -23
- package/src/repositories/attribute-group.ts +6 -4
- package/src/repositories/business-unit.test.ts +63 -57
- package/src/repositories/business-unit.ts +107 -55
- package/src/repositories/cart/actions.ts +96 -65
- package/src/repositories/cart/helpers.ts +15 -11
- package/src/repositories/cart/index.test.ts +136 -30
- package/src/repositories/cart/index.ts +76 -59
- package/src/repositories/cart-discount/actions.ts +12 -44
- package/src/repositories/cart-discount/index.ts +20 -8
- package/src/repositories/category/actions.ts +27 -27
- package/src/repositories/category/index.test.ts +13 -9
- package/src/repositories/category/index.ts +40 -23
- package/src/repositories/channel.test.ts +53 -51
- package/src/repositories/channel.ts +12 -22
- package/src/repositories/custom-object.ts +34 -25
- package/src/repositories/customer/actions.ts +47 -25
- package/src/repositories/customer/index.test.ts +11 -11
- package/src/repositories/customer/index.ts +65 -35
- package/src/repositories/customer-group.test.ts +44 -42
- package/src/repositories/customer-group.ts +12 -22
- package/src/repositories/discount-code/actions.ts +3 -19
- package/src/repositories/discount-code/index.ts +9 -4
- package/src/repositories/discount-group/index.ts +8 -3
- package/src/repositories/extension.test.ts +27 -27
- package/src/repositories/extension.ts +10 -5
- package/src/repositories/helpers.ts +126 -47
- package/src/repositories/inventory-entry/actions.ts +3 -24
- package/src/repositories/inventory-entry/index.ts +19 -11
- package/src/repositories/my-customer.ts +13 -12
- package/src/repositories/my-order.ts +5 -2
- package/src/repositories/order/actions.ts +84 -56
- package/src/repositories/order/index.test.ts +36 -31
- package/src/repositories/order/index.ts +83 -49
- package/src/repositories/order-edit.ts +8 -3
- package/src/repositories/payment/actions.ts +64 -44
- package/src/repositories/payment/helpers.ts +3 -3
- package/src/repositories/payment/index.ts +28 -12
- package/src/repositories/product/actions.ts +133 -98
- package/src/repositories/product/helpers.ts +29 -16
- package/src/repositories/product/index.ts +42 -25
- package/src/repositories/product-discount.ts +6 -4
- package/src/repositories/product-projection.ts +41 -21
- package/src/repositories/product-selection.ts +8 -15
- package/src/repositories/product-tailoring.ts +22 -3
- package/src/repositories/product-type.ts +45 -4
- package/src/repositories/project.ts +57 -13
- package/src/repositories/quote/actions.ts +5 -28
- package/src/repositories/quote/index.ts +29 -6
- package/src/repositories/quote-request/actions.ts +5 -28
- package/src/repositories/quote-request/index.test.ts +3 -3
- package/src/repositories/quote-request/index.ts +31 -11
- package/src/repositories/quote-staged/actions.ts +5 -28
- package/src/repositories/quote-staged/index.ts +22 -8
- package/src/repositories/recurrence-policy/index.ts +6 -4
- package/src/repositories/recurring-order/actions.ts +7 -32
- package/src/repositories/recurring-order/index.ts +8 -6
- package/src/repositories/review.test.ts +147 -142
- package/src/repositories/review.ts +31 -37
- package/src/repositories/shipping-method/actions.ts +11 -28
- package/src/repositories/shipping-method/index.ts +26 -15
- package/src/repositories/shopping-list/actions.ts +21 -31
- package/src/repositories/shopping-list/index.ts +44 -22
- package/src/repositories/standalone-price.ts +6 -4
- package/src/repositories/state.ts +15 -9
- package/src/repositories/store.ts +21 -32
- package/src/repositories/subscription.test.ts +22 -22
- package/src/repositories/subscription.ts +8 -3
- package/src/repositories/tax-category/index.ts +8 -3
- package/src/repositories/type/actions.ts +21 -3
- package/src/repositories/type/index.ts +5 -3
- package/src/repositories/zone.test.ts +112 -77
- package/src/repositories/zone.ts +5 -3
- package/src/schemas/generated/associate-role.ts +13 -0
- package/src/schemas/generated/attribute-group.ts +12 -0
- package/src/schemas/generated/business-unit.ts +38 -0
- package/src/schemas/generated/cart-discount.ts +33 -0
- package/src/schemas/generated/cart.ts +61 -0
- package/src/schemas/generated/category.ts +25 -0
- package/src/schemas/generated/channel.ts +21 -0
- package/src/schemas/generated/common.ts +1372 -0
- package/src/schemas/generated/custom-object.ts +11 -0
- package/src/schemas/generated/customer-group.ts +11 -0
- package/src/schemas/generated/customer.ts +47 -0
- package/src/schemas/generated/discount-code.ts +25 -0
- package/src/schemas/generated/discount-group.ts +13 -0
- package/src/schemas/generated/extension.ts +15 -0
- package/src/schemas/generated/index.ts +42 -0
- package/src/schemas/generated/inventory-entry.ts +20 -0
- package/src/schemas/generated/my-quote-request.ts +10 -0
- package/src/schemas/generated/order-edit.ts +18 -0
- package/src/schemas/generated/order-from-cart.ts +25 -0
- package/src/schemas/generated/payment.ts +30 -0
- package/src/schemas/generated/product-discount.ts +20 -0
- package/src/schemas/generated/product-selection.ts +18 -0
- package/src/schemas/generated/product-tailoring.ts +26 -0
- package/src/schemas/generated/product-type.ts +12 -0
- package/src/schemas/generated/product.ts +37 -0
- package/src/schemas/generated/quote-request.ts +19 -0
- package/src/schemas/generated/quote.ts +18 -0
- package/src/schemas/generated/recurrence-policy.ts +15 -0
- package/src/schemas/generated/recurring-order.ts +19 -0
- package/src/schemas/generated/review.ts +24 -0
- package/src/schemas/generated/shipping-method.ts +24 -0
- package/src/schemas/generated/shopping-list.ts +28 -0
- package/src/schemas/generated/staged-quote.ts +18 -0
- package/src/schemas/generated/standalone-price.ts +32 -0
- package/src/schemas/generated/state.ts +20 -0
- package/src/schemas/generated/store.ts +23 -0
- package/src/schemas/generated/subscription.ts +20 -0
- package/src/schemas/generated/tax-category.ts +12 -0
- package/src/schemas/generated/type.ts +17 -0
- package/src/schemas/generated/zone.ts +12 -0
- package/src/schemas/update-request.ts +3 -5
- package/src/server.ts +32 -4
- package/src/services/abstract.ts +207 -101
- package/src/services/as-associate-cart.test.ts +28 -36
- package/src/services/as-associate-cart.ts +15 -12
- package/src/services/as-associate-order.test.ts +33 -40
- package/src/services/as-associate-order.ts +15 -12
- package/src/services/as-associate-quote-request.ts +15 -12
- package/src/services/as-associate-shopping-list.test.ts +25 -35
- package/src/services/as-associate-shopping-list.ts +15 -12
- package/src/services/as-associate.test.ts +21 -15
- package/src/services/as-associate.ts +23 -22
- package/src/services/associate-roles.test.ts +16 -22
- package/src/services/associate-roles.ts +2 -2
- package/src/services/attribute-group.test.ts +40 -44
- package/src/services/attribute-group.ts +2 -2
- package/src/services/business-units.test.ts +227 -163
- package/src/services/business-units.ts +2 -2
- package/src/services/cart-discount.test.ts +253 -187
- package/src/services/cart-discount.ts +2 -2
- package/src/services/cart.test.ts +833 -832
- package/src/services/cart.ts +31 -12
- package/src/services/category.test.ts +208 -130
- package/src/services/category.ts +2 -2
- package/src/services/channel.test.ts +39 -44
- package/src/services/channel.ts +2 -2
- package/src/services/custom-object.test.ts +103 -79
- package/src/services/custom-object.ts +106 -38
- package/src/services/customer-group.test.ts +39 -44
- package/src/services/customer-group.ts +2 -2
- package/src/services/customer.test.ts +357 -292
- package/src/services/customer.ts +70 -23
- package/src/services/discount-code.test.ts +57 -68
- package/src/services/discount-code.ts +2 -2
- package/src/services/discount-group.test.ts +111 -134
- package/src/services/discount-group.ts +2 -2
- package/src/services/draft-validation.test.ts +255 -0
- package/src/services/extension.test.ts +39 -44
- package/src/services/extension.ts +2 -2
- package/src/services/inventory-entry.test.ts +106 -87
- package/src/services/inventory-entry.ts +2 -2
- package/src/services/my-business-unit.test.ts +82 -112
- package/src/services/my-business-unit.ts +25 -19
- package/src/services/my-cart.test.ts +46 -41
- package/src/services/my-cart.ts +32 -28
- package/src/services/my-customer.test.ts +153 -88
- package/src/services/my-customer.ts +130 -61
- package/src/services/my-order.ts +15 -12
- package/src/services/my-payment.test.ts +30 -24
- package/src/services/my-payment.ts +2 -2
- package/src/services/my-shopping-list.ts +2 -2
- package/src/services/order.test.ts +332 -276
- package/src/services/order.ts +45 -27
- package/src/services/payment.test.ts +31 -29
- package/src/services/payment.ts +2 -2
- package/src/services/product-discount.test.ts +39 -46
- package/src/services/product-discount.ts +2 -2
- package/src/services/product-projection.test.ts +176 -166
- package/src/services/product-projection.ts +31 -15
- package/src/services/product-selection.test.ts +17 -9
- package/src/services/product-selection.ts +2 -2
- package/src/services/product-type.test.ts +80 -21
- package/src/services/product-type.ts +2 -2
- package/src/services/product.test.ts +569 -534
- package/src/services/product.ts +14 -7
- package/src/services/project.test.ts +22 -12
- package/src/services/project.ts +28 -13
- package/src/services/quote-request.test.ts +36 -39
- package/src/services/quote-request.ts +2 -2
- package/src/services/quote-staged.ts +2 -2
- package/src/services/quote.ts +2 -2
- package/src/services/recurrence-policy.test.ts +114 -139
- package/src/services/recurrence-policy.ts +2 -2
- package/src/services/recurring-order.test.ts +149 -194
- package/src/services/recurring-order.ts +2 -2
- package/src/services/reviews.test.ts +127 -106
- package/src/services/reviews.ts +2 -2
- package/src/services/shipping-method.test.ts +96 -125
- package/src/services/shipping-method.ts +24 -12
- package/src/services/shopping-list.test.ts +183 -141
- package/src/services/shopping-list.ts +2 -2
- package/src/services/standalone-price.test.ts +60 -46
- package/src/services/standalone-price.ts +2 -2
- package/src/services/state.test.ts +20 -25
- package/src/services/state.ts +2 -2
- package/src/services/store.test.ts +26 -45
- package/src/services/store.ts +2 -2
- package/src/services/subscription.test.ts +39 -44
- package/src/services/subscription.ts +2 -2
- package/src/services/tax-category.test.ts +33 -36
- package/src/services/tax-category.ts +2 -2
- package/src/services/type.test.ts +45 -44
- package/src/services/type.ts +2 -2
- package/src/services/zone.test.ts +40 -44
- package/src/services/zone.ts +2 -2
- package/src/shipping.ts +41 -11
- package/src/storage/abstract.ts +248 -17
- package/src/storage/in-memory.ts +147 -290
- package/src/storage/sqlite.ts +429 -0
- package/src/storage/storage-map.ts +75 -0
- package/src/storage/storage.test-helpers.ts +97 -0
- package/src/storage/storage.test.ts +802 -0
- package/src/testing/associate-role.ts +28 -0
- package/src/testing/attribute-group.ts +27 -0
- package/src/testing/business-unit.ts +9 -8
- package/src/testing/cart-discount.ts +34 -0
- package/src/testing/cart.ts +20 -0
- package/src/testing/category.ts +25 -0
- package/src/testing/channel.ts +23 -0
- package/src/testing/custom-object.ts +27 -0
- package/src/testing/customer-group.ts +26 -0
- package/src/testing/customer.ts +36 -33
- package/src/testing/discount-code.ts +29 -0
- package/src/testing/discount-group.ts +27 -0
- package/src/testing/extension.ts +32 -0
- package/src/testing/index.ts +33 -0
- package/src/testing/inventory-entry.ts +26 -0
- package/src/testing/order.ts +27 -0
- package/src/testing/payment.ts +23 -0
- package/src/testing/product-discount.ts +33 -0
- package/src/testing/product-selection.ts +28 -0
- package/src/testing/product-type.ts +27 -0
- package/src/testing/product.ts +38 -0
- package/src/testing/quote-request.ts +29 -0
- package/src/testing/recurrence-policy.ts +33 -0
- package/src/testing/recurring-order.ts +32 -0
- package/src/testing/review.ts +24 -0
- package/src/testing/shipping-method.ts +31 -0
- package/src/testing/shopping-list.ts +25 -0
- package/src/testing/standalone-price.ts +31 -0
- package/src/testing/state.ts +21 -0
- package/src/testing/store.ts +26 -0
- package/src/testing/subscription.ts +38 -0
- package/src/testing/tax-category.ts +27 -0
- package/src/testing/type.ts +9 -6
- package/src/testing/zone.ts +22 -0
- package/src/validate.test.ts +122 -0
- package/src/validate.ts +78 -7
- package/src/.env +0 -0
|
@@ -35,11 +35,12 @@ import type {
|
|
|
35
35
|
CartUpdateAction,
|
|
36
36
|
CustomFields,
|
|
37
37
|
GeneralError,
|
|
38
|
+
InvalidOperationError,
|
|
38
39
|
ItemShippingDetails,
|
|
39
40
|
LineItem,
|
|
40
41
|
Product,
|
|
41
|
-
ProductPagedQueryResponse,
|
|
42
42
|
ProductVariant,
|
|
43
|
+
ReferencedResourceNotFoundError,
|
|
43
44
|
} from "@commercetools/platform-sdk";
|
|
44
45
|
import type {
|
|
45
46
|
CartAddDiscountCodeAction,
|
|
@@ -93,7 +94,7 @@ export class CartUpdateHandler
|
|
|
93
94
|
}
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
addLineItem(
|
|
97
|
+
async addLineItem(
|
|
97
98
|
context: RepositoryContext,
|
|
98
99
|
resource: Writable<Cart>,
|
|
99
100
|
{
|
|
@@ -110,14 +111,19 @@ export class CartUpdateHandler
|
|
|
110
111
|
|
|
111
112
|
if (productId && variantId) {
|
|
112
113
|
// Fetch product and variant by ID
|
|
113
|
-
product = this._storage.get(
|
|
114
|
+
product = await this._storage.get(
|
|
115
|
+
context.projectKey,
|
|
116
|
+
"product",
|
|
117
|
+
productId,
|
|
118
|
+
{},
|
|
119
|
+
);
|
|
114
120
|
} else if (sku) {
|
|
115
121
|
// Fetch product and variant by SKU
|
|
116
|
-
const items = this._storage.query(context.projectKey, "product", {
|
|
122
|
+
const items = await this._storage.query(context.projectKey, "product", {
|
|
117
123
|
where: [
|
|
118
124
|
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
119
125
|
],
|
|
120
|
-
})
|
|
126
|
+
});
|
|
121
127
|
|
|
122
128
|
if (items.count === 1) {
|
|
123
129
|
product = items.results[0];
|
|
@@ -182,9 +188,10 @@ export class CartUpdateHandler
|
|
|
182
188
|
country: resource.country,
|
|
183
189
|
});
|
|
184
190
|
if (!price) {
|
|
185
|
-
throw new
|
|
186
|
-
|
|
187
|
-
|
|
191
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
192
|
+
code: "InvalidOperation",
|
|
193
|
+
message: `No valid price found for ${productId} for country ${resource.country} and currency ${currency}`,
|
|
194
|
+
});
|
|
188
195
|
}
|
|
189
196
|
const totalPrice = createCentPrecisionMoney({
|
|
190
197
|
currencyCode: price.value.currencyCode,
|
|
@@ -209,7 +216,11 @@ export class CartUpdateHandler
|
|
|
209
216
|
lineItemMode: "Standard",
|
|
210
217
|
priceMode: "Platform",
|
|
211
218
|
state: [],
|
|
212
|
-
custom: createCustomFields(
|
|
219
|
+
custom: await createCustomFields(
|
|
220
|
+
custom,
|
|
221
|
+
context.projectKey,
|
|
222
|
+
this._storage,
|
|
223
|
+
),
|
|
213
224
|
});
|
|
214
225
|
}
|
|
215
226
|
|
|
@@ -283,12 +294,12 @@ export class CartUpdateHandler
|
|
|
283
294
|
// and prices on related Products have changed in the meanwhile.
|
|
284
295
|
}
|
|
285
296
|
|
|
286
|
-
addDiscountCode(
|
|
297
|
+
async addDiscountCode(
|
|
287
298
|
context: RepositoryContext,
|
|
288
299
|
resource: Writable<Cart>,
|
|
289
300
|
{ code }: CartAddDiscountCodeAction,
|
|
290
301
|
) {
|
|
291
|
-
const info = createDiscountCodeInfoFromCode(
|
|
302
|
+
const info = await createDiscountCodeInfoFromCode(
|
|
292
303
|
context.projectKey,
|
|
293
304
|
this._storage,
|
|
294
305
|
code,
|
|
@@ -346,7 +357,7 @@ export class CartUpdateHandler
|
|
|
346
357
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
|
|
347
358
|
}
|
|
348
359
|
|
|
349
|
-
addCustomLineItem(
|
|
360
|
+
async addCustomLineItem(
|
|
350
361
|
context: RepositoryContext,
|
|
351
362
|
resource: Writable<Cart>,
|
|
352
363
|
{
|
|
@@ -360,7 +371,7 @@ export class CartUpdateHandler
|
|
|
360
371
|
key,
|
|
361
372
|
}: CartAddCustomLineItemAction,
|
|
362
373
|
) {
|
|
363
|
-
const customLineItem = createCustomLineItemFromDraft(
|
|
374
|
+
const customLineItem = await createCustomLineItemFromDraft(
|
|
364
375
|
context.projectKey,
|
|
365
376
|
{ money, name, slug, quantity, taxCategory, custom, priceMode, key },
|
|
366
377
|
this._storage,
|
|
@@ -542,13 +553,16 @@ export class CartUpdateHandler
|
|
|
542
553
|
);
|
|
543
554
|
}
|
|
544
555
|
|
|
545
|
-
setBillingAddressCustomType(
|
|
556
|
+
async setBillingAddressCustomType(
|
|
546
557
|
context: RepositoryContext,
|
|
547
558
|
resource: Writable<Cart>,
|
|
548
559
|
custom: CartSetBillingAddressCustomTypeAction,
|
|
549
560
|
) {
|
|
550
561
|
if (!resource.billingAddress) {
|
|
551
|
-
throw new
|
|
562
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
563
|
+
code: "InvalidOperation",
|
|
564
|
+
message: "Resource has no billing address",
|
|
565
|
+
});
|
|
552
566
|
}
|
|
553
567
|
|
|
554
568
|
if (!custom.type) {
|
|
@@ -556,13 +570,19 @@ export class CartUpdateHandler
|
|
|
556
570
|
return;
|
|
557
571
|
}
|
|
558
572
|
|
|
559
|
-
const resolvedType = this._storage.getByResourceIdentifier<"type">(
|
|
573
|
+
const resolvedType = await this._storage.getByResourceIdentifier<"type">(
|
|
560
574
|
context.projectKey,
|
|
561
575
|
custom.type,
|
|
562
576
|
);
|
|
563
577
|
|
|
564
578
|
if (!resolvedType) {
|
|
565
|
-
throw new
|
|
579
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
580
|
+
code: "ReferencedResourceNotFound",
|
|
581
|
+
message: `Type ${custom.type} not found`,
|
|
582
|
+
typeId: "type",
|
|
583
|
+
id: custom.type?.id,
|
|
584
|
+
key: custom.type?.key,
|
|
585
|
+
});
|
|
566
586
|
}
|
|
567
587
|
|
|
568
588
|
resource.billingAddress.custom = {
|
|
@@ -604,14 +624,10 @@ export class CartUpdateHandler
|
|
|
604
624
|
resource: Cart,
|
|
605
625
|
{ name, value }: CartSetCustomFieldAction,
|
|
606
626
|
) {
|
|
607
|
-
|
|
608
|
-
throw new Error("Resource has no custom field");
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
resource.custom.fields[name] = value;
|
|
627
|
+
this._setCustomFieldValues(resource, { name, value });
|
|
612
628
|
}
|
|
613
629
|
|
|
614
|
-
setCustomShippingMethod(
|
|
630
|
+
async setCustomShippingMethod(
|
|
615
631
|
context: RepositoryContext,
|
|
616
632
|
resource: Writable<Cart>,
|
|
617
633
|
{
|
|
@@ -622,11 +638,14 @@ export class CartUpdateHandler
|
|
|
622
638
|
}: CartSetCustomShippingMethodAction,
|
|
623
639
|
) {
|
|
624
640
|
if (externalTaxRate) {
|
|
625
|
-
throw new
|
|
641
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
642
|
+
code: "InvalidOperation",
|
|
643
|
+
message: "External tax rate is not supported",
|
|
644
|
+
});
|
|
626
645
|
}
|
|
627
646
|
|
|
628
647
|
const tax = taxCategory
|
|
629
|
-
? this._storage.getByResourceIdentifier<"tax-category">(
|
|
648
|
+
? await this._storage.getByResourceIdentifier<"tax-category">(
|
|
630
649
|
context.projectKey,
|
|
631
650
|
taxCategory,
|
|
632
651
|
)
|
|
@@ -649,30 +668,12 @@ export class CartUpdateHandler
|
|
|
649
668
|
};
|
|
650
669
|
}
|
|
651
670
|
|
|
652
|
-
setCustomType(
|
|
671
|
+
async setCustomType(
|
|
653
672
|
context: RepositoryContext,
|
|
654
673
|
resource: Writable<Cart>,
|
|
655
674
|
{ type, fields }: CartSetCustomTypeAction,
|
|
656
675
|
) {
|
|
657
|
-
|
|
658
|
-
resource.custom = undefined;
|
|
659
|
-
} else {
|
|
660
|
-
const resolvedType = this._storage.getByResourceIdentifier(
|
|
661
|
-
context.projectKey,
|
|
662
|
-
type,
|
|
663
|
-
);
|
|
664
|
-
if (!resolvedType) {
|
|
665
|
-
throw new Error(`Type ${type} not found`);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
resource.custom = {
|
|
669
|
-
type: {
|
|
670
|
-
typeId: "type",
|
|
671
|
-
id: resolvedType.id,
|
|
672
|
-
},
|
|
673
|
-
fields: fields || {},
|
|
674
|
-
};
|
|
675
|
-
}
|
|
676
|
+
await this._setCustomType(context, resource, { type, fields });
|
|
676
677
|
}
|
|
677
678
|
|
|
678
679
|
setDirectDiscounts(
|
|
@@ -718,13 +719,16 @@ export class CartUpdateHandler
|
|
|
718
719
|
}
|
|
719
720
|
|
|
720
721
|
if (!lineItem.custom) {
|
|
721
|
-
throw new
|
|
722
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
723
|
+
code: "InvalidOperation",
|
|
724
|
+
message: "Resource has no custom field",
|
|
725
|
+
});
|
|
722
726
|
}
|
|
723
727
|
|
|
724
728
|
lineItem.custom.fields[name] = value;
|
|
725
729
|
}
|
|
726
730
|
|
|
727
|
-
setLineItemCustomType(
|
|
731
|
+
async setLineItemCustomType(
|
|
728
732
|
context: RepositoryContext,
|
|
729
733
|
resource: Writable<Cart>,
|
|
730
734
|
{ lineItemId, lineItemKey, type, fields }: CartSetLineItemCustomTypeAction,
|
|
@@ -748,12 +752,18 @@ export class CartUpdateHandler
|
|
|
748
752
|
if (!type) {
|
|
749
753
|
lineItem.custom = undefined;
|
|
750
754
|
} else {
|
|
751
|
-
const resolvedType = this._storage.getByResourceIdentifier(
|
|
755
|
+
const resolvedType = await this._storage.getByResourceIdentifier(
|
|
752
756
|
context.projectKey,
|
|
753
757
|
type,
|
|
754
758
|
);
|
|
755
759
|
if (!resolvedType) {
|
|
756
|
-
throw new
|
|
760
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
761
|
+
code: "ReferencedResourceNotFound",
|
|
762
|
+
message: `Type ${type} not found`,
|
|
763
|
+
typeId: "type",
|
|
764
|
+
id: type?.id,
|
|
765
|
+
key: type?.key,
|
|
766
|
+
});
|
|
757
767
|
}
|
|
758
768
|
|
|
759
769
|
lineItem.custom = {
|
|
@@ -816,9 +826,10 @@ export class CartUpdateHandler
|
|
|
816
826
|
});
|
|
817
827
|
|
|
818
828
|
if (!price) {
|
|
819
|
-
throw new
|
|
820
|
-
|
|
821
|
-
|
|
829
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
830
|
+
code: "InvalidOperation",
|
|
831
|
+
message: `No valid price found for ${lineItem.productId} for country ${resource.country} and currency ${resource.totalPrice.currencyCode}`,
|
|
832
|
+
});
|
|
822
833
|
}
|
|
823
834
|
|
|
824
835
|
lineItem.price = price;
|
|
@@ -872,7 +883,7 @@ export class CartUpdateHandler
|
|
|
872
883
|
resource.locale = locale;
|
|
873
884
|
}
|
|
874
885
|
|
|
875
|
-
setShippingAddress(
|
|
886
|
+
async setShippingAddress(
|
|
876
887
|
context: RepositoryContext,
|
|
877
888
|
resource: Writable<Cart>,
|
|
878
889
|
{ address }: CartSetShippingAddressAction,
|
|
@@ -884,7 +895,7 @@ export class CartUpdateHandler
|
|
|
884
895
|
|
|
885
896
|
let custom: CustomFields | undefined;
|
|
886
897
|
if ((address as Address & AddressDraft).custom) {
|
|
887
|
-
custom = createCustomFields(
|
|
898
|
+
custom = await createCustomFields(
|
|
888
899
|
(address as Address & AddressDraft).custom,
|
|
889
900
|
context.projectKey,
|
|
890
901
|
this._storage,
|
|
@@ -897,13 +908,16 @@ export class CartUpdateHandler
|
|
|
897
908
|
};
|
|
898
909
|
}
|
|
899
910
|
|
|
900
|
-
setShippingAddressCustomType(
|
|
911
|
+
async setShippingAddressCustomType(
|
|
901
912
|
context: RepositoryContext,
|
|
902
913
|
resource: Writable<Cart>,
|
|
903
914
|
custom: CartSetShippingAddressCustomTypeAction,
|
|
904
915
|
) {
|
|
905
916
|
if (!resource.shippingAddress) {
|
|
906
|
-
throw new
|
|
917
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
918
|
+
code: "InvalidOperation",
|
|
919
|
+
message: "Resource has no shipping address",
|
|
920
|
+
});
|
|
907
921
|
}
|
|
908
922
|
|
|
909
923
|
if (!custom.type) {
|
|
@@ -911,13 +925,19 @@ export class CartUpdateHandler
|
|
|
911
925
|
return;
|
|
912
926
|
}
|
|
913
927
|
|
|
914
|
-
const resolvedType = this._storage.getByResourceIdentifier<"type">(
|
|
928
|
+
const resolvedType = await this._storage.getByResourceIdentifier<"type">(
|
|
915
929
|
context.projectKey,
|
|
916
930
|
custom.type,
|
|
917
931
|
);
|
|
918
932
|
|
|
919
933
|
if (!resolvedType) {
|
|
920
|
-
throw new
|
|
934
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
935
|
+
code: "ReferencedResourceNotFound",
|
|
936
|
+
message: `Type ${custom.type} not found`,
|
|
937
|
+
typeId: "type",
|
|
938
|
+
id: custom.type?.id,
|
|
939
|
+
key: custom.type?.key,
|
|
940
|
+
});
|
|
921
941
|
}
|
|
922
942
|
|
|
923
943
|
resource.shippingAddress.custom = {
|
|
@@ -929,13 +949,13 @@ export class CartUpdateHandler
|
|
|
929
949
|
};
|
|
930
950
|
}
|
|
931
951
|
|
|
932
|
-
setShippingMethod(
|
|
952
|
+
async setShippingMethod(
|
|
933
953
|
context: RepositoryContext,
|
|
934
954
|
resource: Writable<Cart>,
|
|
935
955
|
{ shippingMethod }: CartSetShippingMethodAction,
|
|
936
956
|
) {
|
|
937
957
|
if (shippingMethod) {
|
|
938
|
-
resource.shippingInfo = this.repository.createShippingInfo(
|
|
958
|
+
resource.shippingInfo = await this.repository.createShippingInfo(
|
|
939
959
|
context,
|
|
940
960
|
resource,
|
|
941
961
|
shippingMethod,
|
|
@@ -951,15 +971,21 @@ export class CartUpdateHandler
|
|
|
951
971
|
{ name, value }: CartSetShippingAddressCustomFieldAction,
|
|
952
972
|
) {
|
|
953
973
|
if (!resource.shippingAddress) {
|
|
954
|
-
throw new
|
|
974
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
975
|
+
code: "InvalidOperation",
|
|
976
|
+
message: "Resource has no shipping address",
|
|
977
|
+
});
|
|
955
978
|
}
|
|
956
979
|
if (!resource.shippingAddress.custom) {
|
|
957
|
-
throw new
|
|
980
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
981
|
+
code: "InvalidOperation",
|
|
982
|
+
message: "Resource has no custom field",
|
|
983
|
+
});
|
|
958
984
|
}
|
|
959
985
|
resource.shippingAddress.custom.fields[name] = value;
|
|
960
986
|
}
|
|
961
987
|
|
|
962
|
-
removeShippingMethod(
|
|
988
|
+
async removeShippingMethod(
|
|
963
989
|
context: RepositoryContext,
|
|
964
990
|
resource: Writable<Cart>,
|
|
965
991
|
{ shippingKey }: CartRemoveShippingMethodAction,
|
|
@@ -968,7 +994,7 @@ export class CartUpdateHandler
|
|
|
968
994
|
return;
|
|
969
995
|
}
|
|
970
996
|
const shippingMethod =
|
|
971
|
-
this._storage.getByResourceIdentifier<"shipping-method">(
|
|
997
|
+
await this._storage.getByResourceIdentifier<"shipping-method">(
|
|
972
998
|
context.projectKey,
|
|
973
999
|
{
|
|
974
1000
|
typeId: "shipping-method",
|
|
@@ -977,7 +1003,12 @@ export class CartUpdateHandler
|
|
|
977
1003
|
);
|
|
978
1004
|
|
|
979
1005
|
if (resource.shippingInfo?.shippingMethod?.id !== shippingMethod.id) {
|
|
980
|
-
throw new
|
|
1006
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
1007
|
+
code: "ReferencedResourceNotFound",
|
|
1008
|
+
message: "Shipping method with key not found",
|
|
1009
|
+
typeId: "shipping-method",
|
|
1010
|
+
key: shippingKey,
|
|
1011
|
+
});
|
|
981
1012
|
}
|
|
982
1013
|
|
|
983
1014
|
resource.shippingInfo = undefined;
|
|
@@ -64,16 +64,16 @@ export const calculateCartTotalPrice = (cart: Cart): number => {
|
|
|
64
64
|
return lineItemsTotal + customLineItemsTotal;
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
export const createCustomLineItemFromDraft = (
|
|
67
|
+
export const createCustomLineItemFromDraft = async (
|
|
68
68
|
projectKey: string,
|
|
69
69
|
draft: CustomLineItemDraft,
|
|
70
70
|
storage: AbstractStorage,
|
|
71
71
|
country?: string,
|
|
72
|
-
): CustomLineItem => {
|
|
72
|
+
): Promise<CustomLineItem> => {
|
|
73
73
|
const quantity = draft.quantity ?? 1;
|
|
74
74
|
|
|
75
75
|
const taxCategoryRef = draft.taxCategory
|
|
76
|
-
? getReferenceFromResourceIdentifier<TaxCategoryReference>(
|
|
76
|
+
? await getReferenceFromResourceIdentifier<TaxCategoryReference>(
|
|
77
77
|
draft.taxCategory,
|
|
78
78
|
projectKey,
|
|
79
79
|
storage,
|
|
@@ -83,9 +83,13 @@ export const createCustomLineItemFromDraft = (
|
|
|
83
83
|
let taxCategory: TaxCategory | undefined;
|
|
84
84
|
if (taxCategoryRef) {
|
|
85
85
|
try {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
const result = await storage.get(
|
|
87
|
+
projectKey,
|
|
88
|
+
"tax-category",
|
|
89
|
+
taxCategoryRef.id,
|
|
90
|
+
{},
|
|
91
|
+
);
|
|
92
|
+
taxCategory = result || undefined;
|
|
89
93
|
} catch (_error) {
|
|
90
94
|
// Tax category not found, continue without tax calculation
|
|
91
95
|
}
|
|
@@ -122,7 +126,7 @@ export const createCustomLineItemFromDraft = (
|
|
|
122
126
|
taxCategory: taxCategoryRef,
|
|
123
127
|
taxRate,
|
|
124
128
|
taxedPrice,
|
|
125
|
-
custom: createCustomFields(draft.custom, projectKey, storage),
|
|
129
|
+
custom: await createCustomFields(draft.custom, projectKey, storage),
|
|
126
130
|
discountedPricePerQuantity: [],
|
|
127
131
|
perMethodTaxRate: [],
|
|
128
132
|
priceMode: draft.priceMode ?? "Standard",
|
|
@@ -131,12 +135,12 @@ export const createCustomLineItemFromDraft = (
|
|
|
131
135
|
};
|
|
132
136
|
};
|
|
133
137
|
|
|
134
|
-
export const createDiscountCodeInfoFromCode = (
|
|
138
|
+
export const createDiscountCodeInfoFromCode = async (
|
|
135
139
|
projectKey: string,
|
|
136
140
|
storage: AbstractStorage,
|
|
137
141
|
code: string,
|
|
138
|
-
): DiscountCodeInfo => {
|
|
139
|
-
const discountCodes = storage.query(projectKey, "discount-code", {
|
|
142
|
+
): Promise<DiscountCodeInfo> => {
|
|
143
|
+
const discountCodes = await storage.query(projectKey, "discount-code", {
|
|
140
144
|
where: `code="${code}"`,
|
|
141
145
|
});
|
|
142
146
|
// Does not validate anything besides existence of the DiscountCode object
|
|
@@ -145,7 +149,7 @@ export const createDiscountCodeInfoFromCode = (
|
|
|
145
149
|
code: "DiscountCodeNonApplicable",
|
|
146
150
|
message: `The discount code '${code}' was not found.`,
|
|
147
151
|
reason: "DoesNotExist",
|
|
148
|
-
discountCode:
|
|
152
|
+
discountCode: code,
|
|
149
153
|
});
|
|
150
154
|
}
|
|
151
155
|
return {
|