111 lines
3.8 KiB
JavaScript
111 lines
3.8 KiB
JavaScript
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';
|
|
|
|
export const filterSettings = async (req, res) => {
|
|
const { userid, type } = req.body;
|
|
try {
|
|
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 }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
});
|
|
|
|
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 fetching settings:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
};
|
|
|
|
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 });
|
|
}; |