-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource.js
75 lines (62 loc) · 2.05 KB
/
source.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
let drug = args[0];
let result = [];
async function makeApiRequest(url) {
try {
const response = await Functions.makeHttpRequest({
url,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
params: {
drug_details: drug,
},
});
if (response.error) {
console.error(`API request error for ${url}:`, response.message);
return [];
}
return response.data.brands || [];
} catch (error) {
console.error(`Error making API request for ${url}:`, error);
return [];
}
}
try {
const watsonsUrl = "https://l3h3rjunf1.execute-api.ap-southeast-1.amazonaws.com/dev/v1/ph/watsons/2023";
const mercuryUrl = "https://l3h3rjunf1.execute-api.ap-southeast-1.amazonaws.com/dev/v1/ph/mercury/2023";
const watsonsData = await makeApiRequest(watsonsUrl);
const mercuryData = await makeApiRequest(mercuryUrl);
result.push(...watsonsData, ...mercuryData);
if (result.length > 0) {
let groups = {};
for (let i = 0; i < result.length; i++) {
let key = `${result[i].code}`;
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(result[i]);
}
let lowestPrices = [];
let highestPrices = [];
for (const key in groups) {
let group = groups[key];
let lowestPrice = Math.min(...group.map(item => item.discounted_price));
let highestPrice = Math.max(...group.map(item => item.price));
lowestPrices.push(lowestPrice);
highestPrices.push(highestPrice);
groups[key] = {
lowestPrice: parseFloat(lowestPrice.toFixed(2)),
highestPrice: parseFloat(highestPrice.toFixed(2)),
};
}
const y = drug;
let min = parseFloat(Math.min(...lowestPrices).toFixed(2));
let max = parseFloat(Math.max(...highestPrices).toFixed(2));
let med = parseFloat(((min + max) / 2).toFixed(2));
let finalOutput = { drug: y, min, med, max };
return Functions.encodeString(JSON.stringify(finalOutput));
}
} catch (error) {
console.error("Error processing data:", error);
}