forked from kanselarij-vlaanderen/uuid-generation-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
59 lines (49 loc) · 1.6 KB
/
app.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
import fs from 'fs';
import bodyParser from 'body-parser';
import { app, errorHandler } from 'mu';
import { filterDeltaForInsertedType } from './lib/delta';
import selectSubjectsWithoutUuid from './queries/select-without-uuid';
import insertUuid from './queries/insert-uuid';
const CONFIG = readConfig();
function readConfig() {
const configText = fs.readFileSync('/config/config.json', { encoding: 'utf8' });
const config = JSON.parse(configText);
return config;
}
app.post('/run', async (_req, res) => {
let uris = new Set();
for (let [type, graphs] of Object.entries(CONFIG)) {
const subjectsWithout = await selectSubjectsWithoutUuid(type, graphs);
if (subjectsWithout.length > 0) {
console.log(`Found ${subjectsWithout.length} objects of type <${type}> without a uuid. Handling now.`);
for (const subject of subjectsWithout) {
await insertUuid(subject, graphs);
uris.add(subject);
}
}
}
let ret = [];
uris.forEach((uri) => {
ret.push({
"uri": uri,
})
})
return res.status(200).send(JSON.stringify(
{
"data": ret
}
));
});
app.post('/delta', bodyParser.json(), async (req, res) => {
res.status(202).send();
for (let [type, graphs] of Object.entries(CONFIG)) {
const insertedSubjects = filterDeltaForInsertedType(req.body, type);
if (insertedSubjects.length > 0) {
console.log(`Received ${insertedSubjects.length} object inserts of type ${type} through deltas. Handling now.`);
for (const subject of insertedSubjects) {
await insertUuid(subject, graphs);
}
}
}
});
app.use(errorHandler);