-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (54 loc) · 1.35 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
60
61
62
63
64
import { app, errorHandler, uuid } from 'mu';
import { querySudo as query } from '@lblod/mu-auth-sudo';
import { waitForDatabase } from './support/database-utils';
let sequenceNumber = 0;
waitForDatabase(async () => {
await initializeSequenceNumber();
});
app.post('/sequence-numbers', function(req, res) {
if (sequenceNumber > 0)
sequenceNumber = sequenceNumber + 1;
res.status(200).send({
data: {
type: 'sequence-number',
id: uuid(),
attributes: {
value: sequenceNumber
}
}
});
});
app.post('/reset', async function(req, res) {
await initializeSequenceNumber();
res.status(204).end();
});
app.get('/cache', function(req, res) {
res.status(200).send({
data: {
type: 'sequence-number',
id: uuid(),
attributes: {
value: sequenceNumber
}
}
});
});
async function initializeSequenceNumber() {
const result = await query(`
PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?number
WHERE {
GRAPH ?g {
?product a gr:SomeItems ;
dct:identifier ?number .
}
} ORDER BY DESC(?number) LIMIT 1
`);
if (result.results.bindings.length) {
sequenceNumber = parseInt(result.results.bindings[0]['number'].value);
} else {
sequenceNumber = 0;
}
}
app.use(errorHandler);