diff --git a/package-lock.json b/package-lock.json
index 8dbfac3..38de821 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
+ "@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6",
"dotenv": "^16.1.4",
@@ -25,6 +26,41 @@
"nodemon": "^2.0.22"
}
},
+ "node_modules/@sendgrid/client": {
+ "version": "7.7.0",
+ "resolved": "https://registry.npmjs.org/@sendgrid/client/-/client-7.7.0.tgz",
+ "integrity": "sha512-SxH+y8jeAQSnDavrTD0uGDXYIIkFylCo+eDofVmZLQ0f862nnqbC3Vd1ej6b7Le7lboyzQF6F7Fodv02rYspuA==",
+ "dependencies": {
+ "@sendgrid/helpers": "^7.7.0",
+ "axios": "^0.26.0"
+ },
+ "engines": {
+ "node": "6.* || 8.* || >=10.*"
+ }
+ },
+ "node_modules/@sendgrid/helpers": {
+ "version": "7.7.0",
+ "resolved": "https://registry.npmjs.org/@sendgrid/helpers/-/helpers-7.7.0.tgz",
+ "integrity": "sha512-3AsAxfN3GDBcXoZ/y1mzAAbKzTtUZ5+ZrHOmWQ279AuaFXUNCh9bPnRpN504bgveTqoW+11IzPg3I0WVgDINpw==",
+ "dependencies": {
+ "deepmerge": "^4.2.2"
+ },
+ "engines": {
+ "node": ">= 6.0.0"
+ }
+ },
+ "node_modules/@sendgrid/mail": {
+ "version": "7.7.0",
+ "resolved": "https://registry.npmjs.org/@sendgrid/mail/-/mail-7.7.0.tgz",
+ "integrity": "sha512-5+nApPE9wINBvHSUxwOxkkQqM/IAAaBYoP9hw7WwgDNQPxraruVqHizeTitVtKGiqWCKm2mnjh4XGN3fvFLqaw==",
+ "dependencies": {
+ "@sendgrid/client": "^7.7.0",
+ "@sendgrid/helpers": "^7.7.0"
+ },
+ "engines": {
+ "node": "6.* || 8.* || >=10.*"
+ }
+ },
"node_modules/@types/bcryptjs": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz",
@@ -185,6 +221,14 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
},
+ "node_modules/axios": {
+ "version": "0.26.1",
+ "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
+ "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
+ "dependencies": {
+ "follow-redirects": "^1.14.8"
+ }
+ },
"node_modules/balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@@ -376,6 +420,14 @@
"ms": "2.0.0"
}
},
+ "node_modules/deepmerge": {
+ "version": "4.3.1",
+ "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
+ "integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==",
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@@ -508,6 +560,25 @@
"node": ">= 0.8"
}
},
+ "node_modules/follow-redirects": {
+ "version": "1.15.2",
+ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
+ "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
+ "funding": [
+ {
+ "type": "individual",
+ "url": "https://github.com/sponsors/RubenVerborgh"
+ }
+ ],
+ "engines": {
+ "node": ">=4.0"
+ },
+ "peerDependenciesMeta": {
+ "debug": {
+ "optional": true
+ }
+ }
+ },
"node_modules/forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
diff --git a/package.json b/package.json
index 8885d7b..9e4917b 100644
--- a/package.json
+++ b/package.json
@@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
+ "@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6",
"dotenv": "^16.1.4",
diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts
index 0b8464f..d3f2184 100644
--- a/src/controllers/CartController.ts
+++ b/src/controllers/CartController.ts
@@ -1,6 +1,9 @@
import { Request, Response } from 'express';
import { Cart, ICart, Product, Order, IOrder } from '../mongoose/Schema';
import { sendEmailasync } from '../services/sendGrid';
+import { config } from 'dotenv';
+
+config();
export async function addToCart(req: Request, res: Response) {
try {
@@ -63,11 +66,8 @@ export async function checkout(req: Request, res: Response) {
products: usersCart.products,
});
- console.log(`order: ${order}`)
await removeCart(userId);
- console.log(`cart removed`)
- sendEmailasync(userId);
-
+ sendEmailasync(order.id, userId);
res.status(200).json(order);
}
diff --git a/src/services/sendGrid.ts b/src/services/sendGrid.ts
index a92969a..d23428b 100644
--- a/src/services/sendGrid.ts
+++ b/src/services/sendGrid.ts
@@ -1,13 +1,26 @@
import { config } from "dotenv";
import { User, Order } from "../mongoose/Schema";
+import client from '@sendgrid/mail';
config();
-export async function sendEmailasync (userId: string) {
- const msg = {
- to: await User.findOne({ _id: userId }).select('email'),
- from: 'Ecom',
- subject: 'Order Confirmation',
- text: 'Your order has been placed',
- html: 'Thank you for shopping with us!'
- };
+export async function sendEmailasync(orderId:string, userId: string) {
+ console.log(`Sending email for order ${orderId} to user ${userId}`);
+ try {
+ client.setApiKey(process.env.SENDGRID_API_KEY);
+ const msg = {
+ to: await User.findOne({ _id: userId }).select('email'),
+ from: process.env.EMAIL_FROM,
+ subject: 'Order Confirmation',
+ text: 'Your order has been placed',
+ html: 'Thank you for shopping with us!'
+ };
+ await client.send(msg);
+ console.log(`Email sent for order ${orderId} to user ${userId}`);
+ // Update the order to indicate email has been sent
+ await Order.findOne({ _id: orderId }).updateOne({ emailSent: true });
+ console.log(`Updated order ${orderId} to indicate email has been sent`);
+ return;
+ } catch (error) {
+ console.error('Error sending email:', error);
+ }
};
\ No newline at end of file