-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.js
188 lines (152 loc) · 5.68 KB
/
server1.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
const express = require('express');
const cors = require('cors');
const axios = require('axios'); // Use Axios for HTTP requests
const app = express();
const port = 3001; // Port for the server
// Apply CORS middleware
app.use(cors());
// Middleware to parse JSON request bodies
app.use(express.json());
// Route to fetch organisation units
app.post('/organisationUnits', async (req, res) => {
const { location } = req.body; // Extract location from request body
if (!location) {
return res.status(400).send('Location in the request body is required');
}
try {
const response = await axios.get(`https://ovckla.org/ovc/api/organisationUnits.json?filter=displayName:ilike:${location}`, {
headers: {
'Authorization': 'Basic ' + Buffer.from('Skununka:Nomisr123$$$$').toString('base64'),
'Content-Type': 'application/json'
}
});
res.json(response.data); // Send the data to the client
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
// Route to fetch tracked entity instances
app.post('/trackedEntityInstances', async (req, res) => {
const { orgUnitId } = req.body; // Extract orgUnitId from request body
if (!orgUnitId) {
return res.status(400).send('orgUnitId in the request body is required');
}
try {
const response = await axios.get(`https://ovckla.org/ovc/api/trackedEntityInstances/query.json?ou=${orgUnitId}&ouMode=SELECTED&&order=created:desc&program=IXxHJADVCkb&pageSize=50&page=1&totalPages=false&fields=*`, {
headers: {
'Authorization': 'Basic ' + Buffer.from('Skununka:Nomisr123$$$$').toString('base64'),
'Content-Type': 'application/json'
}
});
res.json(response.data); // Send the data to the client
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
// Route to fetch GroupActivity data
app.post('/groupActivity', async (req, res) => {
const { instanceId } = req.body; // Extract instanceId from request body
if (!instanceId) {
return res.status(400).send('instanceId in the request body is required');
}
try {
const response = await axios.get(`https://ovckla.org/ovc/api/trackedEntityInstances/${instanceId}.json?program=IXxHJADVCkb&fields=*`, {
headers: {
'Authorization': 'Basic ' + Buffer.from('Skununka:Nomisr123$$$$').toString('base64'),
'Content-Type': 'application/json'
}
});
const enrollments = response.data.enrollments;
const result = [];
enrollments.forEach(enrollment => {
const events = enrollment.events.filter(event => event.programStage === 'aTZwDRoJnxj');
events.forEach(event => {
const dataValues = event.dataValues;
const record = {
Name: dataValues[0]?.value,
Code: dataValues[1]?.value,
BeneficiaryType: dataValues[2]?.value,
Age: dataValues[3]?.value,
Sex: dataValues[4]?.value
};
result.push(record);
});
});
res.json(result);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
app.get('/groupActivity', async (req, res) => {
const { instanceId } = req.query; // Extract instanceId from request body
if (!instanceId) {
return res.status(400).send('instanceId in the request body is required');
}
try {
const response = await axios.get(`https://ovckla.org/ovc/api/trackedEntityInstances/${instanceId}.json?program=IXxHJADVCkb&fields=*`, {
headers: {
'Authorization': 'Basic ' + Buffer.from('Skununka:Nomisr123$$$$').toString('base64'),
'Content-Type': 'application/json'
}
});
const enrollments = response.data.enrollments;
const result = [];
enrollments.forEach(enrollment => {
const events = enrollment.events.filter(event => event.programStage === 'aTZwDRoJnxj');
events.forEach(event => {
const dataValues = event.dataValues;
const record = {
Name: dataValues[0]?.value,
Code: dataValues[1]?.value,
BeneficiaryType: dataValues[2]?.value,
Age: dataValues[3]?.value,
Sex: dataValues[4]?.value
};
result.push(record);
});
});
res.json(result);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
// Route to fetch Sessions data
app.get('/sessions', async (req, res) => {
const { instanceId } = req.query; // Extract instanceId from request body
if (!instanceId) {
return res.status(400).send('instanceId in the request body is required');
}
try {
const response = await axios.get(`https://ovckla.org/ovc/api/trackedEntityInstances/${instanceId}.json?program=IXxHJADVCkb&fields=*`, {
headers: {
'Authorization': 'Basic ' + Buffer.from('Skununka:Nomisr123$$$$').toString('base64'),
'Content-Type': 'application/json'
}
});
const enrollments = response.data.enrollments;
const result = [];
enrollments.forEach(enrollment => {
const events = enrollment.events.filter(event => event.programStage === 'VzkQBBglj3O');
events.forEach(event => {
const dataValues = event.dataValues;
const record = {
Code: dataValues[0]?.value,
Session: dataValues[1]?.value,
SessionDate: dataValues[2]?.value
};
result.push(record);
});
});
res.json(result);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});