Skip to content

Commit

Permalink
additional email templates added
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnisLielturks committed Apr 25, 2024
1 parent df6400d commit c9c405e
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 25 deletions.
37 changes: 37 additions & 0 deletions data/emails/layouts/main.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ html(lang="en")
margin: 0;
padding: 0;
}
.warn {
color: #a8a227;
}
.success {
color: #58910d;
}
.container {
padding-top: 40px;
max-width: 600px;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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="container">
div(class="header")
p Your Medusa shop
div(class="center title")
block title
div(class="center description")
Expand Down
23 changes: 23 additions & 0 deletions data/emails/order.cancelled/html.pug
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions data/emails/order.cancelled/subject.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= `Event ${event} emitted`
9 changes: 9 additions & 0 deletions data/emails/order.completed/html.pug
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions data/emails/order.completed/subject.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= `Event ${event} emitted`
27 changes: 20 additions & 7 deletions data/emails/order.placed/html.pug
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions data/emails/order.refund_created/html.pug
Original file line number Diff line number Diff line change
@@ -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 <span class="bold">#{total_value} #{order.currency_code === 'usd' ? '$' : order.currency_code}</span>
p(class="small") Order ID: #{order.id}
//-
//- block actions
//- a(href=link, target="_blank" class="button") Visit our website
1 change: 1 addition & 0 deletions data/emails/order.refund_created/subject.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= `Event ${event} emitted`
9 changes: 9 additions & 0 deletions data/emails/order.shipment_created/html.pug
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions data/emails/order.shipment_created/subject.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= `Event ${event} emitted`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions src/loaders/notification-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
50 changes: 35 additions & 15 deletions src/services/emails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
AbstractNotificationService,
AbstractNotificationService, CartService, LineItemService,
Logger,
OrderService,
} from "@medusajs/medusa";
Expand All @@ -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(
Expand All @@ -46,19 +50,31 @@ class EmailsService extends AbstractNotificationService {
status: string;
data: Record<string, any>;
}> {
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",
};
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -123,7 +141,9 @@ class EmailsService extends AbstractNotificationService {
message: {
to: toAddress,
},
locals: data,
locals: {
...data,
},
});
}
}
Expand Down

0 comments on commit c9c405e

Please sign in to comment.