Fixed websockets in navigation
This commit is contained in:
@@ -3,6 +3,7 @@ import UserParam from '../models/community/user_param.js';
|
||||
import UserRight from '../models/community/user_right.js';
|
||||
import UserRightType from '../models/type/user_right.js';
|
||||
import UserParamType from '../models/type/user_param.js';
|
||||
import FalukantUser from '../models/falukant/data/user.js';
|
||||
|
||||
const menuStructure = {
|
||||
home: {
|
||||
@@ -236,17 +237,20 @@ class NavigationController {
|
||||
return age;
|
||||
}
|
||||
|
||||
filterMenu(menu, rights, age) {
|
||||
async filterMenu(menu, rights, age, userId) {
|
||||
console.log(userId);
|
||||
const filteredMenu = {};
|
||||
const hasFalukantAccount = await this.hasFalukantAccount(userId);
|
||||
for (const [key, value] of Object.entries(menu)) {
|
||||
if (value.visible.includes("all")
|
||||
|| value.visible.some(v => rights.includes(v)
|
||||
|| (value.visible.includes("anyadmin") && rights.length > 0))
|
||||
|| (value.visible.includes("over14") && age >= 14)) {
|
||||
|| value.visible.some(v => rights.includes(v) || (value.visible.includes("anyadmin") && rights.length > 0))
|
||||
|| (value.visible.includes("over14") && age >= 14)
|
||||
|| (value.visible.includes('nofalukantaccount') && !hasFalukantAccount)
|
||||
|| (value.visible.includes('hasfalukantaccount') && hasFalukantAccount)) {
|
||||
const { visible, ...itemWithoutVisible } = value;
|
||||
filteredMenu[key] = { ...itemWithoutVisible };
|
||||
if (value.children) {
|
||||
filteredMenu[key].children = this.filterMenu(value.children, rights, age);
|
||||
filteredMenu[key].children = await this.filterMenu(value.children, rights, age, userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -281,13 +285,18 @@ class NavigationController {
|
||||
const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString();
|
||||
const age = this.calculateAge(birthDate);
|
||||
const rights = userRights.map(ur => ur.rightType.title);
|
||||
const filteredMenu = this.filterMenu(menuStructure, rights, age);
|
||||
const filteredMenu = await this.filterMenu(menuStructure, rights, age, user.id);
|
||||
res.status(200).json(filteredMenu);
|
||||
} catch (error) {
|
||||
console.error('Error fetching menu:', error);
|
||||
res.status(500).json({ error: 'An error occurred while fetching the menu' });
|
||||
}
|
||||
}
|
||||
|
||||
async hasFalukantAccount(userId) {
|
||||
const falukantUser = await FalukantUser.findOne({ where: { userId: userId } });
|
||||
return falukantUser !== null;
|
||||
}
|
||||
}
|
||||
|
||||
export default NavigationController;
|
||||
|
||||
Reference in New Issue
Block a user