Implemented session status.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-06-20 00:33:51 +01:00
parent b4b87380d2
commit ed99638664
5 changed files with 85 additions and 4 deletions

View File

@ -57,6 +57,7 @@ import {
odataRoutes, odataRoutes,
excelRoutes, excelRoutes,
csvRoutes, csvRoutes,
appLaunchRoutes,
} from './routes/index.js'; } from './routes/index.js';
import path from 'path'; import path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
@ -181,6 +182,7 @@ app.use('/notifications', notificationRoutes);
app.use('/odata', odataRoutes); app.use('/odata', odataRoutes);
app.use('/excel', excelRoutes); app.use('/excel', excelRoutes);
app.use('/csv', csvRoutes); app.use('/csv', csvRoutes);
app.use('/applaunch', appLaunchRoutes);
// Start the application // Start the application
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {

View File

@ -50,6 +50,7 @@ import notificationRoutes from './misc/notifications.js';
import odataRoutes from './misc/odata.js'; import odataRoutes from './misc/odata.js';
import excelRoutes from './misc/excel.js'; import excelRoutes from './misc/excel.js';
import csvRoutes from './misc/csv.js'; import csvRoutes from './misc/csv.js';
import appLaunchRoutes from './misc/applaunch.js';
export { export {
userRoutes, userRoutes,
@ -104,4 +105,5 @@ export {
odataRoutes, odataRoutes,
excelRoutes, excelRoutes,
csvRoutes, csvRoutes,
appLaunchRoutes,
}; };

View File

@ -0,0 +1,14 @@
import express from 'express';
import {
appLaunchSessionRouteHandler,
completeAppLaunchSessionRouteHandler,
} from '../../services/misc/applaunch.js';
import { isAuthenticated } from '../../keycloak.js';
const router = express.Router();
router.get('/:launchSession', appLaunchSessionRouteHandler);
router.post('/:launchSession', isAuthenticated, completeAppLaunchSessionRouteHandler);
export default router;

View File

@ -0,0 +1,66 @@
import config from '../../config.js';
import log4js from 'log4js';
import { redisServer } from '../../database/redis.js';
const logger = log4js.getLogger('AppLaunch');
logger.level = config.server.logLevel;
const APP_LAUNCH_PREFIX = 'applaunch:';
const APP_LAUNCH_TTL_SECONDS = 20;
export const appLaunchSessionRouteHandler = async (req, res) => {
try {
const launchSession = req.params.launchSession;
if (!launchSession) {
return res.status(400).json({ error: 'launchSession is required' });
}
// Prefer namespaced key, but also check the raw session id for compatibility.
const launchData =
(await redisServer.getKey(APP_LAUNCH_PREFIX + launchSession)) ??
(await redisServer.getKey(launchSession));
if (!launchData) {
const createdLaunchData = {
complete: false,
};
await redisServer.setKey(
APP_LAUNCH_PREFIX + launchSession,
createdLaunchData,
APP_LAUNCH_TTL_SECONDS
);
return res.json(createdLaunchData);
}
return res.json(launchData);
} catch (error) {
logger.error('Failed to fetch launch session:', error);
return res.status(500).json({ error: 'Failed to fetch launch session' });
}
};
export const completeAppLaunchSessionRouteHandler = async (req, res) => {
try {
const launchSession = req.params.launchSession;
if (!launchSession) {
return res.status(400).json({ error: 'launchSession is required' });
}
const completedLaunchData = {
complete: true,
};
await redisServer.setKey(
APP_LAUNCH_PREFIX + launchSession,
completedLaunchData,
APP_LAUNCH_TTL_SECONDS
);
return res.json(completedLaunchData);
} catch (error) {
logger.error('Failed to complete launch session:', error);
return res.status(500).json({ error: 'Failed to complete launch session' });
}
};

View File

@ -233,10 +233,7 @@ export const loginCallbackRouteHandler = async (req, res, redirectType = 'web')
} }
if (redirectType === 'app-scheme') { if (redirectType === 'app-scheme') {
const templatePath = resolve(process.cwd(), 'src/services/misc/applaunch.html'); res.redirect(config.app.urlClient + '/applaunch?redirect=' + encodeURIComponent(redirectUri));
let html = readFileSync(templatePath, 'utf8');
html = html.replace('__REDIRECT_URI__', redirectUri);
res.send(html);
} else { } else {
res.redirect(redirectUri); res.redirect(redirectUri);
} }