u
This commit is contained in:
40
src/api/routes/auth/selectedCompany.js
Normal file
40
src/api/routes/auth/selectedCompany.js
Normal 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' });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user