-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
450 lines (375 loc) · 14.2 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
const http = require('http');
const mysql = require('mysql');
const { version } = require('os');
const { parseArgs } = require('util');
// MySQL database configuration
const dbConfig = {
host: '127.0.0.1',
user: 'root',
password: '1234',
database: 'pwb'
};
const hostname = '127.0.0.1';
const port = 3000;
// Function to authenticate the user against the MySQL database
function authenticateUser(username, password, callback) {
const connection = mysql.createConnection(dbConfig);
connection.connect((err) => {
if (err) {
console.log(err);
callback(err, false);
return;
}
const query = `SELECT * FROM users WHERE username = '${username}' AND password = '${password}'`;
connection.query(query, (error, results) => {
connection.end(); // Close the connection to the database
if (error) {
console.log(error);
callback(error, false);
return;
}
if (results.length > 0) {
callback(null, true);
} else {
callback(null, false);
}
});
});
}
function addProject(project, callback) {
const connection = mysql.createConnection(dbConfig);
connection.connect(async(err) => {
if (err) {
callback(err);
return;
}
// create an variable to store the Id project.TeamlLead
var teamLeadID;
// get the Id project.TeamlLead from Users
const query = `SELECT * FROM users WHERE FullName = '${project.TeamLead}'`;
// create an async function to get the Id project.TeamlLead from Users
const getTeamLeadID = async () => {
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) {
console.log(error);
reject(error);
}
if (results.length > 0) {
resolve(results[0].UserID);
} else {
reject('Team Lead not found');
}
});
});
};
// get the Id project.TeamlLead from Users
teamLeadID = await getTeamLeadID();
console.log(teamLeadID);
const insertQuery = `INSERT INTO projects (ProjectID, ProjectName, ProjectDescription, TeamLead) VALUES (${project.ProjectID}, '${project.ProjectName}', '${project.ProjectDescription}', ${teamLeadID})`;
connection.query(insertQuery, (err, result) => {
connection.end(); // Close the connection to the database
if (err) {
console.log(err);
callback(err);
return;
}
callback(null);
});
});
}
function addVersion(project, callback) {
const connection = mysql.createConnection(dbConfig);
connection.connect(async(err) => {
if (err) {
callback(err);
return;
}
// create an variable to store the Id project.TeamlLead
var projectName;
console.log(project.ProjectID);
// get the Id project.TeamlLead from Users
const query = `SELECT * FROM projects WHERE ProjectName = '${project.ProjectID}'`;
// create an async function to get the Id project.TeamlLead from Users
const getTeamLeadID = async () => {
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) {
console.log(error);
reject(error);
}
if (results.length > 0) {
// console.log(results[0].UserID);
// console.log(results);
resolve(results[0].ProjectID);
} else {
reject('Team Lead not found');
}
});
});
};
// get the Id project.TeamlLead from Users
projectName = await getTeamLeadID();
console.log(projectName);
// callback(null);
const insertQuery = `INSERT INTO Versions (VersionID, ProjectID, VersionName) VALUES (${project.VersionID}, '${projectName}', '${project.VersionName}')`;
connection.query(insertQuery, (err, result) => {
connection.end(); // Close the connection to the database
if (err) {
console.log(err);
callback(err);
return;
}
callback(null);
});
});
}
function addIssue(project, callback) {
const connection = mysql.createConnection(dbConfig);
connection.connect(async(err) => {
if (err) {
callback(err);
return;
}
// create an variable to store the Id project.TeamlLead
var projectName;
var versionName;
var username;
// console.log(project.ProjectID);
// get the Id project.TeamlLead from Users
const query = `SELECT * FROM projects WHERE ProjectName = '${project.ProjectID}'`;
// create an async function to get the Id project.TeamlLead from Users
const getProjectID = async () => {
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) {
console.log(error);
reject(error);
}
if (results.length > 0) {
resolve(results[0].ProjectID);
} else {
reject('Team Lead not found');
}
});
});
};
const query2 = `SELECT * FROM versions WHERE VersionName = '${project.VersionID}'`;
// create an async function to get the Id project.TeamlLead from Users
const getVersionID = async () => {
return new Promise((resolve, reject) => {
connection.query(query2, (error, results) => {
if (error) {
console.log(error);
reject(error);
}
if (results.length > 0) {
// console.log(results);
resolve(results[0].VersionID);
} else {
reject('Team Lead not found');
}
});
});
};
const query3 = `SELECT * FROM users WHERE FullName = '${project.assignedTo}'`;
// create an async function to get the Id project.TeamlLead from Users
const getUsername = async () => {
return new Promise((resolve, reject) => {
connection.query(query3, (error, results) => {
if (error) {
console.log(error);
reject(error);
}
if (results.length > 0) {
// console.log(results);
resolve(results[0].UserID);
} else {
reject('Team Lead not found');
}
});
});
};
// get the Id project.TeamlLead from Users
projectName = await getProjectID();
versionName = await getVersionID();
username = await getUsername();
// console.log(username);
// callback(null);
const insertQuery = `INSERT INTO issues (IssueID, ProjectID, VersionID, IssueType, Summary, DetailedDescription, assignedTo, Status, Priority, DueDate, LastCommentID) VALUES (${project.IssueID}, '${projectName}', '${versionName}', '${project.IssueType}','${project.Summary}', '${project.DetailedDescription}', '${username}', '${project.Status}', '${project.Priority}',STR_TO_DATE('${project.DueDate}', '%m/%d/%Y'), '${project.LastCommentID}' )`;
connection.query(insertQuery, (err, result) => {
connection.end(); // Close the connection to the database
if (err) {
console.log(err);
callback(err);
return;
}
callback(null);
});
});
}
const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/login') {
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
const formData = new URLSearchParams(body);
const username = formData.get('username');
const password = formData.get('password');
// log console data
console.log(username);
if (username && password) {
authenticateUser(username, password, (error, isAuthenticated) => {
if (error) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error');
return;
}
if (isAuthenticated) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Authentication successful');
} else {
res.statusCode = 401;
res.setHeader('Content-Type', 'text/plain');
res.end('Invalid username or password');
}
});
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Username and password are required');
}
});
} else if (req.method === 'POST' && req.url === '/addProject'){
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
const formData = new URLSearchParams(body);
const projectID = formData.get('ProjectID');
const projectName = formData.get('ProjectName');
const projectDescription = formData.get('ProjectDescription');
const teamLead = formData.get('TeamLead');
if (projectID && projectName && teamLead) {
const project = {
ProjectID: parseInt(projectID),
ProjectName: projectName,
ProjectDescription: projectDescription || '',
TeamLead: teamLead
};
addProject(project, (error) => {
if (error) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error');
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Project added successfully');
});
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('ProjectID, ProjectName, and TeamLead are required');
}
});
}else if (req.method === 'POST' && req.url === '/addVersion'){
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
const formData = new URLSearchParams(body);
const projectID = formData.get('ProjectID');
const VersionID = formData.get('VersionID');
const VersionName = formData.get('VersionName');
if (projectID && VersionID && VersionName) {
const version = {
ProjectID: projectID,
VersionID: parseInt(VersionID),
VersionName: VersionName
};
console.log(version);
addVersion(version, (error) => {
if (error) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error');
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Project Version added successfully');
});
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('ProjectID, VersionID, and VersionName are required');
}
});
}else if (req.method === 'POST' && req.url === '/addIssue'){
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
const formData = new URLSearchParams(body);
const IssueID = formData.get('IssueID');
const ProjectID = formData.get('ProjectID');
const VersionID = formData.get('VersionID');
const IssueType = formData.get('IssueType');
const Summary = formData.get('Summary');
const DetailedDescription = formData.get('DetailedDescription');
const AssignedTo = formData.get('AssignedTo');
const Status = formData.get('Status');
const Priority = formData.get('Priority');
const DueDate = formData.get('DueDate');
const LastCommentID = formData.get('LastCommentID');
if (IssueID && ProjectID && IssueType && Summary && Status && Priority) {
const issue = {
IssueID: parseInt(IssueID),
ProjectID: ProjectID,
VersionID: VersionID,
IssueType: IssueType,
Summary: Summary,
DetailedDescription: DetailedDescription || '',
assignedTo: AssignedTo,
Status: Status,
Priority: Priority,
// DueDate: parse as date
DueDate: DueDate ,
LastCommentID: parseInt(LastCommentID)
};
console.log(issue);
addIssue(issue, (error) => {
if (error) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error');
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Issue added successfully');
});
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('ProjectID, VersionID, and VersionName are required');
}
});
}else{
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not found');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});