124 lines
3.7 KiB
JavaScript
124 lines
3.7 KiB
JavaScript
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(); |