Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s

This commit is contained in:
Torsten Schulz (local)
2026-07-21 09:08:15 +02:00
parent 75b52de282
commit 1ab9407e79
1400 changed files with 22278 additions and 485 deletions

124
backend/services/settingsService.js Normal file → Executable file
View File

@@ -173,6 +173,130 @@ class SettingsService extends BaseService{
return [];
}
async getUserAgeAndLanguage(hashedUserId) {
const user = await this.getUserByHashedId(hashedUserId);
const userParams = await this.getUserParams(user.id, ['birthdate', 'language']);
let birthdate = null;
let language = 'en';
for (const param of userParams) {
if (param.paramType.description === 'birthdate') {
birthdate = param.value;
}
if (param.paramType.description === 'language') {
const languageValue = await UserParamValue.findOne({
where: { id: param.value }
});
language = languageValue ? languageValue.value : 'en';
}
}
return {
user,
age: birthdate ? this.calculateAge(birthdate) : null,
language,
};
}
async buildInterestPayload(interest, languageIdByCode) {
const translations = await InterestTranslation.findAll({
where: { interestsId: interest.id },
order: [[ 'id', 'asc' ]]
});
return {
id: interest.id,
name: interest.name,
allowed: interest.allowed,
adultOnly: interest.adultOnly,
interest_translations: translations.map((translation) => ({
id: translation.id,
translation: translation.translation,
language: translation.language,
user_param_value: {
value: languageIdByCode.get(Number(translation.language)) || 'en',
},
})),
};
}
async getPossibleInterests(hashedUserId) {
const { user, age, language } = await this.getUserAgeAndLanguage(hashedUserId);
const languageType = await UserParamType.findOne({ where: { description: 'language' } });
const languageValues = languageType
? await UserParamValue.findAll({ where: { userParamTypeId: languageType.id } })
: [];
const languageIdByCode = new Map(languageValues.map((entry) => [Number(entry.id), entry.value]));
const where = { allowed: true };
if (age !== null && age < 18) {
where[Op.or] = [
{ adultOnly: false },
{ adultOnly: { [Op.eq]: null } }
];
}
const interests = await InterestType.findAll({
where,
order: [[ 'name', 'asc' ]],
});
return Promise.all(interests.map(async (interest) => {
const payload = await this.buildInterestPayload(interest, languageIdByCode);
if (language && payload.interest_translations.length > 0) {
payload.interest_translations.sort((a, b) => {
const aPreferred = a.user_param_value?.value === language ? 0 : 1;
const bPreferred = b.user_param_value?.value === language ? 0 : 1;
return aPreferred - bPreferred;
});
}
return payload;
}));
}
async getInterests(hashedUserId) {
const { user, language } = await this.getUserAgeAndLanguage(hashedUserId);
const languageType = await UserParamType.findOne({ where: { description: 'language' } });
const languageValues = languageType
? await UserParamValue.findAll({ where: { userParamTypeId: languageType.id } })
: [];
const languageIdByCode = new Map(languageValues.map((entry) => [Number(entry.id), entry.value]));
const selectedInterests = await UserInterest.findAll({
where: { userId: user.id },
include: [
{
model: InterestType,
as: 'interest_type',
}
],
order: [[ 'id', 'asc' ]],
});
const payloads = [];
for (const userInterest of selectedInterests) {
const interestType = userInterest.interest_type;
if (!interestType) {
continue;
}
const payload = await this.buildInterestPayload(interestType, languageIdByCode);
if (language && payload.interest_translations.length > 0) {
payload.interest_translations.sort((a, b) => {
const aPreferred = a.user_param_value?.value === language ? 0 : 1;
const bPreferred = b.user_param_value?.value === language ? 0 : 1;
return aPreferred - bPreferred;
});
}
payloads.push({
id: interestType.id,
user_interest_type: payload,
});
}
return payloads;
}
async filterSettings(hashedUserId, type) {
const user = await this.getUserByHashedId(hashedUserId);
const userParams = await this.getUserParams(user.id, ['birthdate', 'gender']);