sendgrid added

This commit is contained in:
Kfir Dayan 2023-06-10 01:39:09 +03:00
parent 720abc8c02
commit 4e1ace0586
4 changed files with 97 additions and 12 deletions

71
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"dotenv": "^16.1.4", "dotenv": "^16.1.4",
@ -25,6 +26,41 @@
"nodemon": "^2.0.22" "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": { "node_modules/@types/bcryptjs": {
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz", "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", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" "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": { "node_modules/balanced-match": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@ -376,6 +420,14 @@
"ms": "2.0.0" "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": { "node_modules/depd": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@ -508,6 +560,25 @@
"node": ">= 0.8" "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": { "node_modules/forwarded": {
"version": "0.2.0", "version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",

View file

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"dotenv": "^16.1.4", "dotenv": "^16.1.4",

View file

@ -1,6 +1,9 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { Cart, ICart, Product, Order, IOrder } from '../mongoose/Schema'; import { Cart, ICart, Product, Order, IOrder } from '../mongoose/Schema';
import { sendEmailasync } from '../services/sendGrid'; import { sendEmailasync } from '../services/sendGrid';
import { config } from 'dotenv';
config();
export async function addToCart(req: Request, res: Response) { export async function addToCart(req: Request, res: Response) {
try { try {
@ -63,11 +66,8 @@ export async function checkout(req: Request, res: Response) {
products: usersCart.products, products: usersCart.products,
}); });
console.log(`order: ${order}`)
await removeCart(userId); await removeCart(userId);
console.log(`cart removed`) sendEmailasync(order.id, userId);
sendEmailasync(userId);
res.status(200).json(order); res.status(200).json(order);
} }

View file

@ -1,13 +1,26 @@
import { config } from "dotenv"; import { config } from "dotenv";
import { User, Order } from "../mongoose/Schema"; import { User, Order } from "../mongoose/Schema";
import client from '@sendgrid/mail';
config(); config();
export async function sendEmailasync (userId: string) { 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 = { const msg = {
to: await User.findOne({ _id: userId }).select('email'), to: await User.findOne({ _id: userId }).select('email'),
from: 'Ecom', from: process.env.EMAIL_FROM,
subject: 'Order Confirmation', subject: 'Order Confirmation',
text: 'Your order has been placed', text: 'Your order has been placed',
html: '<strong>Thank you for shopping with us!</strong>' html: '<strong>Thank you for shopping with us!</strong>'
}; };
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);
}
}; };