@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
|
@@ -8,9 +8,9 @@ import type {
|
|
|
8
8
|
QueryParam,
|
|
9
9
|
RangeFacetResult,
|
|
10
10
|
TermFacetResult,
|
|
11
|
-
} from
|
|
12
|
-
import { CommercetoolsError } from
|
|
13
|
-
import { nestedLookup } from
|
|
11
|
+
} from "@commercetools/platform-sdk";
|
|
12
|
+
import { CommercetoolsError } from "./exceptions";
|
|
13
|
+
import { nestedLookup } from "./helpers";
|
|
14
14
|
import {
|
|
15
15
|
FilterExpression,
|
|
16
16
|
RangeExpression,
|
|
@@ -18,56 +18,56 @@ import {
|
|
|
18
18
|
getVariants,
|
|
19
19
|
parseFilterExpression,
|
|
20
20
|
resolveVariantValue,
|
|
21
|
-
} from
|
|
22
|
-
import { applyPriceSelector } from
|
|
23
|
-
import { AbstractStorage } from
|
|
24
|
-
import type { Writable } from
|
|
21
|
+
} from "./lib/projectionSearchFilter";
|
|
22
|
+
import { applyPriceSelector } from "./priceSelector";
|
|
23
|
+
import { AbstractStorage } from "./storage";
|
|
24
|
+
import type { Writable } from "./types";
|
|
25
25
|
|
|
26
26
|
export type ProductProjectionSearchParams = {
|
|
27
|
-
fuzzy?: boolean
|
|
28
|
-
fuzzyLevel?: number
|
|
29
|
-
markMatchingVariants?: boolean
|
|
30
|
-
staged?: boolean
|
|
31
|
-
filter?: string[]
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
facet?: string | string[]
|
|
35
|
-
sort?: string | string[]
|
|
36
|
-
limit?: number
|
|
37
|
-
offset?: number
|
|
38
|
-
withTotal?: boolean
|
|
39
|
-
priceCurrency?: string
|
|
40
|
-
priceCountry?: string
|
|
41
|
-
priceCustomerGroup?: string
|
|
42
|
-
priceChannel?: string
|
|
43
|
-
localeProjection?: string
|
|
44
|
-
storeProjection?: string
|
|
45
|
-
expand?: string | string[]
|
|
46
|
-
[key: string]: QueryParam
|
|
47
|
-
}
|
|
27
|
+
"fuzzy"?: boolean;
|
|
28
|
+
"fuzzyLevel"?: number;
|
|
29
|
+
"markMatchingVariants"?: boolean;
|
|
30
|
+
"staged"?: boolean;
|
|
31
|
+
"filter"?: string[];
|
|
32
|
+
"filter.facets"?: string[];
|
|
33
|
+
"filter.query"?: string[];
|
|
34
|
+
"facet"?: string | string[];
|
|
35
|
+
"sort"?: string | string[];
|
|
36
|
+
"limit"?: number;
|
|
37
|
+
"offset"?: number;
|
|
38
|
+
"withTotal"?: boolean;
|
|
39
|
+
"priceCurrency"?: string;
|
|
40
|
+
"priceCountry"?: string;
|
|
41
|
+
"priceCustomerGroup"?: string;
|
|
42
|
+
"priceChannel"?: string;
|
|
43
|
+
"localeProjection"?: string;
|
|
44
|
+
"storeProjection"?: string;
|
|
45
|
+
"expand"?: string | string[];
|
|
46
|
+
[key: string]: QueryParam;
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
export class ProductProjectionSearch {
|
|
50
|
-
protected _storage: AbstractStorage
|
|
50
|
+
protected _storage: AbstractStorage;
|
|
51
51
|
|
|
52
52
|
constructor(storage: AbstractStorage) {
|
|
53
|
-
this._storage = storage
|
|
53
|
+
this._storage = storage;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
search(
|
|
57
57
|
projectKey: string,
|
|
58
|
-
params: ProductProjectionSearchParams
|
|
58
|
+
params: ProductProjectionSearchParams,
|
|
59
59
|
): ProductProjectionPagedSearchResponse {
|
|
60
60
|
let resources = this._storage
|
|
61
|
-
.all(projectKey,
|
|
61
|
+
.all(projectKey, "product")
|
|
62
62
|
.map((r) => this.transform(r, params.staged ?? false))
|
|
63
63
|
.filter((p) => {
|
|
64
64
|
if (!params.staged ?? false) {
|
|
65
|
-
return p.published
|
|
65
|
+
return p.published;
|
|
66
66
|
}
|
|
67
|
-
return true
|
|
68
|
-
})
|
|
67
|
+
return true;
|
|
68
|
+
});
|
|
69
69
|
|
|
70
|
-
const markMatchingVariant = params.markMatchingVariants ?? false
|
|
70
|
+
const markMatchingVariant = params.markMatchingVariants ?? false;
|
|
71
71
|
|
|
72
72
|
// Apply the priceSelector
|
|
73
73
|
applyPriceSelector(resources, {
|
|
@@ -75,64 +75,64 @@ export class ProductProjectionSearch {
|
|
|
75
75
|
channel: params.priceChannel,
|
|
76
76
|
customerGroup: params.priceCustomerGroup,
|
|
77
77
|
currency: params.priceCurrency,
|
|
78
|
-
})
|
|
78
|
+
});
|
|
79
79
|
|
|
80
80
|
// Apply filters pre facetting
|
|
81
81
|
if (params.filter) {
|
|
82
82
|
try {
|
|
83
|
-
const filters = params.filter.map(parseFilterExpression)
|
|
83
|
+
const filters = params.filter.map(parseFilterExpression);
|
|
84
84
|
|
|
85
85
|
// Filters can modify the output. So clone the resources first.
|
|
86
86
|
resources = resources.filter((resource) =>
|
|
87
|
-
filters.every((f) => f(resource, markMatchingVariant))
|
|
88
|
-
)
|
|
87
|
+
filters.every((f) => f(resource, markMatchingVariant)),
|
|
88
|
+
);
|
|
89
89
|
} catch (err) {
|
|
90
|
-
console.error(err)
|
|
90
|
+
console.error(err);
|
|
91
91
|
throw new CommercetoolsError<InvalidInputError>(
|
|
92
92
|
{
|
|
93
|
-
code:
|
|
93
|
+
code: "InvalidInput",
|
|
94
94
|
message: (err as any).message,
|
|
95
95
|
},
|
|
96
|
-
400
|
|
97
|
-
)
|
|
96
|
+
400,
|
|
97
|
+
);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
// TODO: Calculate facets
|
|
102
|
-
const facets = this.getFacets(params, resources)
|
|
102
|
+
const facets = this.getFacets(params, resources);
|
|
103
103
|
|
|
104
104
|
// Apply filters post facetting
|
|
105
|
-
if (params[
|
|
105
|
+
if (params["filter.query"]) {
|
|
106
106
|
try {
|
|
107
|
-
const filters = params[
|
|
107
|
+
const filters = params["filter.query"].map(parseFilterExpression);
|
|
108
108
|
resources = resources.filter((resource) =>
|
|
109
|
-
filters.every((f) => f(resource, markMatchingVariant))
|
|
110
|
-
)
|
|
109
|
+
filters.every((f) => f(resource, markMatchingVariant)),
|
|
110
|
+
);
|
|
111
111
|
} catch (err) {
|
|
112
112
|
throw new CommercetoolsError<InvalidInputError>(
|
|
113
113
|
{
|
|
114
|
-
code:
|
|
114
|
+
code: "InvalidInput",
|
|
115
115
|
message: (err as any).message,
|
|
116
116
|
},
|
|
117
|
-
400
|
|
118
|
-
)
|
|
117
|
+
400,
|
|
118
|
+
);
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
// Expand the resources
|
|
123
123
|
if (params.expand !== undefined) {
|
|
124
124
|
resources = resources.map((resource) =>
|
|
125
|
-
this._storage.expand(projectKey, resource, params.expand)
|
|
126
|
-
)
|
|
125
|
+
this._storage.expand(projectKey, resource, params.expand),
|
|
126
|
+
);
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
// Create a slice for the pagination. If we were working with large datasets
|
|
130
130
|
// then we should have done this before transforming. But that isn't the
|
|
131
131
|
// goal of this library. So lets keep it simple.
|
|
132
|
-
const totalResults = resources.length
|
|
133
|
-
const offset = params.offset || 0
|
|
134
|
-
const limit = params.limit || 20
|
|
135
|
-
const results = resources.slice(offset, offset + limit)
|
|
132
|
+
const totalResults = resources.length;
|
|
133
|
+
const offset = params.offset || 0;
|
|
134
|
+
const limit = params.limit || 20;
|
|
135
|
+
const results = resources.slice(offset, offset + limit);
|
|
136
136
|
|
|
137
137
|
return {
|
|
138
138
|
count: totalResults,
|
|
@@ -141,11 +141,13 @@ export class ProductProjectionSearch {
|
|
|
141
141
|
limit: limit,
|
|
142
142
|
results: results,
|
|
143
143
|
facets: facets,
|
|
144
|
-
}
|
|
144
|
+
};
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
transform(product: Product, staged: boolean): ProductProjection {
|
|
148
|
-
const obj = !staged
|
|
148
|
+
const obj = !staged
|
|
149
|
+
? product.masterData.current
|
|
150
|
+
: product.masterData.staged;
|
|
149
151
|
|
|
150
152
|
return {
|
|
151
153
|
id: product.id,
|
|
@@ -163,57 +165,57 @@ export class ProductProjectionSearch {
|
|
|
163
165
|
productType: product.productType,
|
|
164
166
|
hasStagedChanges: product.masterData.hasStagedChanges,
|
|
165
167
|
published: product.masterData.published,
|
|
166
|
-
}
|
|
168
|
+
};
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
getFacets(
|
|
170
172
|
params: ProductProjectionSearchParams,
|
|
171
|
-
products: ProductProjection[]
|
|
173
|
+
products: ProductProjection[],
|
|
172
174
|
): FacetResults {
|
|
173
|
-
if (!params.facet) return {}
|
|
174
|
-
const result: FacetResults = {}
|
|
175
|
+
if (!params.facet) return {};
|
|
176
|
+
const result: FacetResults = {};
|
|
175
177
|
|
|
176
|
-
const regexp = new RegExp(/ counting products$/)
|
|
178
|
+
const regexp = new RegExp(/ counting products$/);
|
|
177
179
|
for (let facet of params.facet) {
|
|
178
|
-
let countProducts = false
|
|
179
|
-
if (facet.endsWith(
|
|
180
|
-
facet = facet.replace(regexp,
|
|
181
|
-
countProducts = true
|
|
180
|
+
let countProducts = false;
|
|
181
|
+
if (facet.endsWith(" counting products")) {
|
|
182
|
+
facet = facet.replace(regexp, "");
|
|
183
|
+
countProducts = true;
|
|
182
184
|
}
|
|
183
185
|
|
|
184
|
-
const expression = generateFacetFunc(facet)
|
|
186
|
+
const expression = generateFacetFunc(facet);
|
|
185
187
|
|
|
186
188
|
// Term Facet
|
|
187
|
-
if (expression.type ===
|
|
189
|
+
if (expression.type === "TermExpression") {
|
|
188
190
|
result[facet] = this.termFacet(
|
|
189
191
|
expression.source,
|
|
190
192
|
products,
|
|
191
|
-
countProducts
|
|
192
|
-
)
|
|
193
|
+
countProducts,
|
|
194
|
+
);
|
|
193
195
|
}
|
|
194
196
|
|
|
195
197
|
// Range Facet
|
|
196
|
-
if (expression.type ===
|
|
198
|
+
if (expression.type === "RangeExpression") {
|
|
197
199
|
result[expression.source] = this.rangeFacet(
|
|
198
200
|
expression.source,
|
|
199
201
|
expression.children,
|
|
200
202
|
products,
|
|
201
|
-
countProducts
|
|
202
|
-
)
|
|
203
|
+
countProducts,
|
|
204
|
+
);
|
|
203
205
|
}
|
|
204
206
|
|
|
205
207
|
// FilteredFacet
|
|
206
|
-
if (expression.type ===
|
|
208
|
+
if (expression.type === "FilterExpression") {
|
|
207
209
|
result[expression.source] = this.filterFacet(
|
|
208
210
|
expression.source,
|
|
209
211
|
expression.children,
|
|
210
212
|
products,
|
|
211
|
-
countProducts
|
|
212
|
-
)
|
|
213
|
+
countProducts,
|
|
214
|
+
);
|
|
213
215
|
}
|
|
214
216
|
}
|
|
215
217
|
|
|
216
|
-
return result
|
|
218
|
+
return result;
|
|
217
219
|
}
|
|
218
220
|
|
|
219
221
|
/**
|
|
@@ -224,137 +226,137 @@ export class ProductProjectionSearch {
|
|
|
224
226
|
termFacet(
|
|
225
227
|
facet: string,
|
|
226
228
|
products: ProductProjection[],
|
|
227
|
-
countProducts: boolean
|
|
229
|
+
countProducts: boolean,
|
|
228
230
|
): TermFacetResult {
|
|
229
231
|
const result: Writable<TermFacetResult> = {
|
|
230
|
-
type:
|
|
231
|
-
dataType:
|
|
232
|
+
type: "terms",
|
|
233
|
+
dataType: "text",
|
|
232
234
|
missing: 0,
|
|
233
235
|
total: 0,
|
|
234
236
|
other: 0,
|
|
235
237
|
terms: [],
|
|
236
|
-
}
|
|
237
|
-
const terms: Record<any, number> = {}
|
|
238
|
+
};
|
|
239
|
+
const terms: Record<any, number> = {};
|
|
238
240
|
|
|
239
|
-
if (facet.startsWith(
|
|
240
|
-
|
|
241
|
-
const variants = getVariants(p)
|
|
242
|
-
|
|
243
|
-
result.total
|
|
241
|
+
if (facet.startsWith("variants.")) {
|
|
242
|
+
for (const p of products) {
|
|
243
|
+
const variants = getVariants(p);
|
|
244
|
+
for (const v of variants) {
|
|
245
|
+
result.total++;
|
|
244
246
|
|
|
245
|
-
let value = resolveVariantValue(v, facet)
|
|
247
|
+
let value = resolveVariantValue(v, facet);
|
|
246
248
|
if (value === undefined) {
|
|
247
|
-
result.missing
|
|
249
|
+
result.missing++;
|
|
248
250
|
} else {
|
|
249
|
-
if (typeof value ===
|
|
250
|
-
value = Number(value).toFixed(1)
|
|
251
|
+
if (typeof value === "number") {
|
|
252
|
+
value = Number(value).toFixed(1);
|
|
251
253
|
}
|
|
252
|
-
terms[value] = value in terms ? terms[value] + 1 : 1
|
|
254
|
+
terms[value] = value in terms ? terms[value] + 1 : 1;
|
|
253
255
|
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
256
258
|
} else {
|
|
257
|
-
|
|
258
|
-
const value = nestedLookup(p, facet)
|
|
259
|
-
result.total
|
|
259
|
+
for (const p of products) {
|
|
260
|
+
const value = nestedLookup(p, facet);
|
|
261
|
+
result.total++;
|
|
260
262
|
if (value === undefined) {
|
|
261
|
-
result.missing
|
|
263
|
+
result.missing++;
|
|
262
264
|
} else {
|
|
263
|
-
terms[value] = value in terms ? terms[value] + 1 : 1
|
|
265
|
+
terms[value] = value in terms ? terms[value] + 1 : 1;
|
|
264
266
|
}
|
|
265
|
-
}
|
|
267
|
+
}
|
|
266
268
|
}
|
|
267
269
|
for (const term in terms) {
|
|
268
270
|
result.terms.push({
|
|
269
271
|
term: term as any,
|
|
270
272
|
count: terms[term],
|
|
271
|
-
})
|
|
273
|
+
});
|
|
272
274
|
}
|
|
273
275
|
|
|
274
|
-
return result
|
|
276
|
+
return result;
|
|
275
277
|
}
|
|
276
278
|
|
|
277
279
|
filterFacet(
|
|
278
280
|
source: string,
|
|
279
281
|
filters: FilterExpression[] | undefined,
|
|
280
282
|
products: ProductProjection[],
|
|
281
|
-
countProducts: boolean
|
|
283
|
+
countProducts: boolean,
|
|
282
284
|
): FilteredFacetResult {
|
|
283
|
-
let count = 0
|
|
284
|
-
if (source.startsWith(
|
|
285
|
+
let count = 0;
|
|
286
|
+
if (source.startsWith("variants.")) {
|
|
285
287
|
for (const p of products) {
|
|
286
288
|
for (const v of getVariants(p)) {
|
|
287
|
-
const val = resolveVariantValue(v, source)
|
|
289
|
+
const val = resolveVariantValue(v, source);
|
|
288
290
|
if (filters?.some((f) => f.match(val))) {
|
|
289
|
-
count
|
|
291
|
+
count++;
|
|
290
292
|
}
|
|
291
293
|
}
|
|
292
294
|
}
|
|
293
295
|
} else {
|
|
294
|
-
throw new Error(
|
|
296
|
+
throw new Error("not supported");
|
|
295
297
|
}
|
|
296
298
|
|
|
297
299
|
return {
|
|
298
|
-
type:
|
|
300
|
+
type: "filter",
|
|
299
301
|
count: count,
|
|
300
|
-
}
|
|
302
|
+
};
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
rangeFacet(
|
|
304
306
|
source: string,
|
|
305
307
|
ranges: RangeExpression[] | undefined,
|
|
306
308
|
products: ProductProjection[],
|
|
307
|
-
countProducts: boolean
|
|
309
|
+
countProducts: boolean,
|
|
308
310
|
): RangeFacetResult {
|
|
309
311
|
const counts =
|
|
310
312
|
ranges?.map((range) => {
|
|
311
|
-
if (source.startsWith(
|
|
312
|
-
const values = []
|
|
313
|
+
if (source.startsWith("variants.")) {
|
|
314
|
+
const values: number[] = [];
|
|
313
315
|
for (const p of products) {
|
|
314
316
|
for (const v of getVariants(p)) {
|
|
315
|
-
const val = resolveVariantValue(v, source)
|
|
317
|
+
const val = resolveVariantValue(v, source);
|
|
316
318
|
if (val === undefined) {
|
|
317
|
-
continue
|
|
319
|
+
continue;
|
|
318
320
|
}
|
|
319
321
|
|
|
320
322
|
if (range.match(val)) {
|
|
321
|
-
values.push(val)
|
|
323
|
+
values.push(val);
|
|
322
324
|
}
|
|
323
325
|
}
|
|
324
326
|
}
|
|
325
327
|
|
|
326
|
-
const numValues = values.length
|
|
328
|
+
const numValues = values.length;
|
|
327
329
|
return {
|
|
328
|
-
type:
|
|
330
|
+
type: "double",
|
|
329
331
|
from: range.start || 0,
|
|
330
|
-
fromStr: range.start !== null ? Number(range.start).toFixed(1) :
|
|
332
|
+
fromStr: range.start !== null ? Number(range.start).toFixed(1) : "",
|
|
331
333
|
to: range.stop || 0,
|
|
332
|
-
toStr: range.stop !== null ? Number(range.stop).toFixed(1) :
|
|
334
|
+
toStr: range.stop !== null ? Number(range.stop).toFixed(1) : "",
|
|
333
335
|
count: numValues,
|
|
334
336
|
// totalCount: 0,
|
|
335
337
|
total: values.reduce((a, b) => a + b, 0),
|
|
336
338
|
min: numValues > 0 ? Math.min(...values) : 0,
|
|
337
339
|
max: numValues > 0 ? Math.max(...values) : 0,
|
|
338
340
|
mean: numValues > 0 ? mean(values) : 0,
|
|
339
|
-
}
|
|
341
|
+
};
|
|
340
342
|
} else {
|
|
341
|
-
throw new Error(
|
|
343
|
+
throw new Error("not supported");
|
|
342
344
|
}
|
|
343
|
-
}) || []
|
|
345
|
+
}) || [];
|
|
344
346
|
const data: RangeFacetResult = {
|
|
345
|
-
type:
|
|
347
|
+
type: "range",
|
|
346
348
|
// @ts-ignore
|
|
347
|
-
dataType:
|
|
349
|
+
dataType: "number",
|
|
348
350
|
ranges: counts,
|
|
349
|
-
}
|
|
350
|
-
return data
|
|
351
|
+
};
|
|
352
|
+
return data;
|
|
351
353
|
}
|
|
352
354
|
}
|
|
353
355
|
|
|
354
356
|
const mean = (arr: number[]) => {
|
|
355
|
-
let total = 0
|
|
357
|
+
let total = 0;
|
|
356
358
|
for (let i = 0; i < arr.length; i++) {
|
|
357
|
-
total += arr[i]
|
|
359
|
+
total += arr[i];
|
|
358
360
|
}
|
|
359
|
-
return total / arr.length
|
|
360
|
-
}
|
|
361
|
+
return total / arr.length;
|
|
362
|
+
};
|
package/src/projectAPI.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { test } from "vitest";
|
|
2
|
+
import { CommercetoolsMock } from "./index";
|
|
3
3
|
|
|
4
|
-
test(
|
|
5
|
-
const ctMock = new CommercetoolsMock()
|
|
6
|
-
const repo = ctMock.project(
|
|
7
|
-
repo.get({ projectKey:
|
|
8
|
-
})
|
|
4
|
+
test("getRepository", async () => {
|
|
5
|
+
const ctMock = new CommercetoolsMock();
|
|
6
|
+
const repo = ctMock.project("my-project-key").getRepository("order");
|
|
7
|
+
repo.get({ projectKey: "unittest" }, "1234");
|
|
8
|
+
});
|
package/src/projectAPI.ts
CHANGED
|
@@ -1,58 +1,60 @@
|
|
|
1
|
-
import { getBaseResourceProperties } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { AbstractStorage } from
|
|
5
|
-
import { ResourceMap, ResourceType } from
|
|
1
|
+
import { getBaseResourceProperties } from "./helpers";
|
|
2
|
+
import { RepositoryMap } from "./repositories";
|
|
3
|
+
import { GetParams } from "./repositories/abstract";
|
|
4
|
+
import { AbstractStorage } from "./storage";
|
|
5
|
+
import { ResourceMap, ResourceType } from "./types";
|
|
6
6
|
|
|
7
7
|
export class ProjectAPI {
|
|
8
|
-
private projectKey: string
|
|
9
|
-
|
|
10
|
-
private
|
|
8
|
+
private projectKey: string;
|
|
9
|
+
|
|
10
|
+
private _storage: AbstractStorage;
|
|
11
|
+
|
|
12
|
+
private _repositories: RepositoryMap;
|
|
11
13
|
|
|
12
14
|
constructor(
|
|
13
15
|
projectKey: string,
|
|
14
16
|
repositories: RepositoryMap,
|
|
15
|
-
storage: AbstractStorage
|
|
17
|
+
storage: AbstractStorage,
|
|
16
18
|
) {
|
|
17
|
-
this.projectKey = projectKey
|
|
18
|
-
this._storage = storage
|
|
19
|
-
this._repositories = repositories
|
|
19
|
+
this.projectKey = projectKey;
|
|
20
|
+
this._storage = storage;
|
|
21
|
+
this._repositories = repositories;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
add<T extends keyof RepositoryMap & keyof ResourceMap>(
|
|
23
25
|
typeId: T,
|
|
24
|
-
resource: ResourceMap[T]
|
|
26
|
+
resource: ResourceMap[T],
|
|
25
27
|
) {
|
|
26
|
-
const repository = this._repositories[typeId]
|
|
28
|
+
const repository = this._repositories[typeId];
|
|
27
29
|
if (repository) {
|
|
28
30
|
this._storage.add(this.projectKey, typeId, {
|
|
29
31
|
...getBaseResourceProperties(),
|
|
30
32
|
...resource,
|
|
31
|
-
})
|
|
33
|
+
});
|
|
32
34
|
} else {
|
|
33
|
-
throw new Error(`Service for ${typeId} not implemented yet`)
|
|
35
|
+
throw new Error(`Service for ${typeId} not implemented yet`);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
get<RT extends ResourceType>(
|
|
38
40
|
typeId: RT,
|
|
39
41
|
id: string,
|
|
40
|
-
params?: GetParams
|
|
42
|
+
params?: GetParams,
|
|
41
43
|
): ResourceMap[RT] {
|
|
42
44
|
return this._storage.get(
|
|
43
45
|
this.projectKey,
|
|
44
46
|
typeId,
|
|
45
47
|
id,
|
|
46
|
-
params
|
|
47
|
-
) as ResourceMap[RT]
|
|
48
|
+
params,
|
|
49
|
+
) as ResourceMap[RT];
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
// TODO: Not sure if we want to expose this...
|
|
51
53
|
getRepository<RT extends keyof RepositoryMap>(typeId: RT): RepositoryMap[RT] {
|
|
52
|
-
const repository = this._repositories[typeId]
|
|
54
|
+
const repository = this._repositories[typeId];
|
|
53
55
|
if (repository !== undefined) {
|
|
54
|
-
return repository as RepositoryMap[RT]
|
|
56
|
+
return repository as RepositoryMap[RT];
|
|
55
57
|
}
|
|
56
|
-
throw new Error(
|
|
58
|
+
throw new Error("No such repository");
|
|
57
59
|
}
|
|
58
60
|
}
|