-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (83 loc) · 2.71 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
const https = require('https');
const fs = require('fs');
const fetch = require('node-fetch');
const url = "https://interview.adpeai.com/api/v2/get-task";
let data = ''
const PORT = process.env.port || 3000;
const _options = {
key: fs.readFileSync('localhost-key.pem'),
cert: fs.readFileSync('localhost.pem')
};
// This creates a localhost server
https.createServer(_options, function (postRequest, res) {
res.write('Hello World!');
res.end();
}).listen(PORT, function(){
console.log("server start at port "+PORT);
});
// Get Request
fetch(url)
.then(res => res.text())
.then(text => {
let temp = '';
let previousYear = new Date().getFullYear() - 1;
let employees = {}
let transactions = []
let _transactions = ''
let max = -Infinity;
temp = JSON.parse(text);
// Filters based on previous year
temp.transactions.filter(obj => {
if ((new Date(obj.timeStamp)).getFullYear() == previousYear) {
if (obj.employee.id in employees) {
employees[obj.employee.id].push(obj)
} else {
employees[obj.employee.id] = [obj]
}
}
return employees;
});
// Calculates the total of amount
for (const i in employees) {
employees[i].total = 0
employees[i].filter(obj => {
employees[i].total += obj.amount
})
}
// Get the Highest Value of the amount in the Employees
for (const i in employees) {
if(employees[i].total > max){
max = employees[i].total
}
}
// Filters transaction ID for alpha type
for(const i in employees){
if(employees[i].total == max){
_transactions = employees[i].filter(obj => {
if(obj.type == "alpha"){
return transactions.push(obj.transactionID)
}
})
}
}
const results = {
"id": temp.id,
"result": transactions
}
data = JSON.stringify(results)
console.log(data)
// POST Request
fetch('https://interview.adpeai.com/api/v2/submit-task', {
method: 'POST',
body: data,
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(res =>{
console.log('POST Status Code:', res.status);
JSON.stringify(res)})
.then(json => console.log(json))
.catch (err => console.log(err, 'err'))
});