routing improved, settings initialized

This commit is contained in:
Torsten Schulz
2024-07-21 18:47:45 +02:00
parent 12d66d6f9c
commit cd0699f3fd
23 changed files with 476 additions and 69 deletions

View File

@@ -18,7 +18,11 @@ export const login = async (req, res) => {
const result = await userService.loginUser({ username, password });
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
if (error.message === 'credentialsinvalid') {
res.status(404).json({ error: error.message })
} else {
res.status(500).json({ error: error.message });
}
}
};

View File

@@ -1,3 +1,7 @@
import User from '../models/community/user.js';
import UserRight from '../models/community/user_right.js';
import UserRightType from '../models/type/user_right.js';
const menuStructure = {
home: {
visible: ["all"],
@@ -49,7 +53,7 @@ const menuStructure = {
}
},
chats: {
visible: ["all"],
visible: ["over12"],
children: {
multiChat: {
visible: ["over12"],
@@ -138,11 +142,11 @@ const menuStructure = {
},
personal: {
visible: ["all"],
path: "/settings/account"
path: "/settings/personal"
},
view: {
visible: ["all"],
path: "/settings/account"
path: "/settings/view"
},
interrests: {
visible: ["all"],
@@ -202,7 +206,41 @@ const menuStructure = {
}
};
const filterMenu = (menu, rights) => {
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))) {
const { visible, ...itemWithoutVisible } = value;
filteredMenu[key] = { ...itemWithoutVisible };
if (value.children) {
filteredMenu[key].children = filterMenu(value.children, rights);
}
}
}
return filteredMenu;
};
export const menu = async (req, res) => {
const { userid } = req.params;
res.status(200).json({ userId: userid });
}
try {
const { userid } = req.params;
const user = await User.findOne({ where: { hashedId: userid } });
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const userRights = await UserRight.findAll({
where: { userId: user.id },
include: [{
model: UserRightType,
as: 'rightType'
}]
});
const rights = userRights.map(ur => ur.rightType.title);
const filteredMenu = filterMenu(menuStructure, rights);
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' });
}
};