-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (118 loc) · 3.41 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express=require('express');
const app=express();
const passport=require('passport');
const mongoose=require('mongoose');
const flash=require('connect-flash');
const session=require('express-session');
const auth=require('./config/auth');
const passportSetup = require('./config/passport-setup-shop');
const Shop=require('./models/Shop');
const nodemailer=require('nodemailer');
const Order=require('./models/Order');
require('./config/passport-setup-shop-local')(passport);
const bodyParser=require('body-parser');
require('dotenv').config();
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
mongoose.connect(process.env.MONGO,{
useNewUrlParser: true,
useUnifiedTopology: true
});
app.set('view engine','ejs');
app.use(express.urlencoded({extended:false}));
app.use(express.static('public'))
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}))
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req,res,next)=>{
res.locals.success_msg=req.flash('success_msg');
res.locals.error =req.flash('error');
res.locals.chat=req.user;
next();
})
app.use('/auth',require('./routes/auth-routes-shop'));
app.use('/shop',require('./routes/shop-routes'));
app.delete('/api/order/:id',(req,res)=>{
console.log(req.params.id);
Order.findById(req.params.id).then((data)=>{
const mailOptions = {
from: '[email protected]',
to: data.fromEmail,
subject: "Order Declined",
text: `Dear ${data.fromName} ,
Your order has been declined , sorry for the inconvinence , Try again after some time`
};
transporter.sendMail(mailOptions,(err,data)=>{
if(err){
console.log(err);
}else{
console.log('email sent!!!',data);
}
})
data.remove();
res.json("Order Deleted");
}).catch((err)=>{
throw err;
})
})
app.put('/api/order/:id',(req,res)=>{
console.log(req.body);
Order.findOneAndUpdate({_id : req.params.id},{$set:{orderCompleted : true}},{upsert:true,new:true}).then((data)=>{
res.json('data Updates Successfully');
const mailOptions = {
from: '[email protected]',
to: data.fromEmail,
subject: "Order Delevered",
text: `Thanks ${data.fromName} ,
For Using Our Platform
regards
Pansari Team`
};
transporter.sendMail(mailOptions,(err,data)=>{
if(err){
console.log(err);
}else{
console.log('email sent!!!',data);
}
})
})
})
app.post('/api/order/:id',(req,res)=>{
console.log(req.body);
const { time,email } =req.body;
console.log(time,email);
res.json('Time allotted');
const mailOptions = {
from: '[email protected]',
to: email,
subject: "Order Accepted",
text: ` Your Order Has Been Accepted
and we alloted you a time slot of ${time}
and be carefully and wear mask always and
please be on time else your order get cancelled`
};
transporter.sendMail(mailOptions,(err,data)=>{
if(err){
console.log(err);
}else{
console.log('email sent!!!',data);
}
})
})
const PORT=process.env.PORT || 5000;
const server=app.listen(PORT,function(){
console.log(`running on port number ${PORT}`);
});