Verschieden Settings hinzugefügt (inkomplett)

This commit is contained in:
Torsten Schulz
2024-07-22 20:55:33 +02:00
parent 89842ff6c5
commit 4c12303edc
23 changed files with 269 additions and 208 deletions

View File

@@ -7,7 +7,6 @@ export const register = async (req, res) => {
const result = await userService.registerUser({ email, username, password, language });
res.status(201).json(result);
} catch (error) {
console.log(error);
res.status(500).json({ error: error.message });
}
};
@@ -21,7 +20,6 @@ export const login = async (req, res) => {
if (error.message === 'credentialsinvalid') {
res.status(404).json({ error: error.message })
} else {
console.log(error);
res.status(500).json({ error: error.message });
}
}

View File

@@ -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' });
}
};
};

View File

@@ -6,7 +6,6 @@ import UserParamValue from '../models/type/user_param_value.js';
export const filterSettings = async (req, res) => {
const { userid, type } = req.body;
console.log(userid, type);
try {
const fields = await UserParamType.findAll({
include: [
@@ -65,7 +64,6 @@ export const updateSetting = async (req, res) => {
if (!paramType) {
return res.status(404).json({ error: 'Parameter type not found' });
}
console.log(value);
await UserParam.upsertParam(user.id, paramType.id, value);
res.status(200).json({ message: 'Setting updated successfully' });
} catch (error) {