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"; import ContactMessage from "../models/service/contactmessage.js"; import ContactService from "./ContactService.js"; import { sendAnswerEmail } from './emailService.js'; class AdminService { async hasUserAccess(userId, section) { const userRights = await UserRight.findAll({ include: [{ model: UserRightType, as: 'rightType', where: { title: [section, 'mainadmin'], } }, { model: User, as: 'user_with_rights', 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] }); } } } async getOpenContacts(userId) { if (!this.hasUserAccess(userId, 'contacts')) { throw new Error('noaccess'); } const openContacts = await ContactMessage.findAll({ where: { isFinished: false, } }) 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();