|
1
|
import CustomerModule from "@medusajs/customer" |
|
2
|
import ProductModule from "@medusajs/product" |
|
3
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { RemoteQueryFunction } from "@medusajs/types" |
|
5
|
import { |
|
6
|
ContainerRegistrationKeys, |
|
7
|
defaultCurrencies, |
|
8
|
defineLink, |
|
9
|
Modules, |
|
10
|
promiseAll, |
|
11
|
} from "@medusajs/utils" |
|
12
|
import { |
|
13
|
adminHeaders, |
|
14
|
createAdminUser, |
|
15
|
} from "../../../helpers/create-admin-user" |
|
16
|
import { fetchAndRetry } from "../../../helpers/retry" |
|
17
|
import { waitForIndexedEntities } from "../../../helpers/wait-for-index" |
|
18
|
|
|
19
|
jest.setTimeout(300000) |
|
20
|
|
|
21
|
// NOTE: In this tests, both API are used to query, we use object pattern and string pattern |
|
22
|
|
|
23
|
async function populateData(api: any) { |
|
24
|
const shippingProfile = ( |
|
25
|
await api.post( |
|
26
|
`/admin/shipping-profiles`, |
|
27
|
{ name: "Test", type: "default" }, |
|
28
|
adminHeaders |
|
29
|
) |
|
30
|
).data.shipping_profile |
|
31
|
|
|
32
|
const payload = [ |
|
33
|
{ |
|
34
|
title: "Test Product", |
|
35
|
status: "published", |
|
36
|
description: "test-product-description", |
|
37
|
origin_country: "USA", |
|
38
|
shipping_profile_id: shippingProfile.id, |
|
39
|
options: [{ title: "Denominations", values: ["100"] }], |
|
40
|
material: "test-material", |
|
41
|
variants: [ |
|
42
|
{ |
|
43
|
title: `Test variant 1`, |
|
44
|
sku: `test-variant-1`, |
|
45
|
prices: [ |
|
46
|
{ |
|
47
|
currency_code: Object.values(defaultCurrencies)[0].code, |
|
48
|
amount: 30, |
|
49
|
}, |
|
50
|
{ |
|
51
|
currency_code: Object.values(defaultCurrencies)[2].code, |
|
52
|
amount: 50, |
|
53
|
}, |
|
54
|
], |
|
55
|
options: { |
|
56
|
Denominations: "100", |
|
57
|
}, |
|
58
|
}, |
|
59
|
], |
|
60
|
}, |
|
61
|
{ |
|
62
|
title: "Extra product", |
|
63
|
description: "extra description", |
|
64
|
status: "published", |
|
65
|
shipping_profile_id: shippingProfile.id, |
|
66
|
options: [{ title: "Colors", values: ["Red"] }], |
|
67
|
material: "extra-material", |
|
68
|
variants: new Array(2).fill(0).map((_, i) => ({ |
|
69
|
title: `extra variant ${i}`, |
|
70
|
sku: `extra-variant-${i}`, |
|
71
|
prices: [ |
|
72
|
{ |
|
73
|
currency_code: Object.values(defaultCurrencies)[1].code, |
|
74
|
amount: 20, |
|
75
|
}, |
|
76
|
{ |
|
77
|
currency_code: Object.values(defaultCurrencies)[0].code, |
|
78
|
amount: 80, |
|
79
|
}, |
|
80
|
], |
|
81
|
options: { |
|
82
|
Colors: "Red", |
|
83
|
}, |
|
84
|
})), |
|
85
|
}, |
|
86
|
] |
|
87
|
|
|
88
|
const response = await api.post( |
|
89
|
"/admin/products/batch", |
|
90
|
{ create: payload }, |
|
91
|
adminHeaders |
|
92
|
) |
|
93
|
const products = response.data.created |
|
94
|
|
|
95
|
return products |
|
96
|
} |
|
97
|
|
|
98
|
process.env.ENABLE_INDEX_MODULE = "true" |
|
99
|
|
|
100
|
medusaIntegrationTestRunner({ |
|
101
|
hooks: { |
|
102
|
beforeServerStart: async () => { |
|
103
|
const customer = CustomerModule.linkable.customer |
|
104
|
const product = ProductModule.linkable.product |
|
105
|
|
|
106
|
defineLink(customer, { |
|
107
|
linkable: product, |
|
108
|
filterable: ["origin_country"], |
|
109
|
}) |
|
110
|
}, |
|
111
|
}, |
|
112
|
testSuite: ({ getContainer, dbConnection, api, dbUtils, utils }) => { |
|
113
|
let appContainer |
|
114
|
|
|
115
|
describe("Index engine - Query.index", () => { |
|
116
|
beforeAll(async () => { |
|
117
|
appContainer = getContainer() |
|
118
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
119
|
await dbUtils.snapshot() |
|
120
|
}) |
|
121
|
|
|
122
|
afterAll(() => { |
|
123
|
process.env.ENABLE_INDEX_MODULE = "false" |
|
124
|
}) |
|
125
|
|
|
126
|
it("should use query.index to query the index module and hydrate the data", async () => { |
|
127
|
const products = await populateData(api) |
|
128
|
|
|
129
|
const brandModule = appContainer.resolve("brand") |
|
130
|
const link = appContainer.resolve(ContainerRegistrationKeys.LINK) |
|
131
|
const brand = await brandModule.createBrands({ |
|
132
|
name: "Medusa Brand", |
|
133
|
}) |
|
134
|
|
|
135
|
const [createdLink] = await link.create({ |
|
136
|
[Modules.PRODUCT]: { |
|
137
|
product_id: products.find((p) => p.title === "Extra product").id, |
|
138
|
}, |
|
139
|
brand: { |
|
140
|
brand_id: brand.id, |
|
141
|
}, |
|
142
|
}) |
|
143
|
|
|
144
|
const query = appContainer.resolve( |
|
145
|
ContainerRegistrationKeys.QUERY |
|
146
|
) as RemoteQueryFunction |
|
147
|
|
|
148
|
await promiseAll([ |
|
149
|
waitForIndexedEntities( |
|
150
|
dbConnection, |
|
151
|
"Product", |
|
152
|
products.map((p) => p.id) |
|
153
|
), |
|
154
|
waitForIndexedEntities( |
|
155
|
dbConnection, |
|
156
|
"ProductVariant", |
|
157
|
products.flatMap((p) => p.variants.map((v) => v.id)) |
|
158
|
), |
|
159
|
waitForIndexedEntities( |
|
160
|
dbConnection, |
|
161
|
"Price", |
|
162
|
products.flatMap((p) => |
|
163
|
p.variants.flatMap((v) => v.prices.map((p) => p.id)) |
|
164
|
) |
|
165
|
), |
|
166
|
waitForIndexedEntities(dbConnection, "Brand", [brand.id]), |
|
167
|
waitForIndexedEntities( |
|
168
|
dbConnection, |
|
169
|
"ProductProductBrandBrand", |
|
170
|
[createdLink.id], |
|
171
|
{ |
|
172
|
isLink: true, |
|
173
|
} |
|
174
|
), |
|
175
|
]) |
|
176
|
|
|
177
|
const resultset = await fetchAndRetry( |
|
178
|
async () => |
|
179
|
await query.index({ |
|
180
|
entity: "product", |
|
181
|
fields: [ |
|
182
|
"id", |
|
183
|
"description", |
|
184
|
"status", |
|
185
|
"title", |
|
186
|
"brand.name", |
|
187
|
"brand.id", |
|
188
|
"variants.sku", |
|
189
|
"variants.barcode", |
|
190
|
"variants.material", |
|
191
|
"variants.options.value", |
|
192
|
"variants.prices.amount", |
|
193
|
"variants.prices.currency_code", |
|
194
|
"variants.inventory_items.inventory.sku", |
|
195
|
"variants.inventory_items.inventory.description", |
|
196
|
], |
|
197
|
filters: { |
|
198
|
$and: [ |
|
199
|
{ status: "published" }, |
|
200
|
{ material: { $ilike: "%material%" } }, |
|
201
|
{ |
|
202
|
$or: [ |
|
203
|
{ |
|
204
|
brand: { |
|
205
|
name: { $ilike: "%brand" }, |
|
206
|
}, |
|
207
|
}, |
|
208
|
{ title: { $ilike: "%duct%" } }, |
|
209
|
], |
|
210
|
}, |
|
211
|
{ |
|
212
|
variants: { |
|
213
|
$and: [ |
|
214
|
{ sku: { $like: "%-1" } }, |
|
215
|
{ "prices.amount": { $gt: 30 } }, |
|
216
|
], |
|
217
|
}, |
|
218
|
}, |
|
219
|
], |
|
220
|
}, |
|
221
|
pagination: { |
|
222
|
take: 10, |
|
223
|
skip: 0, |
|
224
|
order: { |
|
225
|
"variants.prices.amount": "DESC", |
|
226
|
}, |
|
227
|
}, |
|
228
|
}), |
|
229
|
({ data }) => data.length > 0 |
|
230
|
) |
|
231
|
|
|
232
|
expect(resultset.metadata).toEqual({ |
|
233
|
estimate_count: expect.any(Number), |
|
234
|
skip: 0, |
|
235
|
take: 10, |
|
236
|
}) |
|
237
|
|
|
238
|
// Pagination ordering only refers to the root queried entity(product) |
|
239
|
// Ordering is dropped for hydrated entities (variants, prices, etc) |
|
240
|
for (const product of resultset.data) { |
|
241
|
product.variants?.sort((a, b) => a.sku.localeCompare(b.sku)) |
|
242
|
} |
|
243
|
|
|
244
|
expect(resultset.data).toEqual([ |
|
245
|
{ |
|
246
|
id: expect.any(String), |
|
247
|
description: "extra description", |
|
248
|
title: "Extra product", |
|
249
|
status: "published", |
|
250
|
brand: { |
|
251
|
id: expect.any(String), |
|
252
|
name: "Medusa Brand", |
|
253
|
}, |
|
254
|
variants: [ |
|
255
|
{ |
|
256
|
sku: "extra-variant-0", |
|
257
|
barcode: null, |
|
258
|
material: null, |
|
259
|
id: expect.any(String), |
|
260
|
options: [ |
|
261
|
{ |
|
262
|
value: "Red", |
|
263
|
}, |
|
264
|
], |
|
265
|
inventory_items: [ |
|
266
|
{ |
|
267
|
variant_id: expect.any(String), |
|
268
|
inventory_item_id: expect.any(String), |
|
269
|
inventory: { |
|
270
|
sku: "extra-variant-0", |
|
271
|
description: "extra variant 0", |
|
272
|
id: expect.any(String), |
|
273
|
}, |
|
274
|
}, |
|
275
|
], |
|
276
|
prices: expect.arrayContaining([ |
|
277
|
{ |
|
278
|
currency_code: "cad", |
|
279
|
amount: 20, |
|
280
|
id: expect.any(String), |
|
281
|
}, |
|
282
|
{ |
|
283
|
currency_code: "usd", |
|
284
|
amount: 80, |
|
285
|
id: expect.any(String), |
|
286
|
}, |
|
287
|
]), |
|
288
|
}, |
|
289
|
{ |
|
290
|
sku: "extra-variant-1", |
|
291
|
barcode: null, |
|
292
|
material: null, |
|
293
|
id: expect.any(String), |
|
294
|
options: [ |
|
295
|
{ |
|
296
|
value: "Red", |
|
297
|
}, |
|
298
|
], |
|
299
|
prices: expect.arrayContaining([ |
|
300
|
{ |
|
301
|
amount: 20, |
|
302
|
currency_code: "cad", |
|
303
|
id: expect.any(String), |
|
304
|
}, |
|
305
|
{ |
|
306
|
amount: 80, |
|
307
|
currency_code: "usd", |
|
308
|
id: expect.any(String), |
|
309
|
}, |
|
310
|
]), |
|
311
|
inventory_items: [ |
|
312
|
{ |
|
313
|
variant_id: expect.any(String), |
|
314
|
inventory_item_id: expect.any(String), |
|
315
|
inventory: { |
|
316
|
sku: "extra-variant-1", |
|
317
|
description: "extra variant 1", |
|
318
|
id: expect.any(String), |
|
319
|
}, |
|
320
|
}, |
|
321
|
], |
|
322
|
}, |
|
323
|
], |
|
324
|
}, |
|
325
|
{ |
|
326
|
id: expect.any(String), |
|
327
|
description: "test-product-description", |
|
328
|
title: "Test Product", |
|
329
|
status: "published", |
|
330
|
brand: undefined, |
|
331
|
variants: [ |
|
332
|
{ |
|
333
|
sku: "test-variant-1", |
|
334
|
barcode: null, |
|
335
|
material: null, |
|
336
|
id: expect.any(String), |
|
337
|
options: [ |
|
338
|
{ |
|
339
|
value: "100", |
|
340
|
}, |
|
341
|
], |
|
342
|
prices: expect.arrayContaining([ |
|
343
|
{ |
|
344
|
amount: 30, |
|
345
|
currency_code: "usd", |
|
346
|
id: expect.any(String), |
|
347
|
}, |
|
348
|
{ |
|
349
|
amount: 50, |
|
350
|
currency_code: "eur", |
|
351
|
id: expect.any(String), |
|
352
|
}, |
|
353
|
]), |
|
354
|
inventory_items: [ |
|
355
|
{ |
|
356
|
variant_id: expect.any(String), |
|
357
|
inventory_item_id: expect.any(String), |
|
358
|
inventory: { |
|
359
|
sku: "test-variant-1", |
|
360
|
description: "Test variant 1", |
|
361
|
id: expect.any(String), |
|
362
|
}, |
|
363
|
}, |
|
364
|
], |
|
365
|
}, |
|
366
|
], |
|
367
|
}, |
|
368
|
]) |
|
369
|
}) |
|
370
|
|
|
371
|
it("should use query.index to query the index module sorting by price desc", async () => { |
|
372
|
const products = await populateData(api) |
|
373
|
|
|
374
|
const query = appContainer.resolve( |
|
375
|
ContainerRegistrationKeys.QUERY |
|
376
|
) as RemoteQueryFunction |
|
377
|
|
|
378
|
await promiseAll([ |
|
379
|
waitForIndexedEntities( |
|
380
|
dbConnection, |
|
381
|
"Product", |
|
382
|
products.map((p) => p.id) |
|
383
|
), |
|
384
|
waitForIndexedEntities( |
|
385
|
dbConnection, |
|
386
|
"ProductVariant", |
|
387
|
products.flatMap((p) => p.variants.map((v) => v.id)) |
|
388
|
), |
|
389
|
waitForIndexedEntities( |
|
390
|
dbConnection, |
|
391
|
"Price", |
|
392
|
products.flatMap((p) => |
|
393
|
p.variants.flatMap((v) => v.prices.map((p) => p.id)) |
|
394
|
) |
|
395
|
), |
|
396
|
]) |
|
397
|
|
|
398
|
const resultset = await fetchAndRetry( |
|
399
|
async () => |
|
400
|
await query.index({ |
|
401
|
entity: "product", |
|
402
|
fields: [ |
|
403
|
"id", |
|
404
|
"variants.prices.amount", |
|
405
|
"variants.prices.currency_code", |
|
406
|
], |
|
407
|
filters: { |
|
408
|
"variants.prices.currency_code": "usd", |
|
409
|
}, |
|
410
|
pagination: { |
|
411
|
take: 1, |
|
412
|
skip: 0, |
|
413
|
order: { |
|
414
|
"variants.prices.amount": "DESC", |
|
415
|
}, |
|
416
|
}, |
|
417
|
}), |
|
418
|
({ data }) => data.length > 0 |
|
419
|
) |
|
420
|
|
|
421
|
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained |
|
422
|
expect(resultset.data).toEqual([ |
|
423
|
{ |
|
424
|
id: expect.any(String), |
|
425
|
variants: expect.arrayContaining([ |
|
426
|
expect.objectContaining({ |
|
427
|
prices: expect.arrayContaining([ |
|
428
|
{ |
|
429
|
amount: 20, |
|
430
|
currency_code: "cad", |
|
431
|
id: expect.any(String), |
|
432
|
}, |
|
433
|
{ |
|
434
|
amount: 80, |
|
435
|
currency_code: "usd", |
|
436
|
id: expect.any(String), |
|
437
|
}, |
|
438
|
]), |
|
439
|
}), |
|
440
|
]), |
|
441
|
}, |
|
442
|
]) |
|
443
|
|
|
444
|
const resultset2 = await fetchAndRetry( |
|
445
|
async () => |
|
446
|
query.index({ |
|
447
|
entity: "product", |
|
448
|
fields: [ |
|
449
|
"id", |
|
450
|
"variants.prices.amount", |
|
451
|
"variants.prices.currency_code", |
|
452
|
], |
|
453
|
filters: { |
|
454
|
variants: { |
|
455
|
prices: { |
|
456
|
currency_code: "usd", |
|
457
|
}, |
|
458
|
}, |
|
459
|
}, |
|
460
|
pagination: { |
|
461
|
take: 1, |
|
462
|
skip: 0, |
|
463
|
order: { |
|
464
|
variants: { |
|
465
|
prices: { |
|
466
|
amount: "ASC", |
|
467
|
}, |
|
468
|
}, |
|
469
|
}, |
|
470
|
}, |
|
471
|
}), |
|
472
|
({ data }) => data.length > 0 |
|
473
|
) |
|
474
|
|
|
475
|
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained |
|
476
|
expect(resultset2.data).toEqual([ |
|
477
|
{ |
|
478
|
id: expect.any(String), |
|
479
|
variants: [ |
|
480
|
expect.objectContaining({ |
|
481
|
prices: expect.arrayContaining([ |
|
482
|
{ |
|
483
|
amount: 30, |
|
484
|
currency_code: "usd", |
|
485
|
id: expect.any(String), |
|
486
|
}, |
|
487
|
{ |
|
488
|
amount: 50, |
|
489
|
currency_code: "eur", |
|
490
|
id: expect.any(String), |
|
491
|
}, |
|
492
|
]), |
|
493
|
}), |
|
494
|
], |
|
495
|
}, |
|
496
|
]) |
|
497
|
}) |
|
498
|
|
|
499
|
it("should use query.index to get products by an array of handles", async () => { |
|
500
|
const products = await populateData(api) |
|
501
|
|
|
502
|
await promiseAll([ |
|
503
|
waitForIndexedEntities( |
|
504
|
dbConnection, |
|
505
|
"Product", |
|
506
|
products.map((p) => p.id) |
|
507
|
), |
|
508
|
waitForIndexedEntities( |
|
509
|
dbConnection, |
|
510
|
"ProductVariant", |
|
511
|
products.flatMap((p) => p.variants.map((v) => v.id)) |
|
512
|
), |
|
513
|
waitForIndexedEntities( |
|
514
|
dbConnection, |
|
515
|
"Price", |
|
516
|
products.flatMap((p) => |
|
517
|
p.variants.flatMap((v) => v.prices.map((p) => p.id)) |
|
518
|
) |
|
519
|
), |
|
520
|
]) |
|
521
|
|
|
522
|
const query = appContainer.resolve( |
|
523
|
ContainerRegistrationKeys.QUERY |
|
524
|
) as RemoteQueryFunction |
|
525
|
|
|
526
|
const resultset = await fetchAndRetry( |
|
527
|
async () => |
|
528
|
await query.index({ |
|
529
|
entity: "product", |
|
530
|
fields: ["id"], |
|
531
|
filters: { |
|
532
|
handle: ["extra-product", "test-product"], |
|
533
|
}, |
|
534
|
pagination: { |
|
535
|
take: 10, |
|
536
|
skip: 0, |
|
537
|
order: { |
|
538
|
"variants.prices.amount": "DESC", |
|
539
|
}, |
|
540
|
}, |
|
541
|
}), |
|
542
|
({ data }) => data.length > 0 |
|
543
|
) |
|
544
|
|
|
545
|
expect(resultset.data.length).toEqual(2) |
|
546
|
}) |
|
547
|
|
|
548
|
it("should query by custom linkable field and default field using query.index", async () => { |
|
549
|
const products = await populateData(api) |
|
550
|
|
|
551
|
await promiseAll([ |
|
552
|
waitForIndexedEntities( |
|
553
|
dbConnection, |
|
554
|
"Product", |
|
555
|
products.map((p) => p.id) |
|
556
|
), |
|
557
|
waitForIndexedEntities( |
|
558
|
dbConnection, |
|
559
|
"ProductVariant", |
|
560
|
products.flatMap((p) => p.variants.map((v) => v.id)) |
|
561
|
), |
|
562
|
waitForIndexedEntities( |
|
563
|
dbConnection, |
|
564
|
"Price", |
|
565
|
products.flatMap((p) => |
|
566
|
p.variants.flatMap((v) => v.prices.map((p) => p.id)) |
|
567
|
) |
|
568
|
), |
|
569
|
]) |
|
570
|
|
|
571
|
const query = appContainer.resolve( |
|
572
|
ContainerRegistrationKeys.QUERY |
|
573
|
) as RemoteQueryFunction |
|
574
|
|
|
575
|
const resultset = await fetchAndRetry( |
|
576
|
async () => |
|
577
|
await query.index({ |
|
578
|
entity: "product", |
|
579
|
fields: ["id", "origin_country"], |
|
580
|
filters: { |
|
581
|
origin_country: ["USA"], |
|
582
|
}, |
|
583
|
}), |
|
584
|
({ data }) => data.length > 0 |
|
585
|
) |
|
586
|
|
|
587
|
expect(resultset.data.length).toEqual(1) |
|
588
|
expect(resultset.data[0].origin_country).toEqual("USA") |
|
589
|
}) |
|
590
|
|
|
591
|
it("should use query.index to filter enum field", async () => { |
|
592
|
const products = await populateData(api) |
|
593
|
|
|
594
|
const brandModule = appContainer.resolve("brand") |
|
595
|
const link = appContainer.resolve(ContainerRegistrationKeys.LINK) |
|
596
|
const brand = await brandModule.createBrands({ |
|
597
|
name: "Medusa Brand", |
|
598
|
}) |
|
599
|
|
|
600
|
const [createdLink] = await link.create({ |
|
601
|
[Modules.PRODUCT]: { |
|
602
|
product_id: products.find((p) => p.title === "Extra product").id, |
|
603
|
}, |
|
604
|
brand: { |
|
605
|
brand_id: brand.id, |
|
606
|
}, |
|
607
|
}) |
|
608
|
|
|
609
|
const query = appContainer.resolve( |
|
610
|
ContainerRegistrationKeys.QUERY |
|
611
|
) as RemoteQueryFunction |
|
612
|
|
|
613
|
await promiseAll([ |
|
614
|
waitForIndexedEntities( |
|
615
|
dbConnection, |
|
616
|
"Product", |
|
617
|
products.map((p) => p.id) |
|
618
|
), |
|
619
|
waitForIndexedEntities( |
|
620
|
dbConnection, |
|
621
|
"ProductVariant", |
|
622
|
products.flatMap((p) => p.variants.map((v) => v.id)) |
|
623
|
), |
|
624
|
waitForIndexedEntities( |
|
625
|
dbConnection, |
|
626
|
"Price", |
|
627
|
products.flatMap((p) => |
|
628
|
p.variants.flatMap((v) => v.prices.map((p) => p.id)) |
|
629
|
) |
|
630
|
), |
|
631
|
waitForIndexedEntities(dbConnection, "Brand", [brand.id]), |
|
632
|
waitForIndexedEntities( |
|
633
|
dbConnection, |
|
634
|
"ProductProductBrandBrand", |
|
635
|
[createdLink.id], |
|
636
|
{ |
|
637
|
isLink: true, |
|
638
|
} |
|
639
|
), |
|
640
|
]) |
|
641
|
|
|
642
|
const resultset = await fetchAndRetry( |
|
643
|
async () => |
|
644
|
await query.index({ |
|
645
|
entity: "product", |
|
646
|
fields: ["id"], |
|
647
|
filters: { |
|
648
|
status: "published", |
|
649
|
brand: { |
|
650
|
status: "active", |
|
651
|
}, |
|
652
|
}, |
|
653
|
}), |
|
654
|
({ data }) => data.length > 0, |
|
655
|
{ |
|
656
|
retries: 5, |
|
657
|
waitSeconds: 1.5, |
|
658
|
} |
|
659
|
) |
|
660
|
|
|
661
|
expect(resultset.data.length).toEqual(1) |
|
662
|
}) |
|
663
|
}) |
|
664
|
}, |
|
665
|
}) |