-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRoutes.js
151 lines (134 loc) · 4.16 KB
/
userRoutes.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
149
150
151
const express = require('express');
const bcrypt = require('bcrypt')
const router = express.Router();
const { User } = require('./model');
const { route } = require('./leadRoutes');
const {verifyToken, verifyAdmin} = require('./middleware')
require('dotenv').config();
const saltRounds = 10
// DEVELOPER GET ROUTE
router.get('/developer',verifyToken, verifyAdmin, async(req, res) => {
try {
const result = await User.find({role:{$in:['Developer', 'NewUser']}}).sort({role:1})
res.send(result)
} catch (error) {
console.log('Error in getting developer user', error)
res.status(500).send('Internal Server Error')
}
})
// LEAD COLLECTOR AND CALLER GET ROUTE
router.get('/allUsers', verifyToken, verifyAdmin, async(req, res) => {
console.log('user route hit')
try{
const result = await User.find({role:{$nin:['marketingAdmin', 'developmentAdmin', 'Developer']}}, {password: 0, __v:0}).sort({role:-1})
res.send(result)
}catch(error){
console.log('Error fetching data', error.message)
res.status(500).send("Internal server error")
}
})
// SINGLE USER GET ROUTE
router.get('/userLogin', async (req, res) => {
try {
const email = req.query.email;
const userPassword = req.query.password
const userExist = await User.findOne({email:email})
if(userExist){
const dbPassword = userExist.password
const passwordMatch = await bcrypt.compare(userPassword, dbPassword)
if(passwordMatch){
console.log('Password Matched')
const userInfo = {
name: userExist.name,
email: userExist.email,
role: userExist.role,
}
res.status(200).json({message: 'Login Successful', userInfo})
}else {
console.log('Incorrect Password')
res.status(200).send('Incorrect Password')
}
}else{
res.status(202).send('Could not find the email. Please provide correct email or register')
}
} catch (error) {
console.error('Error fetching leads:', error.message);
res.status(500).send('Internal Server Error');
}
});
// FOR ADD LEAD POST ROUTE
router.post('/addUser', async (req, res) => {
try {
const user = req.body;
console.log('user', user)
const findUser = await User.findOne({email:user.email})
console.log(findUser)
if(!findUser){
const plainPassword = user.password;
console.log(plainPassword)
const hashPassword = async (plainPassword) => {
try {
const salt = await bcrypt.genSalt(saltRounds);
const hash = await bcrypt.hash(plainPassword, salt);
return hash;
} catch (error) {
throw error;
}
};
const hashedPassword = await hashPassword(plainPassword)
console.log(hashedPassword)
user.password = hashedPassword
userData = {...user, role:'NewUser'}
saveUser = new User(userData)
const result = await saveUser.save();
console.log(result)
res.status(201).json({
message: "Registration Successful",
user: result,
});
}else{
res.send('User exist')
}
} catch (error) {
console.error('Error adding user:', error.message);
res.status(500).send('Internal Server Error');
}
});
// USER ROLE CHANGE PATCH ROUTE
router.patch('/userRoleChange/:id', async(req, res) =>{
try {
const id = req.params.id;
const newRole = req.body
const result = await User.updateOne(
{_id:id},
{
$set:newRole
}
)
res.send(result)
} catch (error) {
console.error('Error updating role:', error.message);
res.status(500).send('Internal Server Error')
}
})
//--- CALLER INFO GET ROUTE
router.get('/callerInfo', async(req, res) => {
try {
const result = await User.find({role: 'Caller'})
res.send(result)
} catch (error) {
console.log('Error in fetching caller info', error)
res.status(500).send('Internal Server Error')
}
})
//--- DEVELOPER INFO GET ROUTE
router.get('/developerInfo', async(req, res) => {
try {
const result = await User.find({role: 'Developer'})
res.send(result)
} catch (error) {
console.log('Error in fetching caller info', error)
res.status(500).send('Internal Server Error')
}
})
module.exports = router;