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 Interest from '../models/type/interest.js'; import UserInterest from '../models/community/interest.js' import InterestTranslation from '../models/type/interest_translation.js'; import { calculateAge } from '../utils/userdata.js'; import { Op } from 'sequelize'; class SettingsService { async getUser(userId) { const user = await User.findOne({ where: { hashedId: userId } }); if (!user) { throw new Error('User not found'); } return user; } async getUserParams(userId, paramDescriptions) { return await UserParam.findAll({ where: { userId }, include: [ { model: UserParamType, as: 'paramType', where: { description: { [Op.in]: paramDescriptions } } } ] }); } async getFieldOptions(field) { if (['singleselect', 'multiselect'].includes(field.datatype)) { return await UserParamValue.findAll({ where: { userParamTypeId: field.id } }); } return []; } async filterSettings(userId, type) { const user = await this.getUser(userId); const userParams = await this.getUserParams(user.id, ['birthdate', 'gender']); let birthdate = null; let gender = null; for (const param of userParams) { if (param.paramType.description === 'birthdate') { birthdate = param.value; } if (param.paramType.description === 'gender') { const genderResult = await UserParamValue.findOne({ where: { id: param.value } }); gender = genderResult ? genderResult.dataValues?.value : null; } } 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] } } ] } }); return await Promise.all(fields.map(async (field) => { const options = await this.getFieldOptions(field); 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 })) }; })); } async updateSetting(userId, settingId, value) { const user = await this.getUser(userId); const paramType = await UserParamType.findOne({ where: { id: settingId } }); if (!paramType) { throw new Error('Parameter type not found'); } await UserParam.upsertParam(user.id, paramType.id, value); } async getTypeParamValueId(paramValue) { const userParamValueObject = await UserParamValue.findOne({ where: { value: paramValue } }); if (!userParamValueObject) { throw new Error('Parameter value not found'); } return userParamValueObject.id; } async getTypeParamValues(type) { const userParamValues = await UserParamValue.findAll({ include: [ { model: UserParamType, as: 'user_param_type', where: { description: type } } ] }); return userParamValues.map(type => ({ id: type.dataValues.id, name: type.dataValues.value })); } async getTypeParamValue(id) { const userParamValueObject = await UserParamValue.findOne({ where: { id } }); if (!userParamValueObject) { throw new Error('Parameter value not found'); } return userParamValueObject.value; } async getAccountSettings(userId) { const user = await this.getUser(userId); const email = user.email; return { username: user.username, email, showinsearch: user.searchable }; } async setAccountSettings(data) { const { userId, username, email, searchable, oldpassword, newpassword, newpasswordrepeat } = data; const user = await this.getUser(userId); user.searchable = searchable; if (user.password !== oldpassword) { throw new Error('Wrong password'); } const updateUser = {}; if (username.toLowerCase() !== user.username.toLowerCase()) { const isUsernameTaken = (await User.findAll({ where: { username: username } })).length > 0; if (isUsernameTaken) { throw new Error('Username already taken'); } updateUser.username = username; } if (newpassword.trim().length > 0) { if (newpassword.length < 6) { throw new Error('Password too short'); } if (newpassword !== newpasswordrepeat) { throw new Error('Passwords do not match'); } updateUser.password = newpassword; } await user.update(updateUser); } async getPossibleInterests(userId) { const user = await this.getUser(userId); const userParams = await this.getUserParams(user.id, ['birthdate']); let birthdate = null; for (const param of userParams) { if (param.paramType.description === 'birthdate') { birthdate = param.value; } } const age = birthdate ? calculateAge(birthdate) : 0; const filter = { where: age >= 18 ? { allowed: true, } : { allowed: true, [Op.or]: [ { adultOnly: false }, { adultOnly: { [Op.eq]: null } } ] }, include: [ { model: InterestTranslation, as: 'interest_translations', required: false, include: [ { model: UserParamValue, as: 'user_param_value', required: false } ] } ] }; return await Interest.findAll(filter); } async getInterests(userId) { const user = await this.getUser(userId); return await UserInterest.findAll({ where: { userId: user.id }, include: [ { model: Interest, as: 'user_interest_type', include: [ { model: InterestTranslation, as: 'interest_translations', include: [ { model: UserParamValue, as: 'user_param_value', required: false } ] } ] } ] }); } async addInterest(userId, name) { const user = await this.getUser(userId); const existingInterests = await Interest.findAll({ where: { name: name.toLowerCase() } }); if (existingInterests.length > 0) { throw new Error('Interest already exists'); } const userParam = await this.getUserParams(user.id, ['language']); let language = 'en'; if (userParam) { const userParamValue = await UserParamValue.findOne({ where: { id: userParam[0].value } }); language = userParamValue && userParamValue.value ? userParamValue.value : 'en'; } const languageParam = await UserParamValue.findOne({ where: { value: language } }); const languageId = languageParam.id; const interest = await Interest.create({ name: name.toLowerCase(), allowed: false, adultOnly: true }); await InterestTranslation.create({ interestsId: interest.id, language: languageId, translation: name }); return interest; } async addUserInterest(userId, interestId) { const user = await this.getUser(userId); const interestsFilter = { id: interestId, allowed: true, }; const userParams = await this.getUserParams(user.id, ['birthdate']); let birthdate = null; for (const param of userParams) { if (param.paramType.description === 'birthdate') { birthdate = param.value; } } const age = birthdate ? calculateAge(birthdate) : 0; if (age < 18) { interestsFilter[Op.or] = [ { adultOnly: false }, { adultOnly: { [Op.eq]: null } } ]; } const existingInterests = await Interest.findAll({ where: interestsFilter }); if (existingInterests.length === 0) { throw new Error('Interest not found'); }; const interest = await UserInterest.findAll({ where: { userId: user.id, userinterestId: interestId } }); if (interest.length > 0) { throw new Error('Interest already exists'); } await UserInterest.create({ userId: user.id, userinterestId: interestId }); } async removeInterest(userId, interestId) { const user = await this.getUser(userId); const interests = await UserInterest.findAll({ where: { userId: user.id, userinterestId: interestId } }); for (const interest of interests) { await interest.destroy(); } } } export default new SettingsService();