@labdigital/commercetools-mock 1.4.0 → 1.6.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 +5 -4
- package/dist/index.cjs +116 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -7
- package/dist/index.d.ts +30 -7
- package/dist/index.js +116 -18
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/constants.ts +2 -2
- package/src/ctMock.ts +176 -176
- package/src/exceptions.ts +10 -10
- package/src/helpers.ts +26 -26
- package/src/index.test.ts +173 -173
- package/src/index.ts +3 -3
- package/src/lib/expandParser.ts +19 -19
- package/src/lib/haversine.test.ts +13 -13
- package/src/lib/haversine.ts +14 -14
- package/src/lib/masking.ts +15 -15
- package/src/lib/parser.ts +2 -2
- package/src/lib/predicateParser.test.ts +204 -204
- package/src/lib/predicateParser.ts +398 -398
- package/src/lib/projectionSearchFilter.test.ts +168 -168
- package/src/lib/projectionSearchFilter.ts +272 -269
- package/src/lib/proxy.ts +8 -8
- package/src/oauth/errors.ts +4 -4
- package/src/oauth/helpers.ts +6 -6
- package/src/oauth/server.ts +103 -101
- package/src/oauth/store.ts +27 -27
- package/src/priceSelector.test.ts +68 -68
- package/src/priceSelector.ts +70 -70
- package/src/product-projection-search.ts +296 -296
- package/src/projectAPI.test.ts +3 -3
- package/src/projectAPI.ts +46 -46
- package/src/repositories/abstract.ts +190 -190
- package/src/repositories/associate-role.ts +10 -7
- package/src/repositories/attribute-group.ts +63 -8
- package/src/repositories/business-unit.ts +10 -7
- package/src/repositories/cart-discount.ts +134 -134
- package/src/repositories/cart.ts +517 -514
- package/src/repositories/category.ts +170 -167
- package/src/repositories/channel.ts +114 -111
- package/src/repositories/custom-object.ts +66 -63
- package/src/repositories/customer-group.ts +72 -69
- package/src/repositories/customer.ts +93 -79
- package/src/repositories/discount-code.ts +171 -168
- package/src/repositories/errors.ts +15 -15
- package/src/repositories/extension.ts +79 -76
- package/src/repositories/helpers.ts +180 -180
- package/src/repositories/index.ts +39 -39
- package/src/repositories/inventory-entry.ts +98 -95
- package/src/repositories/my-order.ts +11 -11
- package/src/repositories/order-edit.ts +29 -29
- package/src/repositories/order.test.ts +191 -191
- package/src/repositories/order.ts +393 -389
- package/src/repositories/payment.ts +155 -155
- package/src/repositories/product-discount.ts +149 -149
- package/src/repositories/product-projection.ts +116 -52
- package/src/repositories/product-selection.ts +31 -31
- package/src/repositories/product-type.ts +156 -156
- package/src/repositories/product.ts +600 -597
- package/src/repositories/project.ts +136 -135
- package/src/repositories/quote-request.ts +19 -19
- package/src/repositories/quote.ts +19 -19
- package/src/repositories/review.ts +24 -24
- package/src/repositories/shipping-method.ts +217 -217
- package/src/repositories/shopping-list.ts +49 -49
- package/src/repositories/staged-quote.ts +20 -20
- package/src/repositories/standalone-price.ts +72 -61
- package/src/repositories/state.ts +84 -84
- package/src/repositories/store.ts +114 -114
- package/src/repositories/subscription.ts +40 -40
- package/src/repositories/tax-category.ts +98 -98
- package/src/repositories/type.ts +157 -157
- package/src/repositories/zone.ts +71 -71
- package/src/server.ts +2 -2
- package/src/services/abstract.ts +173 -173
- package/src/services/attribute-group.ts +16 -0
- package/src/services/cart-discount.ts +8 -8
- package/src/services/cart.test.ts +409 -409
- package/src/services/cart.ts +50 -50
- package/src/services/category.test.ts +25 -25
- package/src/services/category.ts +8 -8
- package/src/services/channel.ts +8 -8
- package/src/services/custom-object.test.ts +184 -184
- package/src/services/custom-object.ts +48 -48
- package/src/services/customer-group.ts +8 -8
- package/src/services/customer.test.ts +151 -129
- package/src/services/customer.ts +27 -27
- package/src/services/discount-code.ts +8 -8
- package/src/services/extension.ts +8 -8
- package/src/services/index.ts +52 -44
- package/src/services/inventory-entry.test.ts +162 -162
- package/src/services/inventory-entry.ts +8 -8
- package/src/services/my-cart.test.ts +78 -78
- package/src/services/my-cart.ts +28 -28
- package/src/services/my-customer.test.ts +44 -44
- package/src/services/my-customer.ts +53 -53
- package/src/services/my-order.ts +20 -20
- package/src/services/my-payment.test.ts +65 -65
- package/src/services/my-payment.ts +8 -8
- package/src/services/order.test.ts +527 -527
- package/src/services/order.ts +31 -31
- package/src/services/payment.test.ts +65 -65
- package/src/services/payment.ts +8 -8
- package/src/services/product-discount.ts +8 -8
- package/src/services/product-projection.test.ts +492 -428
- package/src/services/product-projection.ts +32 -18
- package/src/services/product-type.test.ts +56 -56
- package/src/services/product-type.ts +8 -8
- package/src/services/product.test.ts +510 -510
- package/src/services/product.ts +8 -8
- package/src/services/project.ts +34 -34
- package/src/services/shipping-method.test.ts +81 -81
- package/src/services/shipping-method.ts +12 -12
- package/src/services/shopping-list.ts +8 -8
- package/src/services/standalone-price.test.ts +256 -256
- package/src/services/standalone-price.ts +8 -8
- package/src/services/state.test.ts +42 -42
- package/src/services/state.ts +8 -8
- package/src/services/store.test.ts +57 -57
- package/src/services/store.ts +8 -8
- package/src/services/subscription.ts +8 -8
- package/src/services/tax-category.test.ts +61 -61
- package/src/services/tax-category.ts +8 -8
- package/src/services/type.ts +8 -8
- package/src/services/zone.ts +8 -8
- package/src/storage/abstract.ts +58 -58
- package/src/storage/in-memory.ts +419 -419
- package/src/types.ts +82 -82
|
@@ -1,341 +1,341 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
FacetResults,
|
|
3
|
+
FilteredFacetResult,
|
|
4
|
+
InvalidInputError,
|
|
5
|
+
Product,
|
|
6
|
+
ProductProjection,
|
|
7
|
+
ProductProjectionPagedSearchResponse,
|
|
8
|
+
QueryParam,
|
|
9
|
+
RangeFacetResult,
|
|
10
|
+
TermFacetResult,
|
|
11
11
|
} from '@commercetools/platform-sdk'
|
|
12
12
|
import { CommercetoolsError } from './exceptions.js'
|
|
13
13
|
import { nestedLookup } from './helpers.js'
|
|
14
14
|
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
FilterExpression,
|
|
16
|
+
RangeExpression,
|
|
17
|
+
generateFacetFunc,
|
|
18
|
+
getVariants,
|
|
19
|
+
parseFilterExpression,
|
|
20
|
+
resolveVariantValue,
|
|
21
21
|
} from './lib/projectionSearchFilter.js'
|
|
22
22
|
import { applyPriceSelector } from './priceSelector.js'
|
|
23
23
|
import { AbstractStorage } from './storage/index.js'
|
|
24
24
|
import type { Writable } from './types.js'
|
|
25
25
|
|
|
26
26
|
export type ProductProjectionSearchParams = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
47
|
}
|
|
48
48
|
|
|
49
49
|
export class ProductProjectionSearch {
|
|
50
|
-
|
|
50
|
+
protected _storage: AbstractStorage
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
constructor(storage: AbstractStorage) {
|
|
53
|
+
this._storage = storage
|
|
54
|
+
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
search(
|
|
57
|
+
projectKey: string,
|
|
58
|
+
params: ProductProjectionSearchParams
|
|
59
|
+
): ProductProjectionPagedSearchResponse {
|
|
60
|
+
let resources = this._storage
|
|
61
|
+
.all(projectKey, 'product')
|
|
62
|
+
.map((r) => this.transform(r, params.staged ?? false))
|
|
63
|
+
.filter((p) => {
|
|
64
|
+
if (!params.staged ?? false) {
|
|
65
|
+
return p.published
|
|
66
|
+
}
|
|
67
|
+
return true
|
|
68
|
+
})
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
const markMatchingVariant = params.markMatchingVariants ?? false
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
// Apply the priceSelector
|
|
73
|
+
applyPriceSelector(resources, {
|
|
74
|
+
country: params.priceCountry,
|
|
75
|
+
channel: params.priceChannel,
|
|
76
|
+
customerGroup: params.priceCustomerGroup,
|
|
77
|
+
currency: params.priceCurrency,
|
|
78
|
+
})
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
// Apply filters pre facetting
|
|
81
|
+
if (params.filter) {
|
|
82
|
+
try {
|
|
83
|
+
const filters = params.filter.map(parseFilterExpression)
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
85
|
+
// Filters can modify the output. So clone the resources first.
|
|
86
|
+
resources = resources.filter((resource) =>
|
|
87
|
+
filters.every((f) => f(resource, markMatchingVariant))
|
|
88
|
+
)
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error(err)
|
|
91
|
+
throw new CommercetoolsError<InvalidInputError>(
|
|
92
|
+
{
|
|
93
|
+
code: 'InvalidInput',
|
|
94
|
+
message: (err as any).message,
|
|
95
|
+
},
|
|
96
|
+
400
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
// TODO: Calculate facets
|
|
102
|
+
const facets = this.getFacets(params, resources)
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
104
|
+
// Apply filters post facetting
|
|
105
|
+
if (params['filter.query']) {
|
|
106
|
+
try {
|
|
107
|
+
const filters = params['filter.query'].map(parseFilterExpression)
|
|
108
|
+
resources = resources.filter((resource) =>
|
|
109
|
+
filters.every((f) => f(resource, markMatchingVariant))
|
|
110
|
+
)
|
|
111
|
+
} catch (err) {
|
|
112
|
+
throw new CommercetoolsError<InvalidInputError>(
|
|
113
|
+
{
|
|
114
|
+
code: 'InvalidInput',
|
|
115
|
+
message: (err as any).message,
|
|
116
|
+
},
|
|
117
|
+
400
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
121
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
122
|
+
// Expand the resources
|
|
123
|
+
if (params.expand !== undefined) {
|
|
124
|
+
resources = resources.map((resource) =>
|
|
125
|
+
this._storage.expand(projectKey, resource, params.expand)
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
// Create a slice for the pagination. If we were working with large datasets
|
|
130
|
+
// then we should have done this before transforming. But that isn't the
|
|
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)
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
return {
|
|
138
|
+
count: totalResults,
|
|
139
|
+
total: results.length,
|
|
140
|
+
offset: offset,
|
|
141
|
+
limit: limit,
|
|
142
|
+
results: results,
|
|
143
|
+
facets: facets,
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
transform(product: Product, staged: boolean): ProductProjection {
|
|
148
|
+
const obj = !staged ? product.masterData.current : product.masterData.staged
|
|
149
149
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
150
|
+
return {
|
|
151
|
+
id: product.id,
|
|
152
|
+
createdAt: product.createdAt,
|
|
153
|
+
lastModifiedAt: product.lastModifiedAt,
|
|
154
|
+
version: product.version,
|
|
155
|
+
name: obj.name,
|
|
156
|
+
key: product.key,
|
|
157
|
+
description: obj.description,
|
|
158
|
+
metaDescription: obj.metaDescription,
|
|
159
|
+
slug: obj.slug,
|
|
160
|
+
categories: obj.categories,
|
|
161
|
+
masterVariant: obj.masterVariant,
|
|
162
|
+
variants: obj.variants,
|
|
163
|
+
productType: product.productType,
|
|
164
|
+
hasStagedChanges: product.masterData.hasStagedChanges,
|
|
165
|
+
published: product.masterData.published,
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
168
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
169
|
+
getFacets(
|
|
170
|
+
params: ProductProjectionSearchParams,
|
|
171
|
+
products: ProductProjection[]
|
|
172
|
+
): FacetResults {
|
|
173
|
+
if (!params.facet) return {}
|
|
174
|
+
const staged = false
|
|
175
|
+
const result: FacetResults = {}
|
|
176
176
|
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
for (const facet of params.facet) {
|
|
178
|
+
const expression = generateFacetFunc(facet)
|
|
179
179
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
180
|
+
// Term Facet
|
|
181
|
+
if (expression.type === 'TermExpression') {
|
|
182
|
+
result[facet] = this.termFacet(expression.source, products)
|
|
183
|
+
}
|
|
184
184
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
185
|
+
// Range Facet
|
|
186
|
+
if (expression.type === 'RangeExpression') {
|
|
187
|
+
result[expression.source] = this.rangeFacet(
|
|
188
|
+
expression.source,
|
|
189
|
+
expression.children,
|
|
190
|
+
products
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
193
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
194
|
+
// FilteredFacet
|
|
195
|
+
if (expression.type === 'FilterExpression') {
|
|
196
|
+
result[expression.source] = this.filterFacet(
|
|
197
|
+
expression.source,
|
|
198
|
+
expression.children,
|
|
199
|
+
products
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
203
|
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
return result
|
|
205
|
+
}
|
|
206
206
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
207
|
+
/**
|
|
208
|
+
* TODO: This implemention needs the following additional features:
|
|
209
|
+
* - counting products
|
|
210
|
+
* - correct dataType
|
|
211
|
+
*/
|
|
212
|
+
termFacet(facet: string, products: ProductProjection[]): TermFacetResult {
|
|
213
|
+
const result: Writable<TermFacetResult> = {
|
|
214
|
+
type: 'terms',
|
|
215
|
+
dataType: 'text',
|
|
216
|
+
missing: 0,
|
|
217
|
+
total: 0,
|
|
218
|
+
other: 0,
|
|
219
|
+
terms: [],
|
|
220
|
+
}
|
|
221
|
+
const terms: Record<any, number> = {}
|
|
222
222
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
223
|
+
if (facet.startsWith('variants.')) {
|
|
224
|
+
products.forEach((p) => {
|
|
225
|
+
const variants = getVariants(p)
|
|
226
|
+
variants.forEach((v) => {
|
|
227
|
+
result.total++
|
|
228
228
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
229
|
+
let value = resolveVariantValue(v, facet)
|
|
230
|
+
if (value === undefined) {
|
|
231
|
+
result.missing++
|
|
232
|
+
} else {
|
|
233
|
+
if (typeof value === 'number') {
|
|
234
|
+
value = Number(value).toFixed(1)
|
|
235
|
+
}
|
|
236
|
+
terms[value] = value in terms ? terms[value] + 1 : 1
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
} else {
|
|
241
|
+
products.forEach((p) => {
|
|
242
|
+
const value = nestedLookup(p, facet)
|
|
243
|
+
result.total++
|
|
244
|
+
if (value === undefined) {
|
|
245
|
+
result.missing++
|
|
246
|
+
} else {
|
|
247
|
+
terms[value] = value in terms ? terms[value] + 1 : 1
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
for (const term in terms) {
|
|
252
|
+
result.terms.push({
|
|
253
|
+
term: term as any,
|
|
254
|
+
count: terms[term],
|
|
255
|
+
})
|
|
256
|
+
}
|
|
257
|
+
return result
|
|
258
|
+
}
|
|
259
259
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
260
|
+
filterFacet(
|
|
261
|
+
source: string,
|
|
262
|
+
filters: FilterExpression[] | undefined,
|
|
263
|
+
products: ProductProjection[]
|
|
264
|
+
): FilteredFacetResult {
|
|
265
|
+
let count = 0
|
|
266
|
+
if (source.startsWith('variants.')) {
|
|
267
|
+
for (const p of products) {
|
|
268
|
+
for (const v of getVariants(p)) {
|
|
269
|
+
const val = resolveVariantValue(v, source)
|
|
270
|
+
if (filters?.some((f) => f.match(val))) {
|
|
271
|
+
count++
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
throw new Error('not supported')
|
|
277
|
+
}
|
|
278
278
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
279
|
+
return {
|
|
280
|
+
type: 'filter',
|
|
281
|
+
count: count,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
284
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
285
|
+
rangeFacet(
|
|
286
|
+
source: string,
|
|
287
|
+
ranges: RangeExpression[] | undefined,
|
|
288
|
+
products: ProductProjection[]
|
|
289
|
+
): RangeFacetResult {
|
|
290
|
+
const counts =
|
|
291
|
+
ranges?.map((range) => {
|
|
292
|
+
if (source.startsWith('variants.')) {
|
|
293
|
+
const values = []
|
|
294
|
+
for (const p of products) {
|
|
295
|
+
for (const v of getVariants(p)) {
|
|
296
|
+
const val = resolveVariantValue(v, source)
|
|
297
|
+
if (val === undefined) {
|
|
298
|
+
continue
|
|
299
|
+
}
|
|
300
300
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
301
|
+
if (range.match(val)) {
|
|
302
|
+
values.push(val)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
306
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
307
|
+
const numValues = values.length
|
|
308
|
+
return {
|
|
309
|
+
type: 'double',
|
|
310
|
+
from: range.start || 0,
|
|
311
|
+
fromStr: range.start !== null ? Number(range.start).toFixed(1) : '',
|
|
312
|
+
to: range.stop || 0,
|
|
313
|
+
toStr: range.stop !== null ? Number(range.stop).toFixed(1) : '',
|
|
314
|
+
count: numValues,
|
|
315
|
+
// totalCount: 0,
|
|
316
|
+
total: values.reduce((a, b) => a + b, 0),
|
|
317
|
+
min: numValues > 0 ? Math.min(...values) : 0,
|
|
318
|
+
max: numValues > 0 ? Math.max(...values) : 0,
|
|
319
|
+
mean: numValues > 0 ? mean(values) : 0,
|
|
320
|
+
}
|
|
321
|
+
} else {
|
|
322
|
+
throw new Error('not supported')
|
|
323
|
+
}
|
|
324
|
+
}) || []
|
|
325
|
+
const data: RangeFacetResult = {
|
|
326
|
+
type: 'range',
|
|
327
|
+
// @ts-ignore
|
|
328
|
+
dataType: 'number',
|
|
329
|
+
ranges: counts,
|
|
330
|
+
}
|
|
331
|
+
return data
|
|
332
|
+
}
|
|
333
333
|
}
|
|
334
334
|
|
|
335
335
|
const mean = (arr: number[]) => {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
336
|
+
let total = 0
|
|
337
|
+
for (let i = 0; i < arr.length; i++) {
|
|
338
|
+
total += arr[i]
|
|
339
|
+
}
|
|
340
|
+
return total / arr.length
|
|
341
341
|
}
|
package/src/projectAPI.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { CommercetoolsMock } from './index.js'
|
|
|
2
2
|
import { test } from 'vitest'
|
|
3
3
|
|
|
4
4
|
test('getRepository', async () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const ctMock = new CommercetoolsMock()
|
|
6
|
+
const repo = ctMock.project('my-project-key').getRepository('order')
|
|
7
|
+
repo.get({ projectKey: 'unittest' }, '1234')
|
|
8
8
|
})
|