Added movability of dialogs

This commit is contained in:
Torsten Schulz
2024-08-19 12:34:08 +02:00
parent 4b6ad3aefe
commit 16a59daf39
40 changed files with 1625 additions and 204 deletions

View File

@@ -1,84 +1,9 @@
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';
import settingsService from '../services/settingsService.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 }))
};
}));
const responseFields = await settingsService.filterSettings(userid, type);
res.status(200).json(responseFields);
} catch (error) {
console.error('Error filtering settings:', error);
@@ -89,16 +14,7 @@ export const filterSettings = async (req, res) => {
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);
await settingsService.updateSetting(userid, settingId, value);
res.status(200).json({ message: 'Setting updated successfully' });
} catch (error) {
console.error('Error updating user setting:', error);
@@ -106,69 +22,114 @@ export const updateSetting = async (req, res) => {
}
};
export const getTypeParamValueId = async(req, res) => {
export const getTypeParamValueId = async (req, res) => {
const { paramValue } = req.body;
const userParamValueObject = await UserParamValue.findOne({
where: { value: paramValue }
});
if (!userParamValueObject) {
try {
const paramValueId = await settingsService.getTypeParamValueId(paramValue);
res.status(200).json({ paramValueId });
} catch (error) {
console.error('Error retrieving parameter value ID:', error);
res.status(404).json({ error: "notfound" });
return;
}
res.status(200).json({ paramValueId: userParamValueObject.id });
};
export const getTypeParamValues = async(req, res) => {
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}}));
try {
const paramValues = await settingsService.getTypeParamValues(type);
res.status(200).json(paramValues);
} catch (error) {
console.error('Error retrieving parameter values:', error);
res.status(500).json({ error: 'An error occurred while retrieving the parameter values' });
}
}
export const getTypeParamValue = async(req, res) => {
const { id } = req.param;
const userParamValueObject = await UserParamValue.findOne({
where: { id: id }
});
if (!userParamValueObject) {
export const getTypeParamValue = async (req, res) => {
const { id } = req.params;
try {
const paramValue = await settingsService.getTypeParamValue(id);
res.status(200).json({ paramValue });
} catch (error) {
console.error('Error retrieving parameter value:', error);
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 });
const { userId } = req.body;
const accountSettings = await settingsService.getAccountSettings(userId);
res.status(200).json(accountSettings);
} 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;
export const setAccountSettings = async (req, res) => {
try {
await settingsService.setAccountSettings(req.body);
res.status(200).json({ message: 'Account settings updated successfully' });
} catch (error) {
console.error('Error updating account settings:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
}
export const getPossibleInterests = async (req, res) => {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getPossibleInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving possible interests:', error);
res.status(500).json({ error: 'An error occurred while retrieving the possible interests' });
}
}
export const getInterests = async (req, res) => {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving interests:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const addInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { name } = req.body;
const interest = await settingsService.addInterest(userId, name);
res.status(200).json({ interest });
} catch (error) {
console.error('Error adding interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const addUserInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { interestid: interestId } = req.body;
await settingsService.addUserInterest(userId, interestId);
res.status(200).json({ message: 'User interest added successfully' });
} catch (error) {
console.error('Error adding user interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const removeInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { id: interestId } = req.params;
await settingsService.removeInterest(userId, interestId);
res.status(200).json({ message: 'Interest removed successfully' });
} catch (error) {
console.error('Error removing interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}