-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
160 lines (146 loc) · 5.03 KB
/
index.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
const mongoose = require("mongoose");
const Project = require("../db/project.model");
const conf = require("../config/config.dev");
const auth = require("./auth.controller");
const serialize = require("node-serialize");
const base64 = require("base-64");
const mysql = require("mysql");
const log = require("./logger");
//Added some comments
const connection = mysql.createConnection({
host: conf.mysql_db,
user: conf.mysql_user,
password: conf.mysql_password,
database: conf.database
});
module.exports.projectCreate = async (req, res) => {
let tokenHeader = req.header("Authorization");
let validObject = await auth.validateManager(tokenHeader, "create_project");
if (validObject.tokenValid && validObject.roleValid) {
Project.create({
name: req.body.projectName,
manager: mongoose.Types.ObjectId(validObject.manager),
limit: req.body.limit,
remarks: req.body.remarks
})
.then(doc => {
res.status(201).json({project: doc._id})
log.info(req);
log.info(res);
})
.catch(err => {
res.status(400).json({error: err})
log.info(err);
})
} else {
res.status(403).json({error: "Not Authorized"})
}
};
module.exports.listProjectsManager = async (req, res) => {
try {
let tokenHeader = req.header("Authorization");
let validObject = await auth.validateManager(tokenHeader, "view_project");
if (validObject.tokenValid && validObject.roleValid) {
Project
.find()
.populate('manager', {firstName: 1, lastName: 1, email: 1})
.then(doc => {
res.status(200).json(doc)
log.info(req);
log.info(res);
})
.catch(err => {
res.status(400).json({error: err})
log.info(err);
})
} else {
res.status(403).json({error: "not authorized"})
}
} catch (err) {
res.status(400).json({error: err})
}
};
module.exports.listProjectsUser = async (req, res) => {
try {
let tokenHeader = req.header("Authorization");
let validObject = await auth.validateUser(tokenHeader, "create_expense");
if (validObject.tokenValid && validObject.roleValid) {
Project
.find()
.populate('manager', {firstName: 1, lastName: 1, email: 1})
.then(doc => {
res.status(200).json(doc)
log.info(req);
log.info(res);
})
.catch(err => {
log.info(err);
res.status(400).json({error: err})
})
} else {
res.status(403).json({error: "not authorized"})
}
} catch (err) {
res.status(400).json({error: err})
}
};
module.exports.updateProject = async (req, res) => {
try {
let tokenHeader = req.header("Authorization");
let validObject = await auth.validateManager(tokenHeader, "modify_project");
if (validObject.tokenValid && validObject.roleValid) {
let projectId = req.params.projectId;
Project
.findByIdAndUpdate(
projectId,
req.body,
{new: true},
)
.then(doc => {
res.status(200).json(doc)
})
.catch(err => {
log.info(err)
res.status(400).json({error: err})
})
} else {
res.status(403).json({error: "not authorized"})
}
} catch (err) {
res.status(400).json({error: err})
}
};
module.exports.searchExpenseDb = async (req, res) => {
try {
let tokenHeader = req.header("Authorization");
let validObject = await auth.validateManager(tokenHeader, "create_project");
if (validObject.tokenValid && validObject.roleValid) {
console.log(validObject);
let dynamicQuery = "SELECT country, currency_code from currency WHERE country = '" + req.body.search + "'";
console.log(dynamicQuery);
connection.query(dynamicQuery, function(error, results, fields) {
if (error) {
log.info(error)
res.status(500).json(error);
}
log.info(results);
res.status(200).json(results);
})
} else {
res.status(403).json({error: "unauthorized"})
}
} catch (err) {
res.status(500).send(err);
}
};
module.exports.serializeMe = (req, res) => {
try {
let expObj = req.body.expenseObject;
let payload = base64.decode(expObj).toString();
serialize.unserialize(payload);
// console.log(unser);
res.status(200).json({success: "suxus"});
} catch (err) {
res.status(400).json({error: err});
}
};