@@ -2,13 +2,14 @@ import {
22 SubscriberArgs ,
33 type SubscriberConfig ,
44} from "@medusajs/medusa"
5- import { Modules , ContainerRegistrationKeys , MedusaError } from "@medusajs/framework/utils"
6- import { renderTemplate } from "@codee-sh/medusa-plugin-notification-emails/templates/emails"
7- import { TEMPLATES_NAMES } from "@codee-sh/medusa-plugin-notification-emails/templates/emails/types"
8- import { formatDate , getFormattedAddress , getLocaleAmount , getTotalCaptured } from "@codee-sh/medusa-plugin-notification-emails/utils"
9- import { getPluginOptions } from "@codee-sh/medusa-plugin-notification-emails/utils/plugins"
5+ import { Modules , MedusaError } from "@medusajs/framework/utils"
6+ import { renderTemplate } from "../templates/emails"
7+ import { TEMPLATES_NAMES } from "../templates/emails/types"
8+ import { transformContext } from "../utils/transforms"
9+ import { getPluginOptions } from "../utils/plugins"
10+ import { getOrderByIdWorkflow } from "../workflows/order/get-order-by-id"
1011
11- export default async function orderCompletedEmailsHandler ( {
12+ export default async function orderPlacedEmailsHandler ( {
1213 event : { data : { id, trigger_type } } ,
1314 container,
1415} : SubscriberArgs < { id : string , trigger_type : string } > ) {
@@ -17,73 +18,24 @@ export default async function orderCompletedEmailsHandler({
1718 const notificationModuleService = container . resolve (
1819 Modules . NOTIFICATION
1920 )
20- const query = container . resolve ( ContainerRegistrationKeys . QUERY )
2121 const triggerType = trigger_type || 'system'
2222
2323 if ( ! id ) {
2424 throw new MedusaError ( MedusaError . Types . INVALID_ARGUMENT , "Order ID is required" )
2525 }
2626
27- const { data : [ order ] } = await query . graph ( {
28- entity : "order" ,
29- fields : [
30- "id" ,
31- "email" ,
32- "created_at" ,
33- "updated_at" ,
34- "payment_collections.*" ,
35- "items.*" ,
36- "items.variant.*" ,
37- "items.variant.product.*" ,
38- "currency_code" ,
39- "display_id" ,
40- "sales_channel.name" ,
41- "sales_channel.description" ,
42- "shipping_address.*" ,
43- "billing_address.*" ,
44- "summary.*" ,
45- "tax_total" ,
46- "discount_total" ,
47- ] ,
48- filters : {
49- id : id ,
27+ const { result : order } = await getOrderByIdWorkflow ( container ) . run ( {
28+ input : {
29+ order_id : id ,
5030 } ,
5131 } )
5232
5333 if ( ! order ) {
5434 return
5535 }
5636
57- const shippingAddressText = getFormattedAddress ( { address : order . shipping_address } ) . join ( "<br/>" ) ;
58- const billingAddressText = getFormattedAddress ( { address : order . billing_address } ) . join ( "<br/>" ) ;
59- const templateData = {
60- sales_channel : {
61- name : order ?. sales_channel ?. name ,
62- description : order ?. sales_channel ?. description ,
63- } ,
64- orderNumber : `#${ order . display_id } ` ,
65- customerName : order . email ,
66- customerEmail : order . email ,
67- orderDate : formatDate ( { date : order . created_at , includeTime : true , localeCode : "pl" } ) ,
68- completedDate : formatDate ( { date : order . updated_at || order . created_at , includeTime : true , localeCode : "pl" } ) ,
69- totalAmount : order . items . reduce ( ( acc , item ) => acc + ( item . variant ?. prices ?. [ 0 ] ?. amount || 0 ) * item . quantity , 0 ) ,
70- currency : order . currency_code ,
71- items : order . items . map ( ( item ) => ( {
72- thumbnail : item . thumbnail ,
73- title : item . title ,
74- quantity : item . quantity ,
75- price : getLocaleAmount ( item . unit_price , order . currency_code ) ,
76- } ) ) ,
77- shippingAddress : shippingAddressText ,
78- billingAddress : billingAddressText ,
79- summary : {
80- total : getLocaleAmount ( order . summary . original_order_total , order . currency_code ) ,
81- paid_total : getLocaleAmount ( getTotalCaptured ( order . payment_collections || [ ] ) , order . currency_code ) ,
82- tax_total : getLocaleAmount ( order . tax_total , order . currency_code ) ,
83- discount_total : getLocaleAmount ( order . discount_total , order . currency_code ) ,
84- currency_code : order . currency_code ,
85- }
86- } ;
37+ // Transform raw order data to email template format
38+ const templateData = transformContext ( "order" , order , "pl" )
8739
8840 const templateName = TEMPLATES_NAMES . ORDER_COMPLETED
8941
@@ -97,7 +49,7 @@ export default async function orderCompletedEmailsHandler({
9749 )
9850
9951 const result = await notificationModuleService . createNotifications ( {
100- to : order . email ,
52+ to : order . order . customer . email ,
10153 channel : "email" ,
10254 template : templateName ,
10355 trigger_type : triggerType ,
0 commit comments