@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.
- package/dist/index.cjs +4219 -3989
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -415
- package/dist/index.d.ts +268 -415
- package/dist/index.js +4219 -3989
- package/dist/index.js.map +1 -1
- package/package.json +44 -46
- package/src/constants.ts +2 -2
- package/src/ctMock.test.ts +11 -11
- package/src/ctMock.ts +141 -127
- package/src/deprecation.ts +8 -0
- package/src/exceptions.ts +17 -15
- package/src/helpers.ts +32 -32
- package/src/index.test.ts +128 -128
- package/src/index.ts +3 -3
- package/src/lib/expandParser.ts +13 -13
- package/src/lib/haversine.test.ts +9 -9
- package/src/lib/haversine.ts +11 -11
- package/src/lib/masking.ts +11 -11
- package/src/lib/parser.ts +2 -2
- package/src/lib/password.ts +23 -3
- package/src/lib/predicateParser.test.ts +185 -183
- package/src/lib/predicateParser.ts +234 -234
- package/src/lib/projectionSearchFilter.test.ts +103 -101
- package/src/lib/projectionSearchFilter.ts +152 -150
- package/src/lib/proxy.ts +5 -5
- package/src/oauth/errors.ts +4 -4
- package/src/oauth/helpers.ts +6 -6
- package/src/oauth/server.test.ts +110 -67
- package/src/oauth/server.ts +161 -141
- package/src/oauth/store.ts +49 -44
- package/src/priceSelector.test.ts +35 -35
- package/src/priceSelector.ts +30 -30
- package/src/product-projection-search.ts +136 -134
- package/src/projectAPI.test.ts +7 -7
- package/src/projectAPI.ts +24 -22
- package/src/repositories/abstract.ts +168 -116
- package/src/repositories/associate-role.ts +90 -77
- package/src/repositories/attribute-group.ts +51 -40
- package/src/repositories/business-unit.ts +168 -148
- package/src/repositories/cart/actions.ts +489 -0
- package/src/repositories/cart/helpers.ts +30 -0
- package/src/repositories/cart/index.ts +180 -0
- package/src/repositories/cart-discount/actions.ts +148 -0
- package/src/repositories/cart-discount/index.ts +86 -0
- package/src/repositories/category/actions.ts +231 -0
- package/src/repositories/category/index.ts +52 -0
- package/src/repositories/channel.ts +88 -90
- package/src/repositories/custom-object.ts +46 -45
- package/src/repositories/customer/actions.ts +165 -0
- package/src/repositories/customer/index.ts +79 -0
- package/src/repositories/customer-group.ts +66 -55
- package/src/repositories/discount-code/actions.ts +149 -0
- package/src/repositories/discount-code/index.ts +50 -0
- package/src/repositories/errors.ts +10 -10
- package/src/repositories/extension.ts +64 -62
- package/src/repositories/helpers.ts +117 -118
- package/src/repositories/index.ts +80 -79
- package/src/repositories/inventory-entry/actions.ts +84 -0
- package/src/repositories/inventory-entry/index.ts +44 -0
- package/src/repositories/my-customer.ts +114 -0
- package/src/repositories/my-order.ts +8 -8
- package/src/repositories/order/actions.ts +281 -0
- package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
- package/src/repositories/order/index.ts +260 -0
- package/src/repositories/order-edit.ts +10 -23
- package/src/repositories/payment/actions.ts +305 -0
- package/src/repositories/payment/helpers.ts +17 -0
- package/src/repositories/payment/index.ts +56 -0
- package/src/repositories/product/actions.ts +943 -0
- package/src/repositories/product/helpers.ts +98 -0
- package/src/repositories/product/index.ts +130 -0
- package/src/repositories/product-discount.ts +127 -117
- package/src/repositories/product-projection.ts +56 -62
- package/src/repositories/product-selection.ts +31 -28
- package/src/repositories/product-type.ts +136 -134
- package/src/repositories/project.ts +133 -118
- package/src/repositories/quote-request.ts +7 -19
- package/src/repositories/quote.ts +7 -22
- package/src/repositories/review.ts +13 -26
- package/src/repositories/shipping-method/actions.ts +198 -0
- package/src/repositories/shipping-method/helpers.ts +10 -0
- package/src/repositories/shipping-method/index.ts +138 -0
- package/src/repositories/shopping-list/actions.ts +295 -0
- package/src/repositories/shopping-list/index.ts +122 -0
- package/src/repositories/staged-quote.ts +7 -20
- package/src/repositories/standalone-price.ts +57 -44
- package/src/repositories/state.ts +113 -68
- package/src/repositories/store.ts +106 -94
- package/src/repositories/subscription.ts +46 -22
- package/src/repositories/tax-category/actions.ts +94 -0
- package/src/repositories/tax-category/helpers.ts +8 -0
- package/src/repositories/tax-category/index.ts +25 -0
- package/src/repositories/type/actions.ts +162 -0
- package/src/repositories/type/index.ts +24 -0
- package/src/repositories/zone.ts +62 -58
- package/src/server.ts +9 -9
- package/src/services/abstract.ts +75 -72
- package/src/services/associate-roles.test.ts +27 -27
- package/src/services/associate-roles.ts +7 -7
- package/src/services/attribute-group.ts +7 -7
- package/src/services/business-units.test.ts +28 -28
- package/src/services/business-units.ts +7 -7
- package/src/services/cart-discount.test.ts +199 -199
- package/src/services/cart-discount.ts +7 -7
- package/src/services/cart.test.ts +261 -261
- package/src/services/cart.ts +22 -21
- package/src/services/category.test.ts +121 -121
- package/src/services/category.ts +7 -7
- package/src/services/channel.ts +7 -7
- package/src/services/custom-object.test.ts +130 -130
- package/src/services/custom-object.ts +34 -31
- package/src/services/customer-group.ts +7 -7
- package/src/services/customer.test.ts +205 -205
- package/src/services/customer.ts +31 -29
- package/src/services/discount-code.ts +7 -7
- package/src/services/extension.ts +7 -7
- package/src/services/index.ts +85 -81
- package/src/services/inventory-entry.test.ts +106 -106
- package/src/services/inventory-entry.ts +7 -7
- package/src/services/my-cart.test.ts +56 -56
- package/src/services/my-cart.ts +20 -20
- package/src/services/my-customer.test.ts +155 -104
- package/src/services/my-customer.ts +61 -75
- package/src/services/my-order.ts +16 -16
- package/src/services/my-payment.test.ts +40 -40
- package/src/services/my-payment.ts +7 -7
- package/src/services/my-shopping-list.ts +7 -7
- package/src/services/order.test.ts +243 -243
- package/src/services/order.ts +23 -18
- package/src/services/payment.test.ts +40 -40
- package/src/services/payment.ts +7 -7
- package/src/services/product-discount.ts +7 -7
- package/src/services/product-projection.test.ts +190 -190
- package/src/services/product-projection.ts +34 -32
- package/src/services/product-selection.test.ts +19 -19
- package/src/services/product-selection.ts +7 -7
- package/src/services/product-type.test.ts +38 -38
- package/src/services/product-type.ts +7 -7
- package/src/services/product.test.ts +658 -656
- package/src/services/product.ts +7 -7
- package/src/services/project.test.ts +24 -24
- package/src/services/project.ts +17 -17
- package/src/services/reviews.ts +7 -7
- package/src/services/shipping-method.test.ts +78 -78
- package/src/services/shipping-method.ts +16 -16
- package/src/services/shopping-list.test.ts +170 -170
- package/src/services/shopping-list.ts +7 -7
- package/src/services/standalone-price.test.ts +112 -112
- package/src/services/standalone-price.ts +7 -7
- package/src/services/state.test.ts +30 -30
- package/src/services/state.ts +7 -7
- package/src/services/store.test.ts +40 -40
- package/src/services/store.ts +7 -7
- package/src/services/subscription.ts +7 -7
- package/src/services/tax-category.test.ts +43 -43
- package/src/services/tax-category.ts +7 -7
- package/src/services/type.ts +7 -7
- package/src/services/zone.ts +7 -7
- package/src/shippingCalculator.test.ts +43 -43
- package/src/shippingCalculator.ts +23 -23
- package/src/storage/abstract.ts +36 -34
- package/src/storage/in-memory.ts +237 -233
- package/src/storage/index.ts +2 -2
- package/src/types.ts +91 -91
- package/src/repositories/cart-discount.ts +0 -219
- package/src/repositories/cart.ts +0 -659
- package/src/repositories/category.ts +0 -256
- package/src/repositories/customer.ts +0 -228
- package/src/repositories/discount-code.ts +0 -181
- package/src/repositories/inventory-entry.ts +0 -109
- package/src/repositories/order.ts +0 -514
- package/src/repositories/payment.ts +0 -342
- package/src/repositories/product.ts +0 -1106
- package/src/repositories/shipping-method.ts +0 -312
- package/src/repositories/shopping-list.ts +0 -392
- package/src/repositories/tax-category.ts +0 -111
- package/src/repositories/type.ts +0 -172
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChannelReference,
|
|
3
|
+
Price,
|
|
4
|
+
PriceDraft,
|
|
5
|
+
Product,
|
|
6
|
+
ProductData,
|
|
7
|
+
ProductVariant,
|
|
8
|
+
ProductVariantDraft,
|
|
9
|
+
} from "@commercetools/platform-sdk";
|
|
10
|
+
import deepEqual from "deep-equal";
|
|
11
|
+
import { v4 as uuidv4 } from "uuid";
|
|
12
|
+
import { AbstractStorage } from "~src/storage";
|
|
13
|
+
import { Writable } from "~src/types";
|
|
14
|
+
import { RepositoryContext } from "../abstract";
|
|
15
|
+
import {
|
|
16
|
+
createTypedMoney,
|
|
17
|
+
getReferenceFromResourceIdentifier,
|
|
18
|
+
} from "../helpers";
|
|
19
|
+
|
|
20
|
+
interface VariantResult {
|
|
21
|
+
variant: Writable<ProductVariant> | undefined;
|
|
22
|
+
isMasterVariant: boolean;
|
|
23
|
+
variantIndex: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const getVariant = (
|
|
27
|
+
productData: ProductData,
|
|
28
|
+
variantId?: number,
|
|
29
|
+
sku?: string,
|
|
30
|
+
): VariantResult => {
|
|
31
|
+
const variants = [productData.masterVariant, ...productData.variants];
|
|
32
|
+
const foundVariant = variants.find((variant: ProductVariant) => {
|
|
33
|
+
if (variantId) {
|
|
34
|
+
return variant.id === variantId;
|
|
35
|
+
}
|
|
36
|
+
if (sku) {
|
|
37
|
+
return variant.sku === sku;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const isMasterVariant = foundVariant === productData.masterVariant;
|
|
43
|
+
return {
|
|
44
|
+
variant: foundVariant,
|
|
45
|
+
isMasterVariant,
|
|
46
|
+
variantIndex:
|
|
47
|
+
!isMasterVariant && foundVariant
|
|
48
|
+
? productData.variants.indexOf(foundVariant)
|
|
49
|
+
: -1,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Check if the product still has staged data that is different from the
|
|
54
|
+
// current data.
|
|
55
|
+
export const checkForStagedChanges = (product: Writable<Product>) => {
|
|
56
|
+
if (!product.masterData.staged) {
|
|
57
|
+
product.masterData.staged = product.masterData.current;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (deepEqual(product.masterData.current, product.masterData.staged)) {
|
|
61
|
+
product.masterData.hasStagedChanges = false;
|
|
62
|
+
} else {
|
|
63
|
+
product.masterData.hasStagedChanges = true;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const variantFromDraft = (
|
|
68
|
+
context: RepositoryContext,
|
|
69
|
+
storage: AbstractStorage,
|
|
70
|
+
variantId: number,
|
|
71
|
+
variant: ProductVariantDraft,
|
|
72
|
+
): ProductVariant => ({
|
|
73
|
+
id: variantId,
|
|
74
|
+
sku: variant?.sku,
|
|
75
|
+
key: variant?.key,
|
|
76
|
+
attributes: variant?.attributes ?? [],
|
|
77
|
+
prices: variant?.prices?.map((p) => priceFromDraft(context, storage, p)),
|
|
78
|
+
assets: [],
|
|
79
|
+
images: [],
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export const priceFromDraft = (
|
|
83
|
+
context: RepositoryContext,
|
|
84
|
+
storage: AbstractStorage,
|
|
85
|
+
draft: PriceDraft,
|
|
86
|
+
): Price => ({
|
|
87
|
+
id: uuidv4(),
|
|
88
|
+
key: draft.key,
|
|
89
|
+
country: draft.country,
|
|
90
|
+
value: createTypedMoney(draft.value),
|
|
91
|
+
channel: draft.channel
|
|
92
|
+
? getReferenceFromResourceIdentifier<ChannelReference>(
|
|
93
|
+
draft.channel,
|
|
94
|
+
context.projectKey,
|
|
95
|
+
storage,
|
|
96
|
+
)
|
|
97
|
+
: undefined,
|
|
98
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CategoryReference,
|
|
3
|
+
InvalidJsonInputError,
|
|
4
|
+
Product,
|
|
5
|
+
ProductData,
|
|
6
|
+
ProductDraft,
|
|
7
|
+
ProductTypeReference,
|
|
8
|
+
StateReference,
|
|
9
|
+
TaxCategoryReference,
|
|
10
|
+
} from "@commercetools/platform-sdk";
|
|
11
|
+
import { CommercetoolsError } from "~src/exceptions";
|
|
12
|
+
import { getBaseResourceProperties } from "~src/helpers";
|
|
13
|
+
import { AbstractStorage } from "~src/storage/abstract";
|
|
14
|
+
import { AbstractResourceRepository, RepositoryContext } from "../abstract";
|
|
15
|
+
import { getReferenceFromResourceIdentifier } from "../helpers";
|
|
16
|
+
import { ProductUpdateHandler } from "./actions";
|
|
17
|
+
import { variantFromDraft } from "./helpers";
|
|
18
|
+
|
|
19
|
+
export class ProductRepository extends AbstractResourceRepository<"product"> {
|
|
20
|
+
constructor(storage: AbstractStorage) {
|
|
21
|
+
super("product", storage);
|
|
22
|
+
this.actions = new ProductUpdateHandler(storage);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
create(context: RepositoryContext, draft: ProductDraft): Product {
|
|
26
|
+
if (!draft.masterVariant) {
|
|
27
|
+
throw new Error("Missing master variant");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let productType: ProductTypeReference | undefined = undefined;
|
|
31
|
+
try {
|
|
32
|
+
productType = getReferenceFromResourceIdentifier<ProductTypeReference>(
|
|
33
|
+
draft.productType,
|
|
34
|
+
context.projectKey,
|
|
35
|
+
this._storage,
|
|
36
|
+
);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
// For now accept missing product types (but warn)
|
|
39
|
+
console.warn(
|
|
40
|
+
`Error resolving product-type '${draft.productType.id}'. This will be throw an error in later releases.`,
|
|
41
|
+
);
|
|
42
|
+
productType = {
|
|
43
|
+
typeId: "product-type",
|
|
44
|
+
id: draft.productType.id || "",
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Resolve Product categories
|
|
49
|
+
const categoryReferences: CategoryReference[] = [];
|
|
50
|
+
draft.categories?.forEach((category) => {
|
|
51
|
+
if (category) {
|
|
52
|
+
categoryReferences.push(
|
|
53
|
+
getReferenceFromResourceIdentifier<CategoryReference>(
|
|
54
|
+
category,
|
|
55
|
+
context.projectKey,
|
|
56
|
+
this._storage,
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
} else {
|
|
60
|
+
throw new CommercetoolsError<InvalidJsonInputError>(
|
|
61
|
+
{
|
|
62
|
+
code: "InvalidJsonInput",
|
|
63
|
+
message: "Request body does not contain valid JSON.",
|
|
64
|
+
detailedErrorMessage: "categories: JSON object expected.",
|
|
65
|
+
},
|
|
66
|
+
400,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Resolve Tax category
|
|
72
|
+
let taxCategoryReference: TaxCategoryReference | undefined = undefined;
|
|
73
|
+
if (draft.taxCategory) {
|
|
74
|
+
taxCategoryReference =
|
|
75
|
+
getReferenceFromResourceIdentifier<TaxCategoryReference>(
|
|
76
|
+
draft.taxCategory,
|
|
77
|
+
context.projectKey,
|
|
78
|
+
this._storage,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Resolve Product State
|
|
83
|
+
let productStateReference: StateReference | undefined = undefined;
|
|
84
|
+
if (draft.state) {
|
|
85
|
+
productStateReference =
|
|
86
|
+
getReferenceFromResourceIdentifier<StateReference>(
|
|
87
|
+
draft.state,
|
|
88
|
+
context.projectKey,
|
|
89
|
+
this._storage,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const productData: ProductData = {
|
|
94
|
+
name: draft.name,
|
|
95
|
+
slug: draft.slug,
|
|
96
|
+
description: draft.description,
|
|
97
|
+
categories: categoryReferences,
|
|
98
|
+
masterVariant: variantFromDraft(
|
|
99
|
+
context,
|
|
100
|
+
this._storage,
|
|
101
|
+
1,
|
|
102
|
+
draft.masterVariant,
|
|
103
|
+
),
|
|
104
|
+
variants:
|
|
105
|
+
draft.variants?.map((variant, index) =>
|
|
106
|
+
variantFromDraft(context, this._storage, index + 2, variant),
|
|
107
|
+
) ?? [],
|
|
108
|
+
metaTitle: draft.metaTitle,
|
|
109
|
+
metaDescription: draft.metaDescription,
|
|
110
|
+
metaKeywords: draft.metaKeywords,
|
|
111
|
+
searchKeywords: draft.searchKeywords ?? {},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const resource: Product = {
|
|
115
|
+
...getBaseResourceProperties(),
|
|
116
|
+
key: draft.key,
|
|
117
|
+
productType: productType,
|
|
118
|
+
taxCategory: taxCategoryReference,
|
|
119
|
+
state: productStateReference,
|
|
120
|
+
masterData: {
|
|
121
|
+
current: productData,
|
|
122
|
+
staged: productData,
|
|
123
|
+
hasStagedChanges: false,
|
|
124
|
+
published: draft.publish ?? false,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return this.saveNew(context, resource);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -17,145 +17,155 @@ import type {
|
|
|
17
17
|
ProductDiscountValueDraft,
|
|
18
18
|
ProductDiscountValueExternal,
|
|
19
19
|
ProductDiscountValueRelative,
|
|
20
|
-
} from
|
|
21
|
-
import { getBaseResourceProperties } from
|
|
22
|
-
import
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
20
|
+
} from "@commercetools/platform-sdk";
|
|
21
|
+
import { getBaseResourceProperties } from "../helpers";
|
|
22
|
+
import { AbstractStorage } from "../storage/abstract";
|
|
23
|
+
import type { Writable } from "../types";
|
|
24
|
+
import {
|
|
25
|
+
AbstractResourceRepository,
|
|
26
|
+
AbstractUpdateHandler,
|
|
27
|
+
RepositoryContext,
|
|
28
|
+
UpdateHandlerInterface,
|
|
29
|
+
} from "./abstract";
|
|
30
|
+
import { createTypedMoney } from "./helpers";
|
|
25
31
|
|
|
26
|
-
export class ProductDiscountRepository extends AbstractResourceRepository<
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
export class ProductDiscountRepository extends AbstractResourceRepository<"product-discount"> {
|
|
33
|
+
constructor(storage: AbstractStorage) {
|
|
34
|
+
super("product-discount", storage);
|
|
35
|
+
this.actions = new ProductDiscountUpdateHandler(this._storage);
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
create(
|
|
32
39
|
context: RepositoryContext,
|
|
33
|
-
draft: ProductDiscountDraft
|
|
40
|
+
draft: ProductDiscountDraft,
|
|
34
41
|
): ProductDiscount {
|
|
35
42
|
const resource: ProductDiscount = {
|
|
36
43
|
...getBaseResourceProperties(),
|
|
37
44
|
key: draft.key,
|
|
38
45
|
name: draft.name,
|
|
39
46
|
description: draft.description,
|
|
40
|
-
value:
|
|
47
|
+
value: transformValueDraft(draft.value),
|
|
41
48
|
predicate: draft.predicate,
|
|
42
49
|
sortOrder: draft.sortOrder,
|
|
43
50
|
isActive: draft.isActive || false,
|
|
44
51
|
validFrom: draft.validFrom,
|
|
45
52
|
validUntil: draft.validUntil,
|
|
46
53
|
references: [],
|
|
54
|
+
};
|
|
55
|
+
return this.saveNew(context, resource);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const transformValueDraft = (
|
|
60
|
+
value: ProductDiscountValueDraft,
|
|
61
|
+
): ProductDiscountValue => {
|
|
62
|
+
switch (value.type) {
|
|
63
|
+
case "absolute": {
|
|
64
|
+
return {
|
|
65
|
+
type: "absolute",
|
|
66
|
+
money: value.money.map(createTypedMoney),
|
|
67
|
+
} as ProductDiscountValueAbsolute;
|
|
68
|
+
}
|
|
69
|
+
case "external": {
|
|
70
|
+
return {
|
|
71
|
+
type: "external",
|
|
72
|
+
} as ProductDiscountValueExternal;
|
|
47
73
|
}
|
|
48
|
-
|
|
49
|
-
|
|
74
|
+
case "relative": {
|
|
75
|
+
return {
|
|
76
|
+
...value,
|
|
77
|
+
} as ProductDiscountValueRelative;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export class ProductDiscountUpdateHandler
|
|
83
|
+
extends AbstractUpdateHandler
|
|
84
|
+
implements
|
|
85
|
+
UpdateHandlerInterface<ProductDiscount, ProductDiscountUpdateAction>
|
|
86
|
+
{
|
|
87
|
+
changeIsActive(
|
|
88
|
+
context: RepositoryContext,
|
|
89
|
+
resource: Writable<ProductDiscount>,
|
|
90
|
+
{ isActive }: ProductDiscountChangeIsActiveAction,
|
|
91
|
+
) {
|
|
92
|
+
resource.isActive = isActive;
|
|
50
93
|
}
|
|
51
94
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
95
|
+
changeName(
|
|
96
|
+
context: RepositoryContext,
|
|
97
|
+
resource: Writable<ProductDiscount>,
|
|
98
|
+
{ name }: ProductDiscountChangeNameAction,
|
|
99
|
+
) {
|
|
100
|
+
resource.name = name;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
changePredicate(
|
|
104
|
+
context: RepositoryContext,
|
|
105
|
+
resource: Writable<ProductDiscount>,
|
|
106
|
+
{ predicate }: ProductDiscountChangePredicateAction,
|
|
107
|
+
) {
|
|
108
|
+
resource.predicate = predicate;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
changeSortOrder(
|
|
112
|
+
context: RepositoryContext,
|
|
113
|
+
resource: Writable<ProductDiscount>,
|
|
114
|
+
{ sortOrder }: ProductDiscountChangeSortOrderAction,
|
|
115
|
+
) {
|
|
116
|
+
resource.sortOrder = sortOrder;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
changeValue(
|
|
120
|
+
context: RepositoryContext,
|
|
121
|
+
resource: Writable<ProductDiscount>,
|
|
122
|
+
{ value }: ProductDiscountChangeValueAction,
|
|
123
|
+
) {
|
|
124
|
+
resource.value = transformValueDraft(value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
setDescription(
|
|
128
|
+
context: RepositoryContext,
|
|
129
|
+
resource: Writable<ProductDiscount>,
|
|
130
|
+
{ description }: ProductDiscountSetDescriptionAction,
|
|
131
|
+
) {
|
|
132
|
+
if (description && Object.keys(description).length > 0) {
|
|
133
|
+
resource.description = description;
|
|
134
|
+
} else {
|
|
135
|
+
resource.description = undefined;
|
|
72
136
|
}
|
|
73
137
|
}
|
|
74
138
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
{ name }: ProductDiscountChangeNameAction
|
|
107
|
-
) => {
|
|
108
|
-
resource.name = name
|
|
109
|
-
},
|
|
110
|
-
changeValue: (
|
|
111
|
-
context: RepositoryContext,
|
|
112
|
-
resource: Writable<ProductDiscount>,
|
|
113
|
-
{ value }: ProductDiscountChangeValueAction
|
|
114
|
-
) => {
|
|
115
|
-
resource.value = this.transformValueDraft(value)
|
|
116
|
-
},
|
|
117
|
-
changePredicate: (
|
|
118
|
-
context: RepositoryContext,
|
|
119
|
-
resource: Writable<ProductDiscount>,
|
|
120
|
-
{ predicate }: ProductDiscountChangePredicateAction
|
|
121
|
-
) => {
|
|
122
|
-
resource.predicate = predicate
|
|
123
|
-
},
|
|
124
|
-
changeSortOrder: (
|
|
125
|
-
context: RepositoryContext,
|
|
126
|
-
resource: Writable<ProductDiscount>,
|
|
127
|
-
{ sortOrder }: ProductDiscountChangeSortOrderAction
|
|
128
|
-
) => {
|
|
129
|
-
resource.sortOrder = sortOrder
|
|
130
|
-
},
|
|
131
|
-
changeIsActive: (
|
|
132
|
-
context: RepositoryContext,
|
|
133
|
-
resource: Writable<ProductDiscount>,
|
|
134
|
-
{ isActive }: ProductDiscountChangeIsActiveAction
|
|
135
|
-
) => {
|
|
136
|
-
resource.isActive = isActive
|
|
137
|
-
},
|
|
138
|
-
setValidFrom: (
|
|
139
|
-
context: RepositoryContext,
|
|
140
|
-
resource: Writable<ProductDiscount>,
|
|
141
|
-
{ validFrom }: ProductDiscountSetValidFromAction
|
|
142
|
-
) => {
|
|
143
|
-
resource.validFrom = validFrom
|
|
144
|
-
},
|
|
145
|
-
setValidUntil: (
|
|
146
|
-
context: RepositoryContext,
|
|
147
|
-
resource: Writable<ProductDiscount>,
|
|
148
|
-
{ validUntil }: ProductDiscountSetValidUntilAction
|
|
149
|
-
) => {
|
|
150
|
-
resource.validUntil = validUntil
|
|
151
|
-
},
|
|
152
|
-
setValidFromAndUntil: (
|
|
153
|
-
context: RepositoryContext,
|
|
154
|
-
resource: Writable<ProductDiscount>,
|
|
155
|
-
{ validFrom, validUntil }: ProductDiscountSetValidFromAndUntilAction
|
|
156
|
-
) => {
|
|
157
|
-
resource.validFrom = validFrom
|
|
158
|
-
resource.validUntil = validUntil
|
|
159
|
-
},
|
|
139
|
+
setKey(
|
|
140
|
+
context: RepositoryContext,
|
|
141
|
+
resource: Writable<ProductDiscount>,
|
|
142
|
+
{ key }: ProductDiscountSetKeyAction,
|
|
143
|
+
) {
|
|
144
|
+
resource.key = key;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setValidFrom(
|
|
148
|
+
context: RepositoryContext,
|
|
149
|
+
resource: Writable<ProductDiscount>,
|
|
150
|
+
{ validFrom }: ProductDiscountSetValidFromAction,
|
|
151
|
+
) {
|
|
152
|
+
resource.validFrom = validFrom;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setValidFromAndUntil(
|
|
156
|
+
context: RepositoryContext,
|
|
157
|
+
resource: Writable<ProductDiscount>,
|
|
158
|
+
{ validFrom, validUntil }: ProductDiscountSetValidFromAndUntilAction,
|
|
159
|
+
) {
|
|
160
|
+
resource.validFrom = validFrom;
|
|
161
|
+
resource.validUntil = validUntil;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
setValidUntil(
|
|
165
|
+
context: RepositoryContext,
|
|
166
|
+
resource: Writable<ProductDiscount>,
|
|
167
|
+
{ validUntil }: ProductDiscountSetValidUntilAction,
|
|
168
|
+
) {
|
|
169
|
+
resource.validUntil = validUntil;
|
|
160
170
|
}
|
|
161
171
|
}
|
|
@@ -3,101 +3,97 @@ import type {
|
|
|
3
3
|
ProductDraft,
|
|
4
4
|
ProductProjection,
|
|
5
5
|
QueryParam,
|
|
6
|
-
} from
|
|
7
|
-
import { CommercetoolsError } from
|
|
8
|
-
import { parseQueryExpression } from
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
6
|
+
} from "@commercetools/platform-sdk";
|
|
7
|
+
import { CommercetoolsError } from "~src/exceptions";
|
|
8
|
+
import { parseQueryExpression } from "../lib/predicateParser";
|
|
9
|
+
import { applyPriceSelector } from "../priceSelector";
|
|
10
|
+
import { ProductProjectionSearch } from "../product-projection-search";
|
|
11
|
+
import { type AbstractStorage } from "../storage";
|
|
11
12
|
import {
|
|
12
13
|
AbstractResourceRepository,
|
|
13
14
|
GetParams,
|
|
14
15
|
RepositoryContext,
|
|
15
|
-
} from
|
|
16
|
-
import { applyPriceSelector } from '../priceSelector.js'
|
|
16
|
+
} from "./abstract";
|
|
17
17
|
|
|
18
18
|
export type ProductProjectionQueryParams = {
|
|
19
|
-
staged?: boolean
|
|
20
|
-
priceCurrency?: string
|
|
21
|
-
priceCountry?: string
|
|
22
|
-
priceCustomerGroup?: string
|
|
23
|
-
priceChannel?: string
|
|
24
|
-
localeProjection?: string
|
|
25
|
-
storeProjection?: string
|
|
26
|
-
expand?: string | string[]
|
|
27
|
-
sort?: string | string[]
|
|
28
|
-
limit?: number
|
|
29
|
-
offset?: number
|
|
30
|
-
withTotal?: boolean
|
|
31
|
-
where?: string | string[]
|
|
32
|
-
[key: string]: QueryParam
|
|
33
|
-
}
|
|
19
|
+
staged?: boolean;
|
|
20
|
+
priceCurrency?: string;
|
|
21
|
+
priceCountry?: string;
|
|
22
|
+
priceCustomerGroup?: string;
|
|
23
|
+
priceChannel?: string;
|
|
24
|
+
localeProjection?: string;
|
|
25
|
+
storeProjection?: string;
|
|
26
|
+
expand?: string | string[];
|
|
27
|
+
sort?: string | string[];
|
|
28
|
+
limit?: number;
|
|
29
|
+
offset?: number;
|
|
30
|
+
withTotal?: boolean;
|
|
31
|
+
where?: string | string[];
|
|
32
|
+
[key: string]: QueryParam;
|
|
33
|
+
};
|
|
34
34
|
|
|
35
|
-
export class ProductProjectionRepository extends AbstractResourceRepository<
|
|
36
|
-
protected _searchService: ProductProjectionSearch
|
|
35
|
+
export class ProductProjectionRepository extends AbstractResourceRepository<"product-projection"> {
|
|
36
|
+
protected _searchService: ProductProjectionSearch;
|
|
37
37
|
|
|
38
38
|
constructor(storage: AbstractStorage) {
|
|
39
|
-
super(storage)
|
|
40
|
-
this._searchService = new ProductProjectionSearch(storage)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
getTypeId() {
|
|
44
|
-
return 'product-projection' as const
|
|
39
|
+
super("product-projection", storage);
|
|
40
|
+
this._searchService = new ProductProjectionSearch(storage);
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
create(context: RepositoryContext, draft: ProductDraft): ProductProjection {
|
|
48
|
-
throw new Error(
|
|
44
|
+
throw new Error("No valid action");
|
|
49
45
|
}
|
|
50
46
|
|
|
51
47
|
get(
|
|
52
48
|
context: RepositoryContext,
|
|
53
49
|
id: string,
|
|
54
|
-
params: GetParams = {}
|
|
50
|
+
params: GetParams = {},
|
|
55
51
|
): ProductProjection | null {
|
|
56
52
|
const resource = this._storage.get(
|
|
57
53
|
context.projectKey,
|
|
58
|
-
|
|
54
|
+
"product",
|
|
59
55
|
id,
|
|
60
|
-
params
|
|
61
|
-
)
|
|
56
|
+
params,
|
|
57
|
+
);
|
|
62
58
|
if (resource) {
|
|
63
|
-
return this._searchService.transform(resource, false)
|
|
59
|
+
return this._searchService.transform(resource, false);
|
|
64
60
|
}
|
|
65
|
-
return null
|
|
61
|
+
return null;
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
query(context: RepositoryContext, params: ProductProjectionQueryParams = {}) {
|
|
69
65
|
let resources = this._storage
|
|
70
|
-
.all(context.projectKey,
|
|
66
|
+
.all(context.projectKey, "product")
|
|
71
67
|
.map((r) => this._searchService.transform(r, params.staged ?? false))
|
|
72
68
|
.filter((p) => {
|
|
73
69
|
if (!params.staged ?? false) {
|
|
74
|
-
return p.published
|
|
70
|
+
return p.published;
|
|
75
71
|
}
|
|
76
|
-
return true
|
|
77
|
-
})
|
|
72
|
+
return true;
|
|
73
|
+
});
|
|
78
74
|
|
|
79
75
|
// Apply predicates
|
|
80
76
|
if (params.where) {
|
|
81
|
-
const variableMap: Record<string, QueryParam> = {}
|
|
77
|
+
const variableMap: Record<string, QueryParam> = {};
|
|
82
78
|
for (const [k, v] of Object.entries(params)) {
|
|
83
|
-
if (k.startsWith(
|
|
84
|
-
variableMap[k.substring(4)] = v
|
|
79
|
+
if (k.startsWith("var.")) {
|
|
80
|
+
variableMap[k.substring(4)] = v;
|
|
85
81
|
}
|
|
86
82
|
}
|
|
87
83
|
|
|
88
84
|
try {
|
|
89
|
-
const filterFunc = parseQueryExpression(params.where)
|
|
85
|
+
const filterFunc = parseQueryExpression(params.where);
|
|
90
86
|
resources = resources.filter((resource) =>
|
|
91
|
-
filterFunc(resource, variableMap)
|
|
92
|
-
)
|
|
87
|
+
filterFunc(resource, variableMap),
|
|
88
|
+
);
|
|
93
89
|
} catch (err) {
|
|
94
90
|
throw new CommercetoolsError<InvalidInputError>(
|
|
95
91
|
{
|
|
96
|
-
code:
|
|
92
|
+
code: "InvalidInput",
|
|
97
93
|
message: (err as any).message,
|
|
98
94
|
},
|
|
99
|
-
400
|
|
100
|
-
)
|
|
95
|
+
400,
|
|
96
|
+
);
|
|
101
97
|
}
|
|
102
98
|
}
|
|
103
99
|
|
|
@@ -112,23 +108,23 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
|
|
|
112
108
|
customerGroup: params.priceCustomerGroup,
|
|
113
109
|
currency: params.priceCurrency,
|
|
114
110
|
},
|
|
115
|
-
true
|
|
116
|
-
)
|
|
111
|
+
true,
|
|
112
|
+
);
|
|
117
113
|
|
|
118
114
|
// Expand the resources
|
|
119
115
|
if (params.expand !== undefined) {
|
|
120
116
|
resources = resources.map((resource) =>
|
|
121
|
-
this._storage.expand(context.projectKey, resource, params.expand)
|
|
122
|
-
)
|
|
117
|
+
this._storage.expand(context.projectKey, resource, params.expand),
|
|
118
|
+
);
|
|
123
119
|
}
|
|
124
120
|
|
|
125
121
|
// Create a slice for the pagination. If we were working with large datasets
|
|
126
122
|
// then we should have done this before transforming. But that isn't the
|
|
127
123
|
// goal of this library. So lets keep it simple.
|
|
128
|
-
const totalResults = resources.length
|
|
129
|
-
const offset = params.offset || 0
|
|
130
|
-
const limit = params.limit || 20
|
|
131
|
-
const results = resources.slice(offset, offset + limit)
|
|
124
|
+
const totalResults = resources.length;
|
|
125
|
+
const offset = params.offset || 0;
|
|
126
|
+
const limit = params.limit || 20;
|
|
127
|
+
const results = resources.slice(offset, offset + limit);
|
|
132
128
|
|
|
133
129
|
return {
|
|
134
130
|
count: totalResults,
|
|
@@ -136,12 +132,10 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
|
|
|
136
132
|
offset: offset,
|
|
137
133
|
limit: limit,
|
|
138
134
|
results: results,
|
|
139
|
-
}
|
|
135
|
+
};
|
|
140
136
|
}
|
|
141
137
|
|
|
142
138
|
search(context: RepositoryContext, query: ProductProjectionQueryParams) {
|
|
143
|
-
return this._searchService.search(context.projectKey, query)
|
|
139
|
+
return this._searchService.search(context.projectKey, query);
|
|
144
140
|
}
|
|
145
|
-
|
|
146
|
-
actions = {}
|
|
147
141
|
}
|