diff --git a/data/emails/layouts/main.pug b/data/emails/layouts/main.pug index 97994d8..2e54ee8 100644 --- a/data/emails/layouts/main.pug +++ b/data/emails/layouts/main.pug @@ -9,6 +9,12 @@ html(lang="en") margin: 0; padding: 0; } + .warn { + color: #a8a227; + } + .success { + color: #58910d; + } .container { padding-top: 40px; max-width: 600px; @@ -17,6 +23,19 @@ html(lang="en") border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } + .list { + margin: 0 auto; + width: 100%; + border-collapse: collapse; + border-color: #dddddd; + } + .list img { + max-width: 100px; + height: auto; + } + .list td { + padding: 10px; + } h1 { color: #333333; } @@ -36,6 +55,12 @@ html(lang="en") padding: 0 20px; text-align: center; } + .header { + margin-bottom: 40px; + padding: 20px; + text-align: center; + background-color: #afafaf; + } .footer { margin-top: 40px; padding: 20px; @@ -49,8 +74,20 @@ html(lang="en") .description { font-size: 16px; } + .bold { + font-weight: bold; + } + .small { + font-size: 12px; + color: #aaaaaa; + } + .align-right { + text-align: right; + } body
+ div(class="header") + p Your Medusa shop div(class="center title") block title div(class="center description") diff --git a/data/emails/order.cancelled/html.pug b/data/emails/order.cancelled/html.pug new file mode 100644 index 0000000..6350afe --- /dev/null +++ b/data/emails/order.cancelled/html.pug @@ -0,0 +1,23 @@ +extends ./../layouts/main.pug +block title + p(class="warn") Your order has been cancelled! +block description + p(class="warn") Here are the items that were in your order + table(class="list" border="1") + for item in order.items + tr + td + p #{item.title} + td + img(src=item.thumbnail) + td + p #{item.unit_price / 100} #{order.currency_code === 'usd' ? '$' : order.currency_code} + tr + td(colspan="2") + p(class="align-right bold") Total + td + p(class="bold") #{total_value} #{order.currency_code === 'usd' ? '$' : order.currency_code} + p(class="small") Order ID: #{order.id} +//- +//- block actions +//- a(href=link, target="_blank" class="button") Visit our website diff --git a/data/emails/order.cancelled/subject.pug b/data/emails/order.cancelled/subject.pug new file mode 100644 index 0000000..05aa9e6 --- /dev/null +++ b/data/emails/order.cancelled/subject.pug @@ -0,0 +1 @@ += `Event ${event} emitted` \ No newline at end of file diff --git a/data/emails/order.completed/html.pug b/data/emails/order.completed/html.pug new file mode 100644 index 0000000..c583be1 --- /dev/null +++ b/data/emails/order.completed/html.pug @@ -0,0 +1,9 @@ +extends ./../layouts/main.pug +block title + p(class="success") Your order has been completed! +block description + p Thanks for your purchase! + p(class="small") Order ID: #{order.id} +//- +//- block actions +//- a(href=link, target="_blank" class="button") Visit our website diff --git a/data/emails/order.completed/subject.pug b/data/emails/order.completed/subject.pug new file mode 100644 index 0000000..05aa9e6 --- /dev/null +++ b/data/emails/order.completed/subject.pug @@ -0,0 +1 @@ += `Event ${event} emitted` \ No newline at end of file diff --git a/data/emails/order.placed/html.pug b/data/emails/order.placed/html.pug index 410720a..e5ff8a6 100644 --- a/data/emails/order.placed/html.pug +++ b/data/emails/order.placed/html.pug @@ -1,10 +1,23 @@ extends ./../layouts/main.pug block title - p Your order ##{event} has been received + p Your order has been received! block description - p Rest of information about #{ event } - p #{JSON.stringify(data)} - for item in orderItems - li= #{JSON.stringify(item)} -block actions - a(href=website, target="_blank" class="button") Visit our website + p Here are the items you ordered + table(class="list" border="1") + for item in order.items + tr + td + p #{item.title} + td + img(src=item.thumbnail) + td + p #{item.unit_price / 100} #{order.currency_code === 'usd' ? '$' : order.currency_code} + tr + td(colspan="2") + p(class="align-right bold") Total + td + p(class="bold") #{total_value} #{order.currency_code === 'usd' ? '$' : order.currency_code} + p(class="small") Order ID: #{order.id} +//- +//- block actions +//- a(href=link, target="_blank" class="button") Visit our website diff --git a/data/emails/order.refund_created/html.pug b/data/emails/order.refund_created/html.pug new file mode 100644 index 0000000..d6b8d8f --- /dev/null +++ b/data/emails/order.refund_created/html.pug @@ -0,0 +1,9 @@ +extends ./../layouts/main.pug +block title + p(class="success") Your order refund is on your way! +block description + p We're sending back your #{total_value} #{order.currency_code === 'usd' ? '$' : order.currency_code} + p(class="small") Order ID: #{order.id} +//- +//- block actions +//- a(href=link, target="_blank" class="button") Visit our website diff --git a/data/emails/order.refund_created/subject.pug b/data/emails/order.refund_created/subject.pug new file mode 100644 index 0000000..05aa9e6 --- /dev/null +++ b/data/emails/order.refund_created/subject.pug @@ -0,0 +1 @@ += `Event ${event} emitted` \ No newline at end of file diff --git a/data/emails/order.shipment_created/html.pug b/data/emails/order.shipment_created/html.pug new file mode 100644 index 0000000..5395449 --- /dev/null +++ b/data/emails/order.shipment_created/html.pug @@ -0,0 +1,9 @@ +extends ./../layouts/main.pug +block title + p(class="success") Your order has been shipped! +block description + p Thanks for your purchase! + p(class="small") Order ID: #{order.id} +//- +//- block actions +//- a(href=link, target="_blank" class="button") Visit our website diff --git a/data/emails/order.shipment_created/subject.pug b/data/emails/order.shipment_created/subject.pug new file mode 100644 index 0000000..05aa9e6 --- /dev/null +++ b/data/emails/order.shipment_created/subject.pug @@ -0,0 +1 @@ += `Event ${event} emitted` \ No newline at end of file diff --git a/package.json b/package.json index e7e1cd4..cf36b2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rootxpdev/medusa-email-plugin", - "version": "0.3.5", + "version": "0.4.0", "description": "Send emails when certain actions happens in medusa store", "main": "index.js", "repository": { diff --git a/src/loaders/notification-subscriber.ts b/src/loaders/notification-subscriber.ts index e490112..fd269cc 100644 --- a/src/loaders/notification-subscriber.ts +++ b/src/loaders/notification-subscriber.ts @@ -15,8 +15,6 @@ export default async ( OrderService.Events.PLACED, OrderService.Events.CANCELED, OrderService.Events.COMPLETED, - OrderService.Events.PAYMENT_CAPTURED, - OrderService.Events.FULFILLMENT_CREATED, OrderService.Events.SHIPMENT_CREATED, OrderService.Events.REFUND_CREATED, 'testing', diff --git a/src/services/emails.ts b/src/services/emails.ts index 9d9ecc6..9361fd0 100644 --- a/src/services/emails.ts +++ b/src/services/emails.ts @@ -1,5 +1,5 @@ import { - AbstractNotificationService, + AbstractNotificationService, CartService, LineItemService, Logger, OrderService, } from "@medusajs/medusa"; @@ -19,22 +19,26 @@ class EmailsService extends AbstractNotificationService { static identifier = 'emails'; static is_installed = true; - protected orderService_: OrderService; - protected logger_: Logger; + protected orderService: OrderService; + protected cartService: CartService; + protected lineItemService: LineItemService; + protected logger: Logger; protected emailConfig: EmailConfig; constructor(container: any, _options: EmailConfig) { super(container); - this.logger_ = container.logger; - this.logger_.info("✔ Email service initialized"); + this.logger = container.logger; + this.logger.info("✔ Email service initialized"); - this.orderService_ = container.orderService; + this.orderService = container.orderService; + this.cartService = container.cartService; + this.lineItemService = container.lineItemService; this.emailConfig = _options; if (!this.emailConfig.templateDir) { this.emailConfig.templateDir = "node_modules/@rootxpdev/medusa-email-plugin/data/emails"; } - this.logger_.info(`Email templates loaded from ${this.emailConfig.templateDir}`); + this.logger.info(`Email templates loaded from ${this.emailConfig.templateDir}`); } async sendNotification( @@ -46,19 +50,31 @@ class EmailsService extends AbstractNotificationService { status: string; data: Record; }> { - this.logger_.info(`Sending notification '${event}' via email service`); - if (event === "order.placed") { + this.logger.info(`Sending notification '${event}' via email service`); + if (event.includes("order.")) { // retrieve order // @ts-ignore - const order = await this.orderService_.retrieve(data.id || ''); + const order = await this.orderService.retrieve(data.id || '', { + relations: [ + "refunds", + "items", + ] + }); + this.logger.info(`Order: ${JSON.stringify(order)}`); await this.sendEmail(order.email, 'Order received', event, { - orderItems: order.items, + event, + order, + cart: await this.cartService.retrieve(order.cart_id || ''), + id: data.id, + total_value: (order.items.reduce((value, item) => { + return value + item.unit_price; + }, 0) / 100).toFixed(2), }) return { - to: 'test@test.com', - data: {}, + to: order.email, + data: data, status: "sent", }; } @@ -96,6 +112,8 @@ class EmailsService extends AbstractNotificationService { } async sendEmail(toAddress: string, subject: string, templateName: string, data: any) { + // console.log('data', data) + this.logger.info(JSON.stringify(data)); const transport = nodemailer.createTransport({ host: this.emailConfig.smtpHost, port: this.emailConfig.smtpPort, @@ -104,7 +122,7 @@ class EmailsService extends AbstractNotificationService { pass: this.emailConfig.smtpPassword, } }); - this.logger_.info(`Sending email to '${toAddress}' using template '${templateName}'`); + this.logger.info(`Sending email to '${toAddress}' using template '${templateName}'`); const email = new EmailTemplates({ message: { from: this.emailConfig.fromAddress, @@ -123,7 +141,9 @@ class EmailsService extends AbstractNotificationService { message: { to: toAddress, }, - locals: data, + locals: { + ...data, + }, }); } }