-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
273 lines (188 loc) · 6.64 KB
/
index.mjs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import express from 'express';
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import cors from 'cors';
import dotenv from 'dotenv';
import pdf from 'html-pdf';
import upiqr from "upiqr";
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
const PORT = process.env.PORT || 5000;
const MONGODB_URI = process.env.MONGODB_URI;
const JWT_SECRET = process.env.JWT_SECRET;
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('Error connecting to MongoDB:', err));
const userSchema = new mongoose.Schema({
username: { type: String, required: true, trim: true },
mobile: { type: String, default: '' },
email: { type: String, required: true, lowercase: true, unique: true },
password: { type: String, default: '' },
time: { type: String },
});
const User = mongoose.model('User', userSchema);
app.post('/signup', async (req, res) => {
try {
const { username, mobile, email, password, time } = req.body;
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'User already exists' });
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const newUser = new User({ username, mobile, email, password: hashedPassword, time });
await newUser.save();
res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/login', async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: 'Invalid email or password' });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid email or password' });
}
const token = jwt.sign(
{ id: user._id },
JWT_SECRET,
{ expiresIn: '1h' }
);
res.status(200).json({
message: 'Login successful',
token,
user: {
name: user.username,
mobile: user.mobile,
email: user.email
}
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/google-signin', async (req, res) => {
try {
const { email, name } = req.body;
let user = await User.findOne({ email });
if (!user) {
user = new User({
username: name,
mobile: '',
email,
password: '',
time: new Date().toISOString(),
});
await user.save();
}
const token = jwt.sign(
{ id: user._id },
JWT_SECRET,
{ expiresIn: '1h' }
);
res.status(200).json({
message: 'Login successful',
token,
user: {
name: user.username,
mobile: user.mobile,
email: user.email,
},
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
const orderSchema = new mongoose.Schema({
orderNumber: { type: Number, unique: true },
name: { type: String, trim: true, required: true },
mobile: { type: String, required: true },
email: { type: String, lowercase: true, required: true },
add: { type: String, required: true },
city: { type: String, required: true },
pincode: { type: Number, required: true },
state: { type: String, required: true },
paytype: { type: String, required: true },
items: { type: Object, required: true },
cost: { type: Number, required: true }
});
const Order = mongoose.model('Order', orderSchema);
app.post('/submit-order', async (req, res) => {
try {
console.log('Incoming request body:', req.body);
const { orderNumber, name, mobile, email, add, pincode, state, paytype, items, cost, city } = req.body;
if (!orderNumber || !name || !mobile || !email || !add || !pincode || !state || !paytype || !items || !cost) {
return res.status(400).json({ message: 'All fields are required' });
}
const newOrder = new Order({
orderNumber,
name,
mobile,
email,
add,
pincode,
city,
state,
paytype,
items,
cost
});
await newOrder.save();
res.status(201).json({ message: 'Order Submitted Successfully' });
} catch (error) {
if (error.code === 11000) {
return res.status(400).json({ message: 'Duplicate entry detected' });
}
console.error('Error in /submit-order:', error.message);
res.status(500).json({ error: 'Internal Server Error', details: error.message });
}
});
app.post('/verify', async (req, res) => {
try {
const { token } = req.body;
const decode = jwt.verify(token, JWT_SECRET);
res.status(200).json(
decode
);
} catch (error) {
res.status(500).json({ error: error.message });
}
})
app.post('/generate-pdf', (req, res) => {
const htmlContent = req.body.html;
pdf.create(htmlContent).toStream((err, stream) => {
if (err) {
console.error('Error generating PDF:', err);
return res.status(500).send('Error generating PDF');
}
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=invoice.pdf');
stream.pipe(res);
});
});
app.post('/upi-pay', (req, res) => {
const { cost } = req.body;
upiqr({
payeeVPA: "lucky81205@okicici",
payeeName: "Rajiv Dubey",
amount: `${cost}`,
transactionNote: "BookHive"
})
.then(({ qr, intent }) => {
res.status(201).json({ img: `${qr}`, link: `${intent}` });
})
})
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});