-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
271 lines (232 loc) · 7.72 KB
/
app.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
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
const dbConnection = require('./database/dbConnection');
const generateToken = require('./util/generateToken')
//NEED TO ADD RANK TO USERS
//NEED TO ADD DUE DATE AND COMPLETION STATUS TO THE TASKS
const knex = require("knex");
const express = require("express");
const morgan = require("morgan");
const app = express();
const cors = require('cors');
//getPasswordHash, getAll, getAllTasks, getPrivId, createUser, updateTask, postTask, getTaskById, removeTask, getAllTasks, updateUser, removeUser, addUser
//Page imports
const { getPasswordHash, getAll, getSpecificItem, addTask, deleteItem, updateTask } = require("./database/controllers");
//middleware
app.use(morgan("tiny"));
app.use(express.json());
app.use(cors());
//HASHING
const bcrypt = require ('bcryptjs');
//CREATE (POST)
// Register
app.post('/register', async (req, res) => {
try{
const { work_email, password, rank, first_name, last_name, role,} = req.body;
const hashed_password = await bcrypt.hash(password, 12);
await dbConnection.insert({ work_email: work_email, hashed_password: hashed_password, rank: rank, first_name: first_name, last_name: last_name, role: role}).from('users');
res.status(201).json({
work_email: work_email,
role:role,
token:generateToken(work_email)
});
} catch(e) {
console.log(e)
res.status(500).send('Something Went Wrong');
}
});
// login
app.post('/users/login', async(req, res) => {
try{
const {work_email, password} = req.body;
user = await dbConnection.select('*').where({work_email: work_email}).from('users')
if (user.length){
console.log(user)
const validPass = await bcrypt.compare(password, user[0].hashed_password);
if(validPass) {
res.status(201).json({
id: user[0].id,
rank: user[0].rank,
lastName: user[0].last_name,
workEmail: user[0].work_email,
role: user[0].role,
sponsorID: user[0].sponsor_id,
token:generateToken(work_email)
});
}
else{
res.status(404).res.json('Wrong Password')
}
}
else {
res.status(404).json('User not found')
}
}
catch(e) {
console.log(e)
res.status(500).send('Something Went Wrong');
}
});
app.post("/tasks", (req, res) => {
const u_id = req.body.user_id ? req.body.user_id : '';
const newTask = req.body.task ? req.body.task : '';
console.log('post log', req.body)
addTask(u_id, newTask)
.then((data) =>
res.status(200).json({message: `The task was posted successfully`})
)
.catch((err) =>
res.status(422).json({message: `Failure to post task. ERROR: ${err}`})
)
})
//getting all tasks for an inbound user
app.get('/tasks/user/:id', function(req, res){
dbConnection
.select('*')
.from('tasks')
.where({user_id: req.params.id})
.then(data => res.status(200).json(data))
.catch(err =>
res.status(404).json({
message:
'The data you are looking for could not be found. Please try again'
}))
});
//getting the sponsor info for user
app.get('/sponsor/:id', function(req, res){
dbConnection
.select('*')
.where({id: req.params.id})
.from('users')
.then(data => res.status(200).json(data))
.catch(err =>
res.status(404).json({
message:
'The data you are looking for could not be found. Please try again'
}))
});
//getting all inbounds for one sponsor
app.get('/sponsor/user/:id', function(req, res){
dbConnection
.select('*')
.where({sponsor_id: req.params.id})
.from('users')
.then(data => res.status(200).json(data))
.catch(err =>
res.status(404).json({
message:
'The data you are looking for could not be found. Please try again'
}))
});
//adding a new task to an inbound as a sponsor
app.post('/sponsor/addtask', function(req, res){
dbConnection
.insert({ user_id: req.body.user_id, task: req.body.task, due_date: req.body.due_date, task_status: req.body.task_status }).from('tasks')
.then((data) => res.status(201).json(data))
.catch((err) => {
// console.error(err);
res.status(404).json({ message: "Something is wrong."})
})
});
//adding/changing an inbound's sponsor id
app.patch('/admin/sponsorid', function(req, res){
dbConnection
.update({sponsor_id: req.body.sponsor_id})
.where({id: req.body.id})
.from('users')
.then((data) => res.status(201).json(data))
.catch((err) => {
// console.error(err);
res.status(404).json({ message: "Something is wrong."})
})
});
app.get('/admin/getsponsors', function(req, res){
dbConnection
.select('*')
.where({role: 'sponsor'})
.from('users')
.then(data => res.status(200).json(data))
.catch((err) => {
// console.error(err);
res.status(404).json({ message: "Something is wrong."})
})
});
//getting the sponsor for a selected user in ViewChecklist
app.get('/users/sponsor/:id', function(req, res){
dbConnection
.select('*')
.where({id: req.params.id})
.from('users')
.then(data => res.status(200).json(data))
.catch((err) => {
// console.error(err);
res.status(404).json({ message: "Something is wrong."})
})
});
// READ (GET)
//Get by specific ID g2g
// app.get("/:table/:id", (req, res) => {
// getSpecificItem(req.params.table, req.params.id)
// .then((data) => res.send(data))
// .catch((err) =>
// res.status(404).json({message: `${req.params.table} does not exist.`})
// )
// })
// //getting specific user by ID
// app.get('/posts/users/:user_id', function(req, res) {
// dbConnection
// getAllTasks(req.params.user_id)
// .then(data => res.status(200).json(data))
// .catch(err =>
// res.status(404).json({
// message:
// 'The data you are looking for could not be found. Please try again'
// })
// );
// });
// //get by id
// app.get('/:table/:id', function(req, res) {
// dbConnection
// .select('*')
// .from(req.params.table)
// .where({id: req.params.id})
// .then(data => res.status(200).json(data))
// .catch(err =>
// res.status(404).json({
// message:
// 'The data you are looking for could not be found. Please try again'
// })
// );
// });
//UPDATE (PUT, PATCH)
app.patch('/tasks/:id', function(req, res) {
let {user_id, task} = req.body;
// dbConnection
// ('tasks').where({ id: req.params.id}).update({user_id: req.body.user_id, task: req.body.task})
console.log(req.body)
updateTask(req.params.id, req.params.body)
.then((data) => res.status(200).json(data))
.catch((err) => {
console.error(err);
res.status(404).json({ message: "Something is wrong." })
})
});
//DELETE by ID g2g
app.delete("/:table/:id", (req, res) => {
deleteItem(req.params.table, req.params.id)
.then((data) =>
res.status(200).json({message: `Deleted successfully`})
)
.catch((err) =>
res.status(404).json({message: `${req.params.table} does not exist.`})
)
})
// LIST (GET) g2g
app.get("/:table", (req, res) => {
console.log('asdfasdf')
getAll(req.params.table)
.then((data) => res.send(data))
.catch((err) =>
// res.status(404).json({ message: `${req.params.table} does not exist.` })
res.status(404).json({message: `Failure to post data. ERROR: ${err}`})
);
});
module.exports = app;