Changed controllers to classes, added image functionality
This commit is contained in:
@@ -2,7 +2,7 @@ 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';
|
||||
import UserParamType from '../models/type/user_param.js';
|
||||
|
||||
const menuStructure = {
|
||||
home: {
|
||||
@@ -14,7 +14,7 @@ const menuStructure = {
|
||||
friends: {
|
||||
visible: ["all"],
|
||||
children: {
|
||||
manageFriends : {
|
||||
manageFriends: {
|
||||
visible: ["all"],
|
||||
path: "/socialnetwork/friends",
|
||||
icon: "friends24.png"
|
||||
@@ -70,7 +70,7 @@ const menuStructure = {
|
||||
visible: ["over12"],
|
||||
action: "openRanomChat"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
falukant: {
|
||||
visible: ["all"],
|
||||
@@ -220,75 +220,74 @@ const menuStructure = {
|
||||
}
|
||||
};
|
||||
|
||||
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--;
|
||||
class NavigationController {
|
||||
constructor() {
|
||||
this.menu = this.menu.bind(this);
|
||||
}
|
||||
|
||||
return age;
|
||||
};
|
||||
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("over14") && age >= 14)) {
|
||||
const { visible, ...itemWithoutVisible } = value;
|
||||
filteredMenu[key] = { ...itemWithoutVisible };
|
||||
if (value.children) {
|
||||
filteredMenu[key].children = filterMenu(value.children, rights, age);
|
||||
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("over14") && age >= 14)) {
|
||||
const { visible, ...itemWithoutVisible } = value;
|
||||
filteredMenu[key] = { ...itemWithoutVisible };
|
||||
if (value.children) {
|
||||
filteredMenu[key].children = this.filterMenu(value.children, rights, age);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filteredMenu;
|
||||
}
|
||||
return filteredMenu;
|
||||
};
|
||||
|
||||
export const menu = async (req, res) => {
|
||||
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',
|
||||
required: false
|
||||
}]
|
||||
});
|
||||
const userBirthdateParams = await UserParam.findAll({
|
||||
where: {
|
||||
userId: user.id
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: UserParamType,
|
||||
as: 'paramType',
|
||||
where: {
|
||||
description: 'birthdate'
|
||||
async menu(req, res) {
|
||||
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',
|
||||
required: false
|
||||
}]
|
||||
});
|
||||
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 = this.calculateAge(birthDate);
|
||||
const rights = userRights.map(ur => ur.rightType.title);
|
||||
const filteredMenu = this.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' });
|
||||
}
|
||||
const age = ageFunction();
|
||||
const rights = userRights.map(ur => ur.rightType.title);
|
||||
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' });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default NavigationController;
|
||||
|
||||
Reference in New Issue
Block a user