Bugs in settings fixed, profile added
This commit is contained in:
56
backend/services/BaseService.js
Normal file
56
backend/services/BaseService.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import User from '../models/community/user.js';
|
||||
import UserParam from '../models/community/user_param.js';
|
||||
import UserParamType from '../models/type/user_param.js';
|
||||
import UserParamVisibility from '../models/community/user_param_visibility.js';
|
||||
import UserParamVisibilityType from '../models/type/user_param_visibility.js';
|
||||
import { Op } from 'sequelize';
|
||||
|
||||
class BaseService {
|
||||
async getUserByHashedId(hashedId) {
|
||||
const user = await User.findOne({ where: { hashedId } });
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
async getUserById(userId) {
|
||||
const user = await User.findOne({ where: { id: 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 } }
|
||||
},
|
||||
{
|
||||
model: UserParamVisibility,
|
||||
as: 'param_visibilities',
|
||||
include: [
|
||||
{
|
||||
model: UserParamVisibilityType,
|
||||
as: 'visibility_type'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
calculateAge(birthdate) {
|
||||
const birthDate = new Date(birthdate);
|
||||
const ageDifMs = Date.now() - birthDate.getTime();
|
||||
const ageDate = new Date(ageDifMs);
|
||||
return Math.abs(ageDate.getUTCFullYear() - 1970);
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseService;
|
||||
@@ -10,13 +10,30 @@ class ContactService {
|
||||
name = '';
|
||||
email = '';
|
||||
}
|
||||
ContactMessage.create({
|
||||
await ContactMessage.create({
|
||||
email,
|
||||
name,
|
||||
message,
|
||||
allowDataSave: acceptDataSave
|
||||
});
|
||||
}
|
||||
|
||||
async getContactById(id) {
|
||||
const contact = await ContactMessage.findByPk(id);
|
||||
if (!contact) {
|
||||
const error = new Error('Contact not found');
|
||||
error.status = 404;
|
||||
throw error;
|
||||
}
|
||||
return contact;
|
||||
}
|
||||
|
||||
async saveAnswer(contact, answer) {
|
||||
contact.answer = answer;
|
||||
contact.answeredAt = new Date();
|
||||
contact.isFinished = true;
|
||||
await contact.save();
|
||||
}
|
||||
}
|
||||
|
||||
export default new ContactService();
|
||||
export default new ContactService();
|
||||
|
||||
@@ -5,6 +5,8 @@ import InterestTranslationType from "../models/type/interest_translation.js"
|
||||
import User from "../models/community/user.js";
|
||||
import UserParamValue from "../models/type/user_param_value.js";
|
||||
import ContactMessage from "../models/service/contactmessage.js";
|
||||
import ContactService from "./ContactService.js";
|
||||
import { sendAnswerEmail } from './emailService.js';
|
||||
|
||||
class AdminService {
|
||||
async hasUserAccess(userId, section) {
|
||||
@@ -18,7 +20,7 @@ class AdminService {
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: 'user',
|
||||
as: 'user_with_rights',
|
||||
where: {
|
||||
hashedId: userId,
|
||||
}
|
||||
@@ -129,6 +131,13 @@ class AdminService {
|
||||
})
|
||||
return openContacts;
|
||||
}
|
||||
|
||||
async answerContact(contactId, answer) {
|
||||
const contact = await ContactService.getContactById(contactId);
|
||||
await ContactService.saveAnswer(contact, answer);
|
||||
await sendAnswerEmail(contact.email, answer, contact.language || 'en');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default new AdminService();
|
||||
@@ -42,3 +42,16 @@ export const sendAccountActivationEmail = async (email, activationLink, username
|
||||
|
||||
await transporter.sendMail(mailOptions);
|
||||
};
|
||||
|
||||
export const sendAnswerEmail = async (toEmail, answer, language) => {
|
||||
i18n.setLocale(language);
|
||||
const mailOptions = {
|
||||
from: process.env.SMTP_FROM,
|
||||
to: toEmail,
|
||||
subject: 'yourPart',
|
||||
text: answer,
|
||||
html: `<p>${ answer }</p>`
|
||||
};
|
||||
|
||||
await transporter.sendMail(mailOptions);
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
187
backend/services/socialnetworkService.js
Normal file
187
backend/services/socialnetworkService.js
Normal file
@@ -0,0 +1,187 @@
|
||||
import BaseService from './BaseService.js';
|
||||
import { Op, col } from 'sequelize';
|
||||
import User from '../models/community/user.js';
|
||||
import UserParam from '../models/community/user_param.js';
|
||||
import UserParamType from '../models/type/user_param.js';
|
||||
import UserParamValue from '../models/type/user_param_value.js';
|
||||
import UserParamVisibility from '../models/community/user_param_visibility.js';
|
||||
import UserParamVisibilityType from '../models/type/user_param_visibility.js';
|
||||
|
||||
class SocialNetworkService extends BaseService {
|
||||
async searchUsers({ username, ageFrom, ageTo, genders }) {
|
||||
try {
|
||||
const whereClause = {
|
||||
active: true,
|
||||
searchable: true
|
||||
};
|
||||
if (username) {
|
||||
whereClause.username = { [Op.iLike]: `%${username}%` };
|
||||
}
|
||||
const users = await User.findAll({
|
||||
where: whereClause,
|
||||
include: [
|
||||
{
|
||||
model: UserParam,
|
||||
as: 'user_params',
|
||||
include: [
|
||||
{
|
||||
model: UserParamType,
|
||||
as: 'paramType',
|
||||
where: {
|
||||
description: {
|
||||
[Op.in]: ['gender', 'birthdate']
|
||||
}
|
||||
},
|
||||
required: true
|
||||
}
|
||||
],
|
||||
required: true
|
||||
}
|
||||
]
|
||||
});
|
||||
const results = [];
|
||||
for (const user of users) {
|
||||
const id = user.hashedId;
|
||||
const birthdateParam = user.user_params.find(param => param.paramType.description === 'birthdate');
|
||||
const genderParam = user.user_params.find(param => param.paramType.description === 'gender');
|
||||
const age = birthdateParam ? this.calculateAge(birthdateParam.value) : null;
|
||||
const decryptedGenderValue = genderParam ? genderParam.value : null;
|
||||
let gender = null;
|
||||
if (decryptedGenderValue) {
|
||||
const genderValue = await UserParamValue.findOne({
|
||||
where: {
|
||||
id: decryptedGenderValue
|
||||
}
|
||||
});
|
||||
gender = genderValue ? genderValue.value : null;
|
||||
}
|
||||
const isWithinAgeRange = (!ageFrom || age >= ageFrom) && (!ageTo || age <= ageTo);
|
||||
if (isWithinAgeRange && (!genders || !genders.length || (gender && genders.includes(gender))) && age >= 14) {
|
||||
results.push({
|
||||
id: id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
gender: gender,
|
||||
age: age
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
} catch (error) {
|
||||
console.error('Error in searchUsers:', error);
|
||||
throw new Error('Error searching users');
|
||||
}
|
||||
}
|
||||
|
||||
async getProfile(hashedUserId, requestingUserId) {
|
||||
try {
|
||||
const requestingUser = await this.getUserByHashedId(requestingUserId);
|
||||
const requestingUserParams = await this.getUserParams(requestingUser.id, ['birthdate']);
|
||||
let requestingUserAge = 0;
|
||||
|
||||
for (const param of requestingUserParams) {
|
||||
if (param.paramType.description === 'birthdate') {
|
||||
requestingUserAge = this.calculateAge(param.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
hashedId: hashedUserId,
|
||||
active: true,
|
||||
searchable: true,
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: UserParam,
|
||||
as: 'user_params',
|
||||
include: [
|
||||
{
|
||||
model: UserParamType,
|
||||
as: 'paramType',
|
||||
},
|
||||
{
|
||||
model: UserParamVisibility,
|
||||
as: 'param_visibilities',
|
||||
include: [
|
||||
{
|
||||
model: UserParamVisibilityType,
|
||||
as: 'visibility_type'
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
order: [[ 'order_id', 'asc']]
|
||||
}
|
||||
]
|
||||
});
|
||||
if (user) {
|
||||
const userParams = {};
|
||||
await Promise.all(user.user_params.map(async (param) => {
|
||||
const visibilityData = param.param_visibilities?.[0]?.visibility_type;
|
||||
const visibility = visibilityData ? visibilityData.description : 'Invisible';
|
||||
let paramValue = param.value;
|
||||
let paramValueChanged = false;
|
||||
try {
|
||||
const parsedValue = JSON.parse(paramValue);
|
||||
if (Array.isArray(parsedValue)) {
|
||||
paramValue = await Promise.all(parsedValue.map(async (value) => {
|
||||
if (/^\d+$/.test(value)) {
|
||||
const userParamValue = await UserParamValue.findOne({
|
||||
where: {
|
||||
id: parseInt(value, 10),
|
||||
userParamTypeId: param.paramTypeId
|
||||
}
|
||||
});
|
||||
paramValueChanged = true;
|
||||
return userParamValue ? userParamValue.value : value;
|
||||
}
|
||||
return value;
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
if (!paramValueChanged) {
|
||||
if (/^\d+$/.test(paramValue)) {
|
||||
const userParamValue = await UserParamValue.findOne({
|
||||
where: {
|
||||
id: parseInt(paramValue, 10),
|
||||
userParamTypeId: param.paramTypeId
|
||||
}
|
||||
});
|
||||
if (userParamValue) {
|
||||
paramValue = userParamValue.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
const paramTypeDescription = param.paramType.description;
|
||||
if (visibility === 'Invisible') {
|
||||
return;
|
||||
}
|
||||
if (visibility === 'All' || (visibility === 'FriendsAndAdults' && requestingUserAge >= 18) || (visibility === 'AdultsOnly' && requestingUserAge >= 18)) {
|
||||
userParams[paramTypeDescription] = {
|
||||
type: param.paramType.datatype,
|
||||
value: paramValue
|
||||
};
|
||||
if (paramTypeDescription === 'birthdate') {
|
||||
userParams['age'] = { value: this.calculateAge(Date.parse(paramValue)), type: "int"};
|
||||
}
|
||||
}
|
||||
}));
|
||||
const userProfile = {
|
||||
username: user.username,
|
||||
registrationDate: user.registrationDate,
|
||||
params: userParams
|
||||
};
|
||||
|
||||
return userProfile;
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Error in getProfile:', error);
|
||||
throw new Error('Error getting profile');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default SocialNetworkService;
|
||||
Reference in New Issue
Block a user