Verschieden Settings hinzugefügt (inkomplett)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import User from '../models/community/user.js';
|
||||
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';
|
||||
|
||||
const menuStructure = {
|
||||
home: {
|
||||
@@ -206,16 +208,29 @@ const menuStructure = {
|
||||
}
|
||||
};
|
||||
|
||||
const filterMenu = (menu, rights) => {
|
||||
const calculateAge = (birthDate) => {
|
||||
const today = new Date();
|
||||
const birthDateObj = new Date(birthDate);
|
||||
let age = today.getFullYear() - birthDateObj.getFullYear();
|
||||
const monthDiff = today.getMonth() - birthDateObj.getMonth();
|
||||
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDateObj.getDate())) {
|
||||
age--;
|
||||
}
|
||||
|
||||
return age;
|
||||
};
|
||||
|
||||
const filterMenu = (menu, rights, age) => {
|
||||
const filteredMenu = {};
|
||||
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("anyadmin") && rights.length > 0))
|
||||
|| (value.visible.includes("over14") && age >= 14)) {
|
||||
const { visible, ...itemWithoutVisible } = value;
|
||||
filteredMenu[key] = { ...itemWithoutVisible };
|
||||
if (value.children) {
|
||||
filteredMenu[key].children = filterMenu(value.children, rights);
|
||||
filteredMenu[key].children = filterMenu(value.children, rights, age);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -236,11 +251,31 @@ export const menu = async (req, res) => {
|
||||
as: 'rightType'
|
||||
}]
|
||||
});
|
||||
const userBirthdateParams = await UserParam.findAll({
|
||||
where: {
|
||||
userId: user.id
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: UserParamType,
|
||||
as: 'paramType',
|
||||
where: {
|
||||
description: 'birthdate'
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
const ageFunction = function() {
|
||||
const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString();
|
||||
const age = calculateAge(birthDate);
|
||||
return age;
|
||||
}
|
||||
const age = ageFunction();
|
||||
const rights = userRights.map(ur => ur.rightType.title);
|
||||
const filteredMenu = filterMenu(menuStructure, rights);
|
||||
const filteredMenu = filterMenu(menuStructure, rights, age);
|
||||
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' });
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user