This commit is contained in:
seb
2026-07-31 07:07:12 +02:00
parent db8b192094
commit 043305c04c
7 changed files with 219 additions and 10 deletions

View File

@@ -0,0 +1,40 @@
import * as db from '../../../db/googleAuth.js';
import { clearCachedUser } from '../../middleware/auth.js';
/**
* Registers the selected-company route (requires auth middleware).
* @param {import('express').Application} app - Express app
* @param {string} [basePath=''] - Optional URL prefix for all routes in this module
*/
export function register(app, basePath = '') {
/**
* @route PUT /auth/selected-company
* @description Sets the authenticated user's selected company.
* @body {number} companyId
* @returns {Object} 200 - `{ user: { id, email, selectedCompanyId, picture? } }`
*/
app.put(`${basePath}/auth/selected-company`, async (req, res) => {
const companyId = Number(req.body?.companyId);
if (!Number.isInteger(companyId) || companyId <= 0) {
return res.status(400).json({ error: 'companyId is required' });
}
try {
await db.setSelectedCompany(req.user.id, companyId);
clearCachedUser(req.user.email);
const updated = await db.getAllowedEmail(req.user.email);
const user = {
id: updated.id,
email: updated.email,
selectedCompanyId: updated.selectedCompanyId,
...(req.user.picture && { picture: req.user.picture }),
};
res.status(200).json({ user });
} catch (error) {
console.error('Set selected company failed:', error);
res.status(500).json({ error: 'Failed to set selected company' });
}
});
}