Skip to content

Commit bfab3ef

Browse files
author
luluhoc
committed
init
0 parents  commit bfab3ef

31 files changed

+10073
-0
lines changed

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"plugins": [
3+
"@babel/plugin-proposal-class-properties",
4+
"@babel/plugin-transform-instanceof",
5+
"@babel/plugin-transform-classes"
6+
],
7+
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
8+
"env": {
9+
"test": {
10+
"plugins": ["@babel/plugin-transform-runtime"]
11+
}
12+
}
13+
}

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/lib
2+
node_modules
3+
.DS_store
4+
.env*
5+
/*.js
6+
!index.js
7+
!jest.config.js
8+
9+
/dist
10+
11+
/api
12+
/services
13+
/models
14+
/subscribers
15+

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_store
2+
src
3+
dist
4+
yarn.lock
5+
.babelrc
6+
jest.config.js
7+
8+
.turbo
9+
.yarn

CHANGELOG.md

Lines changed: 385 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# SendGrid
2+
3+
Handle sending emails to customers related to orders, restock notifications, users, or custom events.
4+
5+
[SendGrid Plugin Documentation](https://docs.medusajs.com/plugins/notifications/sendgrid) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
6+
7+
## Features
8+
9+
- Send emails when an event related to orders, restock notifications, or users is triggered.
10+
- Use dynamic templates in SendGrid to build the emails to be sent.
11+
- Send emails with SendGrid for custom events.
12+
13+
---
14+
15+
## Prerequisites
16+
17+
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
18+
- [SendGrid account](https://signup.sendgrid.com/)
19+
20+
---
21+
22+
## How to Install
23+
24+
1\. Run the following command in the directory of the Medusa backend:
25+
26+
```bash
27+
npm install medusa-plugin-sendgrid
28+
```
29+
30+
```bash
31+
yarn add medusa-plugin-sendgrid
32+
```
33+
34+
2\. Set the following environment variable in `.env`:
35+
36+
```bash
37+
SENDGRID_API_KEY=<API_KEY>
38+
SENDGRID_FROM=<SEND_FROM_EMAIL>
39+
# IDs for different email templates
40+
SENDGRID_ORDER_PLACED_ID=<ORDER_PLACED_TEMPLATE_ID> # example
41+
```
42+
43+
3\. In `medusa-config.js` add the following at the end of the `plugins` array:
44+
45+
```ts
46+
const plugins = [
47+
// ...,
48+
{
49+
resolve: `medusa-plugin-sendgrid`,
50+
/** @type {import('medusa-plugin-sendgrid').PluginOptions} */
51+
options: {
52+
api_key: process.env.SENDGRID_API_KEY,
53+
from: process.env.SENDGRID_FROM,
54+
templates: {
55+
order_placed_template: process.env.SENDGRID_ORDER_PLACED_ID,
56+
},
57+
localization: {
58+
"de-DE": { // locale key
59+
order_placed_template:
60+
process.env.SENDGRID_ORDER_PLACED_ID_LOCALIZED,
61+
},
62+
},
63+
},
64+
},
65+
]
66+
```
67+
68+
---
69+
70+
## Test the Plugin
71+
72+
1\. Run the following command in the directory of the Medusa backend to run the backend:
73+
74+
```bash
75+
npm run start
76+
```
77+
78+
2\. Place an order using a storefront or the [Store APIs](https://docs.medusajs.com/api/store). You should receive a confirmation email.
79+
80+
---
81+
82+
## Additional Resources
83+
84+
- [SendGrid Plugin Documentation](https://docs.medusajs.com/plugins/notifications/sendgrid)

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: "node",
3+
}

package.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "medusa-plugin-sendgrid-typescript",
3+
"version": "2.0.0",
4+
"description": "SendGrid transactional emails typescript",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/medusajs/medusa",
8+
"directory": "packages/medusa-plugin-sendgrid"
9+
},
10+
"engines": {
11+
"node": ">=16"
12+
},
13+
"types": "dist/types.d.ts",
14+
"files": [
15+
"dist"
16+
],
17+
"author": "Oliver Juhl | Lucjan Grzesik",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"@medusajs/medusa": "^1.20.1",
21+
"@medusajs/types": "^1.11.9",
22+
"@types/express": "^4.17.21",
23+
"client-sessions": "^0.8.0",
24+
"cross-env": "^7.0.3",
25+
"jest": "^29.7.0",
26+
"medusa-interfaces": "^1.3.7",
27+
"medusa-test-utils": "^1.1.41",
28+
"rimraf": "^5.0.5",
29+
"ts-jest": "^29.1.1",
30+
"typescript": "^5.3.3"
31+
},
32+
"scripts": {
33+
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
34+
"test": "jest --passWithNoTests src",
35+
"build": "rimraf dist && tsc",
36+
"watch": "tsc --watch"
37+
},
38+
"peerDependencies": {
39+
"@medusajs/medusa": "^1.19.0",
40+
"medusa-interfaces": "^1.3.7",
41+
"typeorm": "^0.3.16"
42+
},
43+
"dependencies": {
44+
"@medusajs/utils": "^1.11.2",
45+
"@sendgrid/mail": "^8.1.0",
46+
"medusa-core-utils": "^1.2.0"
47+
},
48+
"gitHead": "3bbd1e8507e00bc471de6ae3c30207999a4a4011",
49+
"keywords": [
50+
"medusa-plugin",
51+
"medusa-plugin-notification"
52+
]
53+
}

src/api/store/send-email/route.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type {
2+
MedusaRequest,
3+
MedusaResponse
4+
} from "@medusajs/medusa";
5+
import SendGridService from "../../../services/sendgrid";
6+
7+
8+
export async function POST(req: MedusaRequest, res: MedusaResponse) {
9+
const sendgridService: SendGridService = req.scope.resolve("sendgridService")
10+
11+
await sendgridService.sendEmail({
12+
templateId: req.body.template_id,
13+
from: req.body.from,
14+
to: req.body.to,
15+
dynamicTemplateData: req.body.data || {}
16+
})
17+
res.sendStatus(200)
18+
}

src/loaders/order.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
MedusaContainer,
3+
NotificationService,
4+
} from "@medusajs/medusa"
5+
6+
export default async (
7+
container: MedusaContainer
8+
): Promise<void> => {
9+
const notificationService = container.resolve<NotificationService>(
10+
"notificationService"
11+
)
12+
13+
notificationService.subscribe("order.shipment_created", "sendgrid")
14+
notificationService.subscribe("order.gift_card_created", "sendgrid")
15+
notificationService.subscribe("gift_card.created", "sendgrid")
16+
notificationService.subscribe("order.placed", "sendgrid")
17+
notificationService.subscribe("order.canceled", "sendgrid")
18+
notificationService.subscribe("customer.password_reset", "sendgrid")
19+
notificationService.subscribe("claim.shipment_created", "sendgrid")
20+
notificationService.subscribe("swap.shipment_created", "sendgrid")
21+
notificationService.subscribe("swap.created", "sendgrid")
22+
notificationService.subscribe("order.items_returned", "sendgrid")
23+
notificationService.subscribe("order.return_requested", "sendgrid")
24+
notificationService.subscribe("order.refund_created", "sendgrid")
25+
}

0 commit comments

Comments
 (0)