-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (50 loc) · 1.97 KB
/
server.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
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');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});