Skip to content

Commit

Permalink
LMB-339: add cronjob that fetches the efffectieve mandatarissen every…
Browse files Browse the repository at this point in the history
… minute
  • Loading branch information
JonasVanHoof committed Sep 24, 2024
1 parent 6630ed3 commit 7a6b165
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
4 changes: 4 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { burgemeesterRouter } from './routes/burgemeester-benoeming';
import { installatievergaderingRouter } from './routes/intallatievergadering';
import { mandatenRouter } from './routes/mandaten';

import { cronjob as notificationBekrachtigdMandataris } from './cron/notification-for-bekrachtigde-mandataris';

app.use(
bodyParser.json({
limit: '500mb',
Expand Down Expand Up @@ -43,3 +45,5 @@ const errorHandler: ErrorRequestHandler = function (err, _req, res, _next) {
};

app.use(errorHandler);

setTimeout(() => notificationBekrachtigdMandataris.start(), 10000);
49 changes: 49 additions & 0 deletions cron/notification-for-bekrachtigde-mandataris.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CronJob } from 'cron';

import { querySudo } from '@lblod/mu-auth-sudo';
import { getSparqlResults } from '../util/sparql-result';

const NOTIFICATION_CRON_PATTERN = '* * * * *';
let running = false;

export const cronjob = CronJob.from({
cronTime: NOTIFICATION_CRON_PATTERN,
onTick: async () => {
if (running) {
return;
}
running = true;
await HandleEffectieveMandatarissen();
running = false;
},
});

async function HandleEffectieveMandatarissen() {
await fetchMandatarissen();
running = false;
}

async function fetchMandatarissen() {
const query = `
PREFIX mandaat: <http://data.vlaanderen.be/ns/mandaat#>
SELECT DISTINCT ?mandataris
WHERE {
GRAPH ?graph {
?mandataris a mandaat:Mandataris;
mandaat:status <http://data.vlaanderen.be/id/concept/MandatarisStatusCode/21063a5b-912c-4241-841c-cc7fb3c73e75>.
}
FILTER NOT EXISTS {
?graph a <http://mu.semte.ch/vocabularies/ext/FormHistory>
}
FILTER NOT EXISTS {
?graph a <http://mu.semte.ch/graphs/public>
}
}
`;

const sparqlResult = await querySudo(query);
const results = getSparqlResults(sparqlResult);

console.log('LOG: results', results.length);
}
38 changes: 22 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"@lblod/mu-auth-sudo": "^0.6.1",
"body-parser": "^1.19.0",
"cron": "^1.8.2",
"cron": "^3.1.7",
"crypto-js": "^4.0.0",
"csv-parse": "^5.5.6",
"express-promise-router": "^4.1.1",
Expand Down

0 comments on commit 7a6b165

Please sign in to comment.