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

@@ -0,0 +1,22 @@
import ContactMessage from "../models/service/contactmessage.js";
class ContactService {
async addContactMessage(email, name, message, acceptDataSave) {
if (acceptDataSave && !email) {
throw new Error('emailrequired');
}
if (!acceptDataSave) {
name = '';
email = '';
}
ContactMessage.create({
email,
name,
message,
allowDataSave: acceptDataSave
});
}
}
export default new ContactService();

View File

@@ -0,0 +1,124 @@
import UserRight from "../models/community/user_right.js";
import UserRightType from "../models/type/user_right.js";
import InterestType from "../models/type/interest.js"
import InterestTranslationType from "../models/type/interest_translation.js"
import User from "../models/community/user.js";
import UserParamValue from "../models/type/user_param_value.js";
class AdminService {
async hasUserAccess(userId, section) {
const userRights = await UserRight.findAll({
/* where: {
userId: userId,
},*/
include: [{
model: UserRightType,
as: 'rightType',
where: {
title: [section, 'mainadmin'],
}
},
{
model: User,
as: 'user',
where: {
hashedId: userId,
}
}
]
});
return userRights.length > 0;
}
async getOpenInterests(userId) {
if (!this.hasUserAccess(userId, 'interests')) {
throw new Error('noaccess');
}
const openInterests = await InterestType.findAll({
where: {
allowed: false
},
include: {
model: InterestTranslationType,
as: 'interest_translations',
}
})
return openInterests;
}
async changeInterest(userId, interestId, active, adultOnly) {
if (!this.hasUserAccess(userId, 'interests')) {
throw new Error('noaccess');
}
const interest = await InterestType.findOne({
where: {
id: interestId
}
});
if (interest) {
interest.allowed = active;
interest.adultOnly = adultOnly;
await interest.save();
}
}
async deleteInterest(userId, interestId) {
if (!this.hasUserAccess(userId, 'interests')) {
throw new Error('noaccess');
}
const interest = await InterestType.findOne({
where: {
id: interestId
}
});
if (interest) {
await interest.destroy();
}
}
async changeTranslation(userId, interestId, translations) {
if (!this.hasUserAccess(userId, 'interests')) {
throw new Error('noaccess');
}
const interest = await InterestType.findOne({
id: interestId
});
if (!interest) {
throw new Error('notexisting');
}
for (const languageId of Object.keys(translations)) {
const languageObject = await UserParamValue.findOne(
{
where: {
id: languageId
}
}
);
if (!languageObject) {
throw new Error('wronglanguage');
}
const translation = await InterestTranslationType.findOne(
{
where: {
interestsId: interestId,
language: languageObject.id
}
}
);
if (translation) {
translation.translation = translations[languageId];
translation.save();
} else {
await InterestTranslationType.create({
interestsId: interestId,
language: languageObject.id,
translation: translations[languageId]
});
}
}
}
}
export default new AdminService();

View File

@@ -0,0 +1,313 @@
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();