@@ -40,6 +40,7 @@ import {
40
40
promiseAll ,
41
41
removeNullish ,
42
42
simpleHash ,
43
+ upperCaseFirst ,
43
44
} from "@medusajs/framework/utils"
44
45
45
46
import {
@@ -472,6 +473,7 @@ export default class PricingModuleService
472
473
) : Promise < PriceSetDTO >
473
474
474
475
@InjectManager ( )
476
+ @EmitEvents ( )
475
477
async upsertPriceSets (
476
478
data : UpsertPriceSetDTO | UpsertPriceSetDTO [ ] ,
477
479
@MedusaContext ( ) sharedContext : Context = { }
@@ -513,6 +515,7 @@ export default class PricingModuleService
513
515
) : Promise < PriceSetDTO [ ] >
514
516
515
517
@InjectManager ( )
518
+ @EmitEvents ( )
516
519
// @ts -expect-error
517
520
async updatePriceSets (
518
521
idOrSelector : string | PricingTypes . FilterablePriceSetProps ,
@@ -563,13 +566,19 @@ export default class PricingModuleService
563
566
} )
564
567
565
568
const prices = normalizedData . flatMap ( ( priceSet ) => priceSet . prices || [ ] )
566
- const { entities : upsertedPrices } =
569
+ const { entities : upsertedPrices , performedActions } =
567
570
await this . priceService_ . upsertWithReplace (
568
571
prices ,
569
572
{ relations : [ "price_rules" ] } ,
570
573
sharedContext
571
574
)
572
575
576
+ composeAllEvents ( {
577
+ eventBuilders,
578
+ performedActions,
579
+ sharedContext,
580
+ } )
581
+
573
582
const priceSetsToUpsert = normalizedData . map ( ( priceSet ) => {
574
583
const { prices, ...rest } = priceSet
575
584
return {
@@ -594,13 +603,19 @@ export default class PricingModuleService
594
603
}
595
604
} )
596
605
597
- const { entities : priceSets } =
606
+ const { entities : priceSets , performedActions : priceSetPerformedActions } =
598
607
await this . priceSetService_ . upsertWithReplace (
599
608
priceSetsToUpsert ,
600
609
{ relations : [ "prices" ] } ,
601
610
sharedContext
602
611
)
603
612
613
+ composeAllEvents ( {
614
+ eventBuilders,
615
+ performedActions : priceSetPerformedActions ,
616
+ sharedContext,
617
+ } )
618
+
604
619
return priceSets . map ( ( ps ) => {
605
620
if ( ps . prices ) {
606
621
ps . prices = ( ps . prices as any ) . filter ( ( p ) => ! p . price_list_id )
@@ -747,6 +762,7 @@ export default class PricingModuleService
747
762
}
748
763
749
764
@InjectTransactionManager ( )
765
+ @EmitEvents ( )
750
766
// @ts -ignore
751
767
async updatePriceLists (
752
768
data : PricingTypes . UpdatePriceListDTO [ ] ,
@@ -760,6 +776,7 @@ export default class PricingModuleService
760
776
}
761
777
762
778
@InjectManager ( )
779
+ @EmitEvents ( )
763
780
async updatePriceListPrices (
764
781
data : PricingTypes . UpdatePriceListPricesDTO [ ] ,
765
782
@MedusaContext ( ) sharedContext : Context = { }
@@ -770,6 +787,7 @@ export default class PricingModuleService
770
787
}
771
788
772
789
@InjectManager ( )
790
+ @EmitEvents ( )
773
791
async removePrices (
774
792
ids : string [ ] ,
775
793
@MedusaContext ( ) sharedContext : Context = { }
@@ -789,6 +807,7 @@ export default class PricingModuleService
789
807
}
790
808
791
809
@InjectManager ( )
810
+ @EmitEvents ( )
792
811
async setPriceListRules (
793
812
data : PricingTypes . SetPriceListRulesDTO ,
794
813
@MedusaContext ( ) sharedContext : Context = { }
@@ -801,6 +820,7 @@ export default class PricingModuleService
801
820
}
802
821
803
822
@InjectManager ( )
823
+ @EmitEvents ( )
804
824
async removePriceListRules (
805
825
data : PricingTypes . RemovePriceListRulesDTO ,
806
826
@MedusaContext ( ) sharedContext : Context = { }
@@ -852,6 +872,7 @@ export default class PricingModuleService
852
872
) : Promise < PricePreferenceDTO >
853
873
854
874
@InjectManager ( )
875
+ @EmitEvents ( )
855
876
async upsertPricePreferences (
856
877
data : UpsertPricePreferenceDTO | UpsertPricePreferenceDTO [ ] ,
857
878
@MedusaContext ( ) sharedContext : Context = { }
@@ -1081,29 +1102,10 @@ export default class PricingModuleService
1081
1102
{ relations : [ "price_rules" ] } ,
1082
1103
sharedContext
1083
1104
)
1084
- eventBuilders . createdPrice ( {
1085
- data : performedActions . created [ Price . name ] ?? [ ] ,
1086
- sharedContext,
1087
- } )
1088
- eventBuilders . updatedPrice ( {
1089
- data : performedActions . updated [ Price . name ] ?? [ ] ,
1090
- sharedContext,
1091
- } )
1092
- eventBuilders . deletedPrice ( {
1093
- data : performedActions . deleted [ Price . name ] ?? [ ] ,
1094
- sharedContext,
1095
- } )
1096
1105
1097
- eventBuilders . createdPriceRule ( {
1098
- data : performedActions . created [ PriceRule . name ] ?? [ ] ,
1099
- sharedContext,
1100
- } )
1101
- eventBuilders . updatedPriceRule ( {
1102
- data : performedActions . updated [ PriceRule . name ] ?? [ ] ,
1103
- sharedContext,
1104
- } )
1105
- eventBuilders . deletedPriceRule ( {
1106
- data : performedActions . deleted [ PriceRule . name ] ?? [ ] ,
1106
+ composeAllEvents ( {
1107
+ eventBuilders,
1108
+ performedActions,
1107
1109
sharedContext,
1108
1110
} )
1109
1111
@@ -1277,12 +1279,16 @@ export default class PricingModuleService
1277
1279
}
1278
1280
)
1279
1281
1280
- const { entities } = await this . priceListService_ . upsertWithReplace (
1281
- normalizedData ,
1282
- {
1282
+ const { entities, performedActions } =
1283
+ await this . priceListService_ . upsertWithReplace ( normalizedData , {
1283
1284
relations : [ "price_list_rules" ] ,
1284
- }
1285
- )
1285
+ } )
1286
+
1287
+ composeAllEvents ( {
1288
+ eventBuilders,
1289
+ performedActions,
1290
+ sharedContext,
1291
+ } )
1286
1292
1287
1293
return entities
1288
1294
}
@@ -1326,11 +1332,18 @@ export default class PricingModuleService
1326
1332
}
1327
1333
}
1328
1334
1329
- const { entities } = await this . priceService_ . upsertWithReplace (
1330
- pricesToUpsert ,
1331
- { relations : [ "price_rules" ] } ,
1332
- sharedContext
1333
- )
1335
+ const { entities, performedActions } =
1336
+ await this . priceService_ . upsertWithReplace (
1337
+ pricesToUpsert ,
1338
+ { relations : [ "price_rules" ] } ,
1339
+ sharedContext
1340
+ )
1341
+
1342
+ composeAllEvents ( {
1343
+ eventBuilders,
1344
+ performedActions,
1345
+ sharedContext,
1346
+ } )
1334
1347
1335
1348
return entities
1336
1349
}
@@ -1388,29 +1401,9 @@ export default class PricingModuleService
1388
1401
sharedContext
1389
1402
)
1390
1403
1391
- eventBuilders . createdPrice ( {
1392
- data : performedActions . created [ Price . name ] ?? [ ] ,
1393
- sharedContext,
1394
- } )
1395
- eventBuilders . updatedPrice ( {
1396
- data : performedActions . updated [ Price . name ] ?? [ ] ,
1397
- sharedContext,
1398
- } )
1399
- eventBuilders . deletedPrice ( {
1400
- data : performedActions . deleted [ Price . name ] ?? [ ] ,
1401
- sharedContext,
1402
- } )
1403
-
1404
- eventBuilders . createdPriceRule ( {
1405
- data : performedActions . created [ PriceRule . name ] ?? [ ] ,
1406
- sharedContext,
1407
- } )
1408
- eventBuilders . updatedPriceRule ( {
1409
- data : performedActions . updated [ PriceRule . name ] ?? [ ] ,
1410
- sharedContext,
1411
- } )
1412
- eventBuilders . deletedPriceRule ( {
1413
- data : performedActions . deleted [ PriceRule . name ] ?? [ ] ,
1404
+ composeAllEvents ( {
1405
+ eventBuilders,
1406
+ performedActions,
1414
1407
sharedContext,
1415
1408
} )
1416
1409
@@ -1472,11 +1465,18 @@ export default class PricingModuleService
1472
1465
} )
1473
1466
. filter ( Boolean )
1474
1467
1475
- const { entities } = await this . priceListService_ . upsertWithReplace (
1476
- priceListsUpsert ,
1477
- { relations : [ "price_list_rules" ] } ,
1478
- sharedContext
1479
- )
1468
+ const { entities, performedActions } =
1469
+ await this . priceListService_ . upsertWithReplace (
1470
+ priceListsUpsert ,
1471
+ { relations : [ "price_list_rules" ] } ,
1472
+ sharedContext
1473
+ )
1474
+
1475
+ composeAllEvents ( {
1476
+ eventBuilders,
1477
+ performedActions,
1478
+ sharedContext,
1479
+ } )
1480
1480
1481
1481
return entities
1482
1482
}
@@ -1539,11 +1539,18 @@ export default class PricingModuleService
1539
1539
} )
1540
1540
. filter ( Boolean )
1541
1541
1542
- const { entities } = await this . priceListService_ . upsertWithReplace (
1543
- priceListsUpsert ,
1544
- { relations : [ "price_list_rules" ] } ,
1545
- sharedContext
1546
- )
1542
+ const { entities, performedActions } =
1543
+ await this . priceListService_ . upsertWithReplace (
1544
+ priceListsUpsert ,
1545
+ { relations : [ "price_list_rules" ] } ,
1546
+ sharedContext
1547
+ )
1548
+
1549
+ composeAllEvents ( {
1550
+ eventBuilders,
1551
+ performedActions,
1552
+ sharedContext,
1553
+ } )
1547
1554
1548
1555
return entities
1549
1556
}
@@ -1582,6 +1589,26 @@ export default class PricingModuleService
1582
1589
}
1583
1590
}
1584
1591
1592
+ const composeAllEvents = ( {
1593
+ eventBuilders,
1594
+ performedActions,
1595
+ sharedContext,
1596
+ } ) => {
1597
+ for ( const action of Object . keys ( performedActions ) ) {
1598
+ for ( const entity of Object . keys ( performedActions [ action ] ) ) {
1599
+ const eventName = action + upperCaseFirst ( entity )
1600
+ if ( ! eventBuilders [ eventName ] ) {
1601
+ continue
1602
+ }
1603
+
1604
+ eventBuilders [ eventName ] ( {
1605
+ data : performedActions [ action ] [ entity ] ?? [ ] ,
1606
+ sharedContext,
1607
+ } )
1608
+ }
1609
+ }
1610
+ }
1611
+
1585
1612
const isTaxInclusive = (
1586
1613
priceRules : InferEntityType < typeof PriceRule > [ ] ,
1587
1614
preferences : InferEntityType < typeof PricePreference > [ ] ,
0 commit comments