-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
145 lines (135 loc) · 4.67 KB
/
api.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
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var db=require('./database.js');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//app.use('/static',express.static('static'));
//api to get leave application for employee and manager
app.get('/:username/',function(req,res){
//check whether the username exists in the database
db.user.findOne({username:req.params.username},function(err,doc){
if(err){
console.log(err);
res.json({code:-1,message:"Could not retrieve Leave Applications"});
}
//if not registered user, don't allow access
if(doc===null){
console.log("Unauthentic User");
res.json({code:-1,message:"Unauthentic Username!"});
}
//if registered user
else{
console.log(doc);
//if role is Employee, return the list of previous leave applications
if(doc.role=="Employee"){
db.leave.find({requestedBy:req.params.username},function(err,appl){
if(err){
console.log(err);
res.json({code:-1,message:"Could not retrieve Leave Applications"});
}
else {
if(appl.length>0){
console.log(appl);
res.json(appl);
}
else {
res.json({code:1,message:"No Leave Application to show!"});
}
}
});
}
//if role is Manager, return the entire list of leave applications
else if(doc.role=="Manager"){
//find all leave applications and sort in order of request date
db.leave.find().sort({requestedAt:-1}).exec(function(err,applications){
if(err){
console.log(err);
res.json({code:-1,message:"Could not retrieve leave application"});
}
else{
if(applications.length>0){
console.log(applications);
res.json(applications);
}
else {
res.json({code:1,message:"No Leave Application to show!"});
}
}
});
}
}
});
});
//api to post/submit leave application by employee
app.post('/:username',function(req,res){
//check whether the username exists in the database
db.user.findOne({username:req.params.username},function(err,doc){
if(err){
console.log(err);
res.json({code:-1,message:"Internal Error"});
}
//if not registered user, don't allow access
else if(doc===null){
console.log("Unauthentic User");
res.json({code:-1,message:"Unauthentic Username!"});
}
//if role is Manager, refrain from making POST request
else if(doc.role=="Manager"){
console.log("Not authorized");
res.json({code:-1,message:"Not authorized to make POST request"});
}
//if role is Employee, allow POST request
else{
console.log(req.body);
var newleave = new db.leave(req.body);
newleave.requestedBy=req.params.username;
newleave.requestedAt= new Date();
newleave.save(function(err){
if(err){
console.log(err);
res.json({code:-1,message:"Could not Post the leave Application"});
}
else {
console.log("Post Successful");
res.json({code:1,message:"Leave Application Posted Succesfully"})
}
});
}
});
});
//api to approve leave application for manager
app.put('/:username/:id',function(req,res){
//check whether the username exists in the database
db.user.findOne({username:req.params.username},function(err,doc){
if(err){
console.log(err);
res.json({code:-1,message:"Internal Error"});
}
//if not registered user, don't allow access
else if(doc===null){
console.log("Unauthentic User");
res.json({code:-1,message:"Unauthentic Username!"});
}
//if role is Employee, refrain from making PUT request
else if(doc.role=="Employee"){
console.log("Not authorized");
res.json({code:-1,message:"Not authorized to make PUT request"});
}
//if role is Manager, allow PUT request
else{
//update approvalStatus to true and approvedAt to Current date
db.leave.update({_id:req.params.id},{approvalStatus:true,approvedAt:new Date()},function(err){
if(err){
console.log(err);
res.json({code:-1,mesage:"Could not approve leave Application"});
}
else{
console.log("Put Successfuly");
res.json({code:1,message:"Approved Leave Application id: "+req.params.id});
}
});
}
});
});
module.exports=app;