Skip to content

Commit

Permalink
feat: synchronize with Intra API every 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Jun 5, 2024
1 parent f3daebe commit abcbd86
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/handlers/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const setupNunjucksFilters = function(app: Express): void {
else if (hours > 1) {
return `${hours} hours ago`;
}
else if (minutes > 15) { // > 15 because we synchronize every 15 minutes, otherwise we'll show "just now"
else if (minutes > 10) { // > 10 because we synchronize every 10 minutes, otherwise we'll show "just now"
return `${minutes} minutes ago`;
}
else {
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ app.use(async function(req: express.Request, res: express.Response, next: expres
setupExpressMiddleware(app);

// Set up all the routes/endpoints for Express
setupHomeRoutes(app);
setupHomeRoutes(app, prisma);
setupLoginRoutes(app);
setupUsersRoutes(app, prisma);
setupPiscinesRoutes(app, prisma);
Expand All @@ -71,6 +71,13 @@ app.listen(4000, async () => {

await syncWithIntra(api);
firstSyncComplete = true;

// Schedule a synchronization round every 10 minutes
setInterval(async () => {
console.log(`Synchronization with Intra started at ${new Date().toISOString()}`);
await syncWithIntra(api);
console.log(`Synchronization with Intra completed at ${new Date().toISOString()}`);
}, 10 * 60 * 1000);
}
catch (err) {
console.error('Failed to synchronize with the Intra API:', err);
Expand Down
14 changes: 11 additions & 3 deletions src/routes/home.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Express } from 'express';
import passport from 'passport';
import { PrismaClient } from '@prisma/client';

export const setupHomeRoutes = function(app: Express): void {
export const setupHomeRoutes = function(app: Express, prisma: PrismaClient): void {
app.get('/', passport.authenticate('session', {
keepSessionInfo: true,
}), (req, res) => {
return res.render('index.njk');
}), async (req, res) => {
// Fetch last synchronization times from the database
const syncTimes = await prisma.synchronization.findMany({
orderBy: [
{ kind: 'asc' },
],
});

return res.render('index.njk', { syncTimes });
});
};
6 changes: 6 additions & 0 deletions templates/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<li><a href="/users">List of all students/pisciners/staff</a></li>
<li><a href="/logout">Logout</a></li>
</ul>
<h4>Last synchronizations:</h4>
<ul style="font-size: small;">
{% for sync in syncTimes %}
<li>{{ sync.kind }} - {{ sync.last_synced_at }} ({{ sync.last_synced_at | timeAgo }})</li>
{% endfor %}
</ul>
{% endblock %}

0 comments on commit abcbd86

Please sign in to comment.