import UserParamType from '../models/type/user_param.js'; import SettingsType from '../models/type/settings.js'; import UserParam from '../models/community/user_param.js'; import User from '../models/community/user.js'; import UserParamValue from '../models/type/user_param_value.js'; import { calculateAge } from '../utils/userdata.js'; import { DataTypes, Op } from 'sequelize'; import { decrypt } from '../utils/encryption.js'; export const filterSettings = async (req, res) => { const { userid, type } = req.body; try { const user = await User.findOne({ where: { hashedId: userid } }); if (!user) { return res.status(404).json({ error: 'User not found' }); } const userParams = await UserParam.findAll({ where: { userId: user.id }, include: [ { model: UserParamType, as: 'paramType' } ] }); let birthdate = null; let gender = null; for (const param of userParams) { console.log(param.paramType.description); if (param.paramType.description === 'birthdate') { birthdate = param.value; } if (param.paramType.description === 'gender') { const genderResult = await UserParamValue.findOne({ where: { id: param.value } }); gender = genderResult.dataValues.value; } } const age = birthdate ? calculateAge(birthdate) : null; const fields = await UserParamType.findAll({ include: [ { model: SettingsType, as: 'settings_type', where: { name: type } }, { model: UserParam, as: 'user_params', required: false, include: [ { model: User, as: 'user', where: { hashedId: userid } } ] } ], where: { [Op.and]: [ { minAge: { [Op.or]: [null, { [Op.lte]: age }] } }, { gender: { [Op.or]: [null, gender] } } ] } }); const responseFields = await Promise.all(fields.map(async (field) => { const options = ['singleselect', 'multiselect'].includes(field.datatype) ? await UserParamValue.findAll({ where: { userParamTypeId: field.id } }) : []; return { id: field.id, name: field.description, minAge: field.minAge, gender: field.gender, datatype: field.datatype, value: field.user_params.length > 0 ? field.user_params[0].value : null, options: options.map(opt => ({ id: opt.id, value: opt.value })) }; })); res.status(200).json(responseFields); } catch (error) { console.error('Error filtering settings:', error); res.status(500).json({ error: 'An error occurred while filtering the settings' }); } }; export const updateSetting = async (req, res) => { const { userid, settingId, value } = req.body; try { const user = await User.findOne({ where: { hashedId: userid } }); if (!user) { return res.status(404).json({ error: 'User not found' }); } const paramType = await UserParamType.findOne({ where: { id: settingId } }); if (!paramType) { return res.status(404).json({ error: 'Parameter type not found' }); } await UserParam.upsertParam(user.id, paramType.id, value); res.status(200).json({ message: 'Setting updated successfully' }); } catch (error) { console.error('Error updating user setting:', error); res.status(500).json({ error: 'Internal server error' }); } }; export const getTypeParamValueId = async(req, res) => { const { paramValue } = req.body; const userParamValueObject = await UserParamValue.findOne({ where: { value: paramValue } }); if (!userParamValueObject) { res.status(404).json({ error: "notfound" }); return; } res.status(200).json({ paramValueId: userParamValueObject.id }); }; export const getTypeParamValues = async(req, res) => { const { type } = req.body; const userParamValues = await UserParamValue.findAll({ include: [ { model: UserParamType, as: 'user_param_type', where: { description: type } } ] }); res.status(200).json(userParamValues.map(type => { return { id: type.dataValues.id, name: type.dataValues.value}})); } export const getTypeParamValue = async(req, res) => { const { id } = req.param; const userParamValueObject = await UserParamValue.findOne({ where: { id: id } }); if (!userParamValueObject) { res.status(404).json({ error: "notfound" }); return; } res.status(200).json({ paramValueId: userParamValueObject.value }); }; export const getAccountSettings = async (req, res) => { try { const user = await User.findOne({ where: { hashedId: req.body.userId } }); if (!user) { return res.status(404).json({ error: 'User not found' }); } const email = user.email; res.status(200).json({ username: user.username, email, showinsearch: user.searchable }); } catch (error) { console.error('Error retrieving account settings:', error); res.status(500).json({ error: 'Internal server error' }); } }; export const setAccountSettings = async(req, res) => { const { userid: userId, username, email, searchable, oldpassword, newpassword, newpasswordrepeat } = req.body; const user = await User.findOne({ where: { hashedId: userId }}); if (!user) { res.status(404).json({ error: 'User not found' }); return; } user.searchable = searchable; if (user.password !== oldpassword) { res.status(401).json({error: 'Wrong password'}); return; } }