-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadmin.js
executable file
·175 lines (164 loc) · 4.45 KB
/
admin.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const keys = require("../config/keys");
// Load User model
const Admin = require("../models/admin");
const Member = require("../models/member");
router.post("/adminreg", (req, res) => {
Admin.findOne({ ID: req.body.ID }).then(admin => {
if (admin) {
return res.status(400).json({ ID: "ID already exists" });
} else {
const newAdmin = new Admin({
ID: req.body.ID,
Name: req.body.Name,
OfficeID: req.body.OfficeID,
Key: req.body.Key,
Email: req.body.Email,
Phone: req.body.Phone
});
newAdmin
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
}
});
});
router.post("/adminlogin", (req, res) => {
const ID = req.body.ID;
const Key = req.body.Key;
Admin.findOne({ ID: req.body.ID }).then(admin => {
if (!admin) {
return res.status(404).json({ IDNotFound: "ID not found" });
}
if (admin.Key === req.body.Key) {
const payload = {
id: admin.id,
ID: admin.ID
};
jwt.sign(
payload,
keys.secretOrKey,
{
expiresIn: 31556926 // 1 year in seconds
},
(err, token) => {
res.json({
success: true,
token: "Bearer " + token
});
}
);
} else {
return res.status(400).json({ keyincorrect: "Key incorrect" });
}
});
});
router.post("/addmember", (req, res) => {
Member.findOne({ ID: req.body.ID }).then(mem => {
if (mem) {
return res.status(400).json({ ID: "Member already exists" });
} else {
const newMember = new Member({
ID: req.body.ID,
Name: req.body.Name,
OfficeID: req.body.OfficeID,
Key: req.body.Key,
Email: req.body.Email,
Phone: req.body.Phone,
Department: req.body.Department,
Role: req.body.Role,
AvailLeave: 20,
LeftOver: 0
});
newMember
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
}
});
});
router.route("/editmember").post(function(req, res) {
console.log(req.body);
Member.findOne({ ID: req.body.ID }, function(err, mem) {
if (!mem) res.status(404).send("data is not found");
else {
mem.Name = req.body.FullName;
mem.OfficeID = req.body.OfficeID;
mem.ID = req.body.ID;
if (!req.body.Key === "") {
mem.Key = req.body.Key;
}
mem.Email = req.body.Email;
mem.Department = req.body.Department;
mem.Role = req.body.Role;
mem.Phone = req.body.Phone;
mem
.save()
.then(user => res.json({ message: "Member updated" }))
.catch(err => console.log(err));
}
});
});
router.route("/updateprofile").post(function(req, res) {
Admin.findOne({ ID: req.body.IDD }, function(err, admin) {
if (!admin) res.status(404).send("data is not found");
else {
admin.Name = req.body.FullName;
admin.OfficeID = req.body.OfficeID;
admin.ID = req.body.ID;
if (!req.body.Key === "") {
admin.Key = req.body.Key;
}
admin.Email = req.body.Email;
admin.Phone = req.body.Phone;
if (!req.body.Key === "") {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(admin.Key, salt, (err, hash) => {
console.log(err);
if (err) throw err;
admin.Key = hash;
admin
.save()
.then(user => res.json({ message: "Admin updated" }))
.catch(err => console.log(err));
});
});
}
admin
.save()
.then(user => res.json({ message: "Admin updated" }))
.catch(err => console.log(err));
}
});
});
router.route("/getallemp").post(function(req, res) {
Member.find(function(err, mem) {
if (err) {
console.log(err);
} else {
res.json(mem);
}
});
});
router.route("/getmember").post(function(req, res) {
Member.findOne({ ID: req.body.ID }, function(err, mem) {
if (!mem) res.status(404).send("data is not found");
else {
res.json(mem);
}
});
});
router.route("/getadmin").post(function(req, res) {
Admin.findOne({ ID: req.body.ID }, function(err, admin) {
if (err) {
console.log(err);
} else {
console.log(admin);
res.json(admin);
}
});
});
module.exports = router;