Bugs in settings fixed, profile added

This commit is contained in:
Torsten Schulz
2024-09-21 00:25:42 +02:00
parent c5a72d57d8
commit e494fe41db
65 changed files with 3121 additions and 7478 deletions

View File

@@ -1,23 +1,18 @@
import BaseService from './BaseService.js';
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 UserInterest from '../models/community/interest.js';
import InterestTranslation from '../models/type/interest_translation.js';
import { calculateAge } from '../utils/userdata.js';
import { Op } from 'sequelize';
import UserParamVisibilityType from '../models/type/user_param_visibility.js';
import UserParamVisibility from '../models/community/user_param_visibility.js';
import { generateIv } from '../utils/encryption.js';
class SettingsService {
async getUser(userId) {
const user = await User.findOne({ where: { hashedId: userId } });
if (!user) {
throw new Error('User not found');
}
return user;
}
class SettingsService extends BaseService{
async getUserParams(userId, paramDescriptions) {
return await UserParam.findAll({
where: { userId },
@@ -25,7 +20,8 @@ class SettingsService {
{
model: UserParamType,
as: 'paramType',
where: { description: { [Op.in]: paramDescriptions } }
where: { description: { [Op.in]: paramDescriptions } },
order: [[ 'order_id', 'asc' ]]
}
]
});
@@ -34,16 +30,16 @@ class SettingsService {
async getFieldOptions(field) {
if (['singleselect', 'multiselect'].includes(field.datatype)) {
return await UserParamValue.findAll({
where: { userParamTypeId: field.id }
where: { userParamTypeId: field.id },
order: [[ 'order_id', 'asc' ]]
});
}
return [];
}
}
async filterSettings(userId, type) {
const user = await this.getUser(userId);
async filterSettings(hashedUserId, type) {
const user = await this.getUserByHashedId(hashedUserId);
const userParams = await this.getUserParams(user.id, ['birthdate', 'gender']);
let birthdate = null;
let gender = null;
for (const param of userParams) {
@@ -55,8 +51,7 @@ class SettingsService {
gender = genderResult ? genderResult.dataValues?.value : null;
}
}
const age = birthdate ? calculateAge(birthdate) : null;
const age = birthdate ? this.calculateAge(birthdate) : null;
const fields = await UserParamType.findAll({
include: [
{
@@ -72,7 +67,18 @@ class SettingsService {
{
model: User,
as: 'user',
where: { hashedId: userId }
where: { id: user.id }
},
{
model: UserParamVisibility,
as: 'param_visibilities',
required: false,
include: [
{
model: UserParamVisibilityType,
as: 'visibility_type'
}
]
}
]
}
@@ -84,28 +90,55 @@ class SettingsService {
]
}
});
return await Promise.all(fields.map(async (field) => {
const options = await this.getFieldOptions(field);
const visibilityData = field.user_params[0]?.param_visibilities?.[0];
const visibility = visibilityData
? { id: visibilityData.visibility_type?.id, description: visibilityData.visibility_type?.description }
: { id: null, description: 'Invisible' };
return {
id: field.id,
name: field.description,
minAge: field.minAge,
gender: field.gender,
datatype: field.datatype,
unit: field.unit,
value: field.user_params.length > 0 ? field.user_params[0].value : null,
options: options.map(opt => ({ id: opt.id, value: opt.value }))
options: options.map(opt => ({ id: opt.id, value: opt.value })),
visibility
};
}));
}
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');
async updateSetting(hashedUserId, settingId, value) {
try {
const user = await this.getUserByHashedId(hashedUserId);
const paramType = await UserParamType.findOne({ where: { id: settingId } });
if (!paramType) {
throw new Error('Parameter type not found');
}
const userParam = await UserParam.findOne({
where: { userId: user.id, paramTypeId: settingId }
});
if (userParam) {
console.log('update param with ', value)
if (typeof value === 'boolean') {
value = value ? 'true' : 'false';
}
await userParam.update({value: value});
} else {
await UserParam.create(
{
userId: user.id,
paramTypeId: settingId,
value: value
}
);
}
} catch (error) {
console.error('Error updating setting:', hashedUserId, settingId, value, error);
throw error;
}
await UserParam.upsertParam(user.id, paramType.id, value);
}
async getTypeParamValueId(paramValue) {
@@ -141,171 +174,123 @@ class SettingsService {
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 addInterest(hashedUserId, name) {
try {
const user = await this.getUserByHashedId(hashedUserId);
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.length > 0) {
const userParamValue = await UserParamValue.findOne({
where: { id: userParam[0].value }
});
language = userParamValue ? 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;
} catch (error) {
console.error('Error adding interest:', error);
throw error;
}
}
async setAccountSettings(data) {
const { userId, username, email, searchable, oldpassword, newpassword, newpasswordrepeat } = data;
const user = await this.getUser(userId);
async addUserInterest(hashedUserId, interestId) {
try {
const user = await this.getUserByHashedId(hashedUserId);
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');
const userParams = await this.getUserParams(user.id, ['birthdate']);
let birthdate = null;
for (const param of userParams) {
if (param.paramType.description === 'birthdate') {
birthdate = param.value;
}
}
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]: [
const age = birthdate ? this.calculateAge(birthdate) : 0;
const interestsFilter = { id: interestId, allowed: true };
if (age < 18) {
interestsFilter[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 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 });
} catch (error) {
console.error('Error adding user interest:', error);
throw error;
}
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();
async removeInterest(hashedUserId, interestId) {
try {
const user = await this.getUserByHashedId(hashedUserId);
const interests = await UserInterest.findAll({
where: { userId: user.id, userinterestId: interestId }
});
for (const interest of interests) {
await interest.destroy();
}
} catch (error) {
console.error('Error removing interest:', error);
throw error;
}
}
async getVisibilities() {
return UserParamVisibilityType.findAll();
}
async updateVisibility(hashedUserId, userParamTypeId, visibilityId) {
try {
const user = await this.getUserByHashedId(hashedUserId);
console.log(JSON.stringify(user));
if (!user) {
throw new Error('User not found');
}
const userParam = await UserParam.findOne({
where: { paramTypeId: userParamTypeId, userId: user.id }
});
if (!userParam) {
console.error(`UserParam not found for settingId: ${userParamTypeId} and userId: ${user.id}`);
throw new Error('User parameter not found or does not belong to the user');
}
let userParamVisibility = await UserParamVisibility.findOne({
where: { param_id: userParam.id }
});
if (userParamVisibility) {
userParamVisibility.visibility = visibilityId;
await userParamVisibility.save();
} else {
await UserParamVisibility.create({
param_id: userParam.id,
visibility: visibilityId
});
}
console.log(`Visibility updated for settingId: ${userParamTypeId} with visibilityId: ${visibilityId}`);
} catch (error) {
console.error('Error updating visibility:', error.message);
throw error;
}
}
}