Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMS Feature Added #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion backend/controllers/orderController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Order = require("../models/orderModel");
const User = require("../models/userModel");
const Product = require("../models/productModel");
const ErrorHander = require("../utils/errorhander");
const catchAsyncErrors = require("../middleware/catchAsyncErrors");
const sendSms = require("../utils/sendSms");

// Create new Order
exports.newOrder = catchAsyncErrors(async (req, res, next) => {
Expand Down Expand Up @@ -80,7 +82,7 @@ exports.getAllOrders = catchAsyncErrors(async (req, res, next) => {
// update Order Status -- Admin
exports.updateOrder = catchAsyncErrors(async (req, res, next) => {
const order = await Order.findById(req.params.id);

const user = await User.findById(order.user);
if (!order) {
return next(new ErrorHander("Order not found with this Id", 404));
}
Expand All @@ -93,11 +95,28 @@ exports.updateOrder = catchAsyncErrors(async (req, res, next) => {
order.orderItems.forEach(async (o) => {
await updateStock(o.product, o.quantity);
});
try {
await sendSms({
body: `Your Order is Shipped Successfully`,
phone_no: user.phone_no,
});
} catch (error) {
return next(new ErrorHander(error.message, 500));
}
}
order.orderStatus = req.body.status;

if (req.body.status === "Delivered") {
order.deliveredAt = Date.now();

try {
await sendSms({
body: `Your Order is Delivered Successfully`,
phone_no: user.phone_no,
});
} catch (error) {
return next(new ErrorHander(error.message, 500));
}
}

await order.save({ validateBeforeSave: false });
Expand Down
3 changes: 2 additions & 1 deletion backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ exports.registerUser = catchAsyncErrors(async (req, res, next) => {
crop: "scale",
});

const { name, email, password } = req.body;
const { name, email, password, phone_no } = req.body;

const user = await User.create({
name,
email,
password,
phone_no,
avatar: {
public_id: myCloud.public_id,
url: myCloud.secure_url,
Expand Down
5 changes: 5 additions & 0 deletions backend/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const userSchema = new mongoose.Schema({
minLength: [8, "Password should be greater than 8 characters"],
select: false,
},
phone_no: {
type: String,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here type of phone_no should be Number

required: [true, "Please Enter Your Contact Number"],
minLength: [10, "Number should be of 10 digits"],
},
avatar: {
public_id: {
type: String,
Expand Down
19 changes: 19 additions & 0 deletions backend/utils/sendSms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const twilio = require("twilio")(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);

const sendSms = async (options) => {
twilio.messages
.create({
from: process.env.TWILIO_PHONE_NO,
to: `+91${options.phone_no}`,
body: options.body,
})
.then((res) => {})
.catch((err) => {
console.log(err);
});
};

module.exports = sendSms;
Loading