Falukant production, family and administration enhancements
This commit is contained in:
@@ -7,6 +7,11 @@ 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';
|
||||
import { Op } from 'sequelize';
|
||||
import FalukantUser from "../models/falukant/data/user.js";
|
||||
import FalukantCharacter from "../models/falukant/data/character.js";
|
||||
import FalukantPredefineFirstname from "../models/falukant/predefine/firstname.js";
|
||||
import FalukantPredefineLastname from "../models/falukant/predefine/lastname.js";
|
||||
|
||||
class AdminService {
|
||||
async hasUserAccess(userId, section) {
|
||||
@@ -18,7 +23,7 @@ class AdminService {
|
||||
title: [section, 'mainadmin'],
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
model: User,
|
||||
as: 'user_with_rights',
|
||||
where: {
|
||||
@@ -26,9 +31,9 @@ class AdminService {
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
});
|
||||
return userRights.length > 0;
|
||||
return userRights.length > 0;
|
||||
}
|
||||
|
||||
async getOpenInterests(userId) {
|
||||
@@ -55,7 +60,7 @@ class AdminService {
|
||||
where: {
|
||||
id: interestId
|
||||
}
|
||||
});
|
||||
});
|
||||
if (interest) {
|
||||
interest.allowed = active;
|
||||
interest.adultOnly = adultOnly;
|
||||
@@ -69,7 +74,7 @@ class AdminService {
|
||||
}
|
||||
const interest = await InterestType.findOne({
|
||||
where: {
|
||||
id: interestId
|
||||
id: interestId
|
||||
}
|
||||
});
|
||||
if (interest) {
|
||||
@@ -114,10 +119,10 @@ class AdminService {
|
||||
interestsId: interestId,
|
||||
language: languageObject.id,
|
||||
translation: translations[languageId]
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getOpenContacts(userId) {
|
||||
@@ -138,6 +143,149 @@ class AdminService {
|
||||
await sendAnswerEmail(contact.email, answer, contact.language || 'en');
|
||||
}
|
||||
|
||||
async getFalukantUser(userId, userName, characterName) {
|
||||
if (!(await this.hasUserAccess(userId, 'falukantusers'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
let users;
|
||||
if (userName) {
|
||||
users = await User.findAll({
|
||||
where: {
|
||||
username: {
|
||||
[Op.like]: '%' + userName + '%'
|
||||
}
|
||||
},
|
||||
include: [{
|
||||
model: FalukantUser,
|
||||
as: 'falukantData',
|
||||
required: true,
|
||||
include: [{
|
||||
model: FalukantCharacter,
|
||||
as: 'character',
|
||||
required: true,
|
||||
include: [{
|
||||
model: FalukantPredefineFirstname,
|
||||
as: 'definedFirstName',
|
||||
required: true
|
||||
}, {
|
||||
model: FalukantPredefineLastname,
|
||||
as: 'definedLastName',
|
||||
required: true
|
||||
}]
|
||||
}]
|
||||
}]
|
||||
});
|
||||
} else if (characterName) {
|
||||
const [firstname, lastname] = characterName.split(' ');
|
||||
users = await User.findAll({
|
||||
include: [{
|
||||
model: FalukantUser,
|
||||
as: 'falukantData',
|
||||
required: true,
|
||||
include: [{
|
||||
model: FalukantCharacter,
|
||||
as: 'character',
|
||||
required: true,
|
||||
include: [{
|
||||
model: FalukantPredefineFirstname,
|
||||
as: 'definedFirstName',
|
||||
required: true,
|
||||
where: {
|
||||
name: firstname
|
||||
}
|
||||
}, {
|
||||
model: FalukantPredefineLastname,
|
||||
as: 'definedLastName',
|
||||
required: true,
|
||||
where: {
|
||||
name: lastname
|
||||
}
|
||||
}]
|
||||
}]
|
||||
}]
|
||||
});
|
||||
} else {
|
||||
throw new Error('no search parameter');
|
||||
}
|
||||
return users.map(user => {
|
||||
return {
|
||||
id: user.hashedId,
|
||||
username: user.username,
|
||||
falukantUser: user.falukantData
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getFalukantUserById(userId, hashedId) {
|
||||
if (!(await this.hasUserAccess(userId, 'falukantusers'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
hashedId: hashedId
|
||||
},
|
||||
attributes: ['hashedId', 'username'],
|
||||
include: [{
|
||||
model: FalukantUser,
|
||||
as: 'falukantData',
|
||||
required: true,
|
||||
attributes: ['money', 'certificate', 'id'],
|
||||
include: [{
|
||||
model: FalukantCharacter,
|
||||
as: 'character',
|
||||
attributes: ['birthdate', 'health', 'title_of_nobility'],
|
||||
include: [{
|
||||
model: FalukantPredefineFirstname,
|
||||
as: 'definedFirstName',
|
||||
}, {
|
||||
model: FalukantPredefineLastname,
|
||||
as: 'definedLastName',
|
||||
}]
|
||||
}]
|
||||
}]
|
||||
});
|
||||
return user;
|
||||
}
|
||||
|
||||
async changeFalukantUser(userId, falukantUserId, falukantData) {
|
||||
if (!(await this.hasUserAccess(userId, 'falukantusers'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const falukantUser = await FalukantUser.findOne({
|
||||
where: {
|
||||
id: falukantUserId
|
||||
}
|
||||
});
|
||||
if (!falukantUser) {
|
||||
throw new Error('notfound');
|
||||
}
|
||||
const character = await FalukantCharacter.findOne({
|
||||
where: {
|
||||
userId: falukantUserId
|
||||
}
|
||||
});
|
||||
if (!character) {
|
||||
throw new Error('notfound');
|
||||
}
|
||||
if (Object.keys(falukantData).indexOf('age') >= 0) {
|
||||
const birthDate = (new Date()) - (falukantData.age * 24 * 3600000);
|
||||
await character.update({
|
||||
birthdate: birthDate
|
||||
});
|
||||
}
|
||||
if (Object.keys(falukantData).indexOf('money') >= 0) {
|
||||
await falukantUser.update({
|
||||
money: falukantData.money
|
||||
});
|
||||
}
|
||||
if (Object.keys(falukantData).indexOf('title_of_nobility') >= 0) {
|
||||
await character.update({
|
||||
titleOfNobility: falukantData.title_of_nobility
|
||||
});
|
||||
}
|
||||
await falukantUser.save();
|
||||
await character.save();
|
||||
}
|
||||
}
|
||||
|
||||
export default new AdminService();
|
||||
Reference in New Issue
Block a user