1 import { EventBusTypes, InferEntityType } from "@medusajs/framework/types"
2 import { toMikroORMEntity } from "@medusajs/framework/utils"
3 import { IndexData, IndexRelation } from "@models"
4 import {
5 createIndexTestBed,
6 IndexModuleTestBed,
7 schema,
8 } from "../__fixtures__"
9
10 jest.setTimeout(300000)
11
12 const productId = "prod_1"
13 const productId2 = "prod_2"
14 const variantId = "var_1"
15 const variantId2 = "var_2"
16 const priceSetId = "price_set_1"
17 const priceId = "money_amount_1"
18 const linkId = "link_id_1"
19
20 const seedQueryData = (testBed: IndexModuleTestBed) => {
21 testBed.query
22 .seed("product", [
23 { id: productId, title: "Test Product 1" },
24 { id: productId2, title: "Test Product 2" },
25 ])
26 .seed("product_variant", [
27 {
28 id: variantId,
29 sku: "aaa test aaa",
30 product: { id: productId },
31 },
32 {
33 id: variantId2,
34 sku: "sku 123",
35 product: { id: productId2 },
36 },
37 ])
38 .seed("price_set", [{ id: priceSetId }])
39 .seed("price", [
40 {
41 id: priceId,
42 amount: 100,
43 price_set: [{ id: priceSetId }],
44 },
45 ])
46 .seed("product_variant_price_set", [
47 {
48 id: linkId,
49 variant_id: variantId,
50 price_set_id: priceSetId,
51 variant: [{ id: variantId }],
52 },
53 ])
54 }
55
56 const findIndexEntries = async (testBed: IndexModuleTestBed) => {
57 const manager = testBed.forkManager()
58
59 const indexEntries: InferEntityType<typeof IndexData>[] = await manager.find(
60 toMikroORMEntity(IndexData),
61 {}
62 )
63 const indexRelationEntries: InferEntityType<typeof IndexRelation>[] =
64 await manager.find(toMikroORMEntity(IndexRelation), {})
65
66 return { indexEntries, indexRelationEntries }
67 }
68
69 describe("IndexModuleService", function () {
70 let testBed: IndexModuleTestBed
71
72 beforeAll(async () => {
73 testBed = await createIndexTestBed({ schema })
74 })
75
76 afterAll(async () => {
77 await testBed.teardown()
78 })
79
80 beforeEach(async () => {
81 await testBed.truncateTables()
82 testBed.query.reset()
83 seedQueryData(testBed)
84 })
85
86 describe("on created or attached events", function () {
87 const eventDataToEmit: EventBusTypes.Event[] = [
88 {
89 name: "product.created",
90 data: {
91 id: productId,
92 },
93 },
94 {
95 name: "product.created",
96 data: {
97 id: productId2,
98 },
99 },
100 {
101 name: "variant.created",
102 data: {
103 id: variantId,
104 product: {
105 id: productId,
106 },
107 },
108 },
109 {
110 name: "pricing.price-set.created",
111 data: {
112 id: priceSetId,
113 },
114 },
115 {
116 name: "price.created",
117 data: {
118 id: priceId,
119 price_set: {
120 id: priceSetId,
121 },
122 },
123 },
124 {
125 name: "LinkProductVariantPriceSet.attached",
126 data: {
127 id: linkId,
128 variant_id: variantId,
129 price_set_id: priceSetId,
130 },
131 },
132 ]
133
134 it("should create the corresponding index entries and index relation entries", async function () {
135 await testBed.eventBus.emit(eventDataToEmit)
136
137 const { indexEntries, indexRelationEntries } = await findIndexEntries(
138 testBed
139 )
140
141 const productIndexEntries = indexEntries.filter((entry) => {
142 return entry.name === "Product"
143 })
144
145 expect(productIndexEntries).toHaveLength(2)
146
147 const variantIndexEntries = indexEntries.filter((entry) => {
148 return entry.name === "ProductVariant"
149 })
150
151 expect(variantIndexEntries).toHaveLength(1)
152
153 const priceSetIndexEntries = indexEntries.filter((entry) => {
154 return entry.name === "PriceSet"
155 })
156
157 expect(priceSetIndexEntries).toHaveLength(1)
158
159 const priceIndexEntries = indexEntries.filter((entry) => {
160 return entry.name === "Price"
161 })
162
163 expect(priceIndexEntries).toHaveLength(1)
164
165 const linkIndexEntries = indexEntries.filter((entry) => {
166 return entry.name === "LinkProductVariantPriceSet"
167 })
168
169 expect(linkIndexEntries).toHaveLength(1)
170
171 expect(indexRelationEntries).toHaveLength(4)
172
173 const productVariantIndexRelationEntries = indexRelationEntries.filter(
174 (entry) => {
175 return (
176 entry.parent_id === productId &&
177 entry.parent_name === "Product" &&
178 entry.child_id === variantId &&
179 entry.child_name === "ProductVariant"
180 )
181 }
182 )
183
184 expect(productVariantIndexRelationEntries).toHaveLength(1)
185
186 const variantLinkIndexRelationEntries = indexRelationEntries.filter(
187 (entry) => {
188 return (
189 entry.parent_id === variantId &&
190 entry.parent_name === "ProductVariant" &&
191 entry.child_id === linkId &&
192 entry.child_name === "LinkProductVariantPriceSet"
193 )
194 }
195 )
196
197 expect(variantLinkIndexRelationEntries).toHaveLength(1)
198
199 const linkPriceSetIndexRelationEntries = indexRelationEntries.filter(
200 (entry) => {
201 return (
202 entry.parent_id === linkId &&
203 entry.parent_name === "LinkProductVariantPriceSet" &&
204 entry.child_id === priceSetId &&
205 entry.child_name === "PriceSet"
206 )
207 }
208 )
209
210 expect(linkPriceSetIndexRelationEntries).toHaveLength(1)
211
212 const priceSetPriceIndexRelationEntries = indexRelationEntries.filter(
213 (entry) => {
214 return (
215 entry.parent_id === priceSetId &&
216 entry.parent_name === "PriceSet" &&
217 entry.child_id === priceId &&
218 entry.child_name === "Price"
219 )
220 }
221 )
222
223 expect(priceSetPriceIndexRelationEntries).toHaveLength(1)
224 })
225
226 it("should not create index entries for events whose data cannot be resolved from the query", async function () {
227 await testBed.eventBus.emit([
228 {
229 name: "product.created",
230 data: {
231 id: "prod_does_not_exist",
232 },
233 },
234 ])
235
236 const { indexEntries, indexRelationEntries } = await findIndexEntries(
237 testBed
238 )
239
240 expect(indexEntries).toHaveLength(0)
241 expect(indexRelationEntries).toHaveLength(0)
242 })
243 })
244
245 describe("on unordered created or attached events", function () {
246 const eventDataToEmit: EventBusTypes.Event[] = [
247 {
248 name: "variant.created",
249 data: {
250 id: variantId,
251 product: {
252 id: productId,
253 },
254 },
255 },
256 {
257 name: "product.created",
258 data: {
259 id: productId,
260 },
261 },
262 {
263 name: "product.created",
264 data: {
265 id: productId2,
266 },
267 },
268 {
269 name: "variant.created",
270 data: {
271 id: variantId2,
272 product: {
273 id: productId2,
274 },
275 },
276 },
277 {
278 name: "pricing.price-set.created",
279 data: {
280 id: priceSetId,
281 },
282 },
283 {
284 name: "price.created",
285 data: {
286 id: priceId,
287 price_set: {
288 id: priceSetId,
289 },
290 },
291 },
292 {
293 name: "LinkProductVariantPriceSet.attached",
294 data: {
295 id: linkId,
296 variant_id: variantId,
297 price_set_id: priceSetId,
298 },
299 },
300 ]
301
302 it("should create the corresponding index entries and index relation entries", async function () {
303 await testBed.eventBus.emit(eventDataToEmit)
304
305 const { indexEntries, indexRelationEntries } = await findIndexEntries(
306 testBed
307 )
308
309 const productIndexEntries = indexEntries.filter((entry) => {
310 return entry.name === "Product"
311 })
312
313 expect(productIndexEntries).toHaveLength(2)
314 expect(productIndexEntries).toEqual(
315 expect.arrayContaining([
316 expect.objectContaining({
317 id: productId,
318 data: expect.objectContaining({
319 id: productId,
320 title: expect.stringContaining("Test Product"),
321 }),
322 }),
323 expect.objectContaining({
324 id: productId2,
325 data: expect.objectContaining({
326 id: productId2,
327 title: expect.stringContaining("Test Product"),
328 }),
329 }),
330 ])
331 )
332
333 const variantIndexEntries = indexEntries.filter((entry) => {
334 return entry.name === "ProductVariant"
335 })
336
337 expect(variantIndexEntries).toHaveLength(2)
338 expect(variantIndexEntries).toEqual(
339 expect.arrayContaining([
340 expect.objectContaining({
341 id: variantId,
342 data: expect.objectContaining({
343 id: variantId,
344 }),
345 }),
346 expect.objectContaining({
347 id: variantId2,
348 data: expect.objectContaining({
349 id: variantId2,
350 }),
351 }),
352 ])
353 )
354
355 const priceSetIndexEntries = indexEntries.filter((entry) => {
356 return entry.name === "PriceSet"
357 })
358
359 expect(priceSetIndexEntries).toHaveLength(1)
360 expect(priceSetIndexEntries[0].id).toEqual(priceSetId)
361
362 const priceIndexEntries = indexEntries.filter((entry) => {
363 return entry.name === "Price"
364 })
365
366 expect(priceIndexEntries).toHaveLength(1)
367 expect(priceIndexEntries[0].id).toEqual(priceId)
368
369 const linkIndexEntries = indexEntries.filter((entry) => {
370 return entry.name === "LinkProductVariantPriceSet"
371 })
372
373 expect(linkIndexEntries).toHaveLength(1)
374 expect(linkIndexEntries[0].id).toEqual(linkId)
375
376 expect(indexRelationEntries).toHaveLength(5)
377
378 const productVariantIndexRelationEntries = indexRelationEntries.filter(
379 (entry) => {
380 return (
381 entry.parent_id === productId &&
382 entry.parent_name === "Product" &&
383 entry.child_id === variantId &&
384 entry.child_name === "ProductVariant"
385 )
386 }
387 )
388
389 expect(productVariantIndexRelationEntries).toHaveLength(1)
390
391 const productVariant2IndexRelationEntries = indexRelationEntries.filter(
392 (entry) => {
393 return (
394 entry.parent_id === productId2 &&
395 entry.parent_name === "Product" &&
396 entry.child_id === variantId2 &&
397 entry.child_name === "ProductVariant"
398 )
399 }
400 )
401
402 expect(productVariant2IndexRelationEntries).toHaveLength(1)
403
404 const variantLinkIndexRelationEntries = indexRelationEntries.filter(
405 (entry) => {
406 return (
407 entry.parent_id === variantId &&
408 entry.parent_name === "ProductVariant" &&
409 entry.child_id === linkId &&
410 entry.child_name === "LinkProductVariantPriceSet"
411 )
412 }
413 )
414
415 expect(variantLinkIndexRelationEntries).toHaveLength(1)
416
417 const linkPriceSetIndexRelationEntries = indexRelationEntries.filter(
418 (entry) => {
419 return (
420 entry.parent_id === linkId &&
421 entry.parent_name === "LinkProductVariantPriceSet" &&
422 entry.child_id === priceSetId &&
423 entry.child_name === "PriceSet"
424 )
425 }
426 )
427
428 expect(linkPriceSetIndexRelationEntries).toHaveLength(1)
429
430 const priceSetPriceIndexRelationEntries = indexRelationEntries.filter(
431 (entry) => {
432 return (
433 entry.parent_id === priceSetId &&
434 entry.parent_name === "PriceSet" &&
435 entry.child_id === priceId &&
436 entry.child_name === "Price"
437 )
438 }
439 )
440
441 expect(priceSetPriceIndexRelationEntries).toHaveLength(1)
442 })
443 })
444
445 describe("on updated events", function () {
446 beforeEach(async () => {
447 const manager = testBed.forkManager()
448 const indexRepository = manager.getRepository(toMikroORMEntity(IndexData))
449
450 await indexRepository.upsertMany([
451 {
452 id: productId,
453 name: "Product",
454 data: {
455 id: productId,
456 },
457 },
458 {
459 id: variantId,
460 name: "ProductVariant",
461 data: {
462 id: variantId,
463 sku: "aaa test aaa",
464 product: {
465 id: productId,
466 },
467 },
468 },
469 ])
470
471 testBed.query
472 .seed("product", [{ id: productId, title: "updated Title" }])
473 .seed("product_variant", [
474 {
475 id: variantId,
476 sku: "updated sku",
477 product: [{ id: productId }],
478 },
479 ])
480
481 await testBed.eventBus.emit([
482 {
483 name: "product.updated",
484 data: {
485 id: productId,
486 },
487 },
488 {
489 name: "variant.updated",
490 data: {
491 id: variantId,
492 product: {
493 id: productId,
494 },
495 },
496 },
497 ])
498 })
499
500 it("should update the corresponding index entries", async () => {
501 const { indexEntries: updatedIndexEntries } = await findIndexEntries(
502 testBed
503 )
504
505 expect(updatedIndexEntries).toHaveLength(2)
506
507 const productEntry = updatedIndexEntries.find((entry) => {
508 return entry.name === "Product" && entry.id === productId
509 })
510
511 expect((productEntry?.data as any)?.title).toEqual("updated Title")
512
513 const variantEntry = updatedIndexEntries.find((entry) => {
514 return entry.name === "ProductVariant" && entry.id === variantId
515 })
516
517 expect((variantEntry?.data as any)?.sku).toEqual("updated sku")
518 })
519 })
520
521 describe("on deleted events", function () {
522 const eventDataToEmit: EventBusTypes.Event[] = [
523 {
524 name: "product.created",
525 data: {
526 id: productId,
527 },
528 },
529 {
530 name: "variant.created",
531 data: {
532 id: variantId,
533 product: {
534 id: productId,
535 },
536 },
537 },
538 {
539 name: "pricing.price-set.created",
540 data: {
541 id: priceSetId,
542 },
543 },
544 {
545 name: "price.created",
546 data: {
547 id: priceId,
548 price_set: {
549 id: priceSetId,
550 },
551 },
552 },
553 {
554 name: "LinkProductVariantPriceSet.attached",
555 data: {
556 id: linkId,
557 variant_id: variantId,
558 price_set_id: priceSetId,
559 },
560 },
561 ]
562
563 const deleteEventDataToEmit: EventBusTypes.Event[] = [
564 {
565 name: "product.deleted",
566 data: {
567 id: productId,
568 },
569 },
570 {
571 name: "variant.deleted",
572 data: {
573 id: variantId,
574 },
575 },
576 ]
577
578 beforeEach(async () => {
579 await testBed.eventBus.emit(eventDataToEmit)
580 await testBed.eventBus.emit(deleteEventDataToEmit)
581 })
582
583 it("should consume all deleted events and delete the index entries", async () => {
584 const { indexEntries, indexRelationEntries } = await findIndexEntries(
585 testBed
586 )
587
588 expect(indexEntries).toHaveLength(3)
589 expect(indexRelationEntries).toHaveLength(2)
590 })
591 })
592 })