2024-09-01 14:55:05 +02:00
|
|
|
import { Request, Response } from "express";
|
2025-01-05 14:14:00 +01:00
|
|
|
import UserService from "../../../service/user/userService";
|
|
|
|
import UserFactory from "../../../factory/admin/user/user";
|
|
|
|
import UserPermissionService from "../../../service/user/userPermissionService";
|
|
|
|
import PermissionHelper from "../../../helpers/permissionHelper";
|
|
|
|
import RoleFactory from "../../../factory/admin/user/role";
|
|
|
|
import { DeleteUserCommand, UpdateUserCommand, UpdateUserRolesCommand } from "../../../command/user/user/userCommand";
|
|
|
|
import UserCommandHandler from "../../../command/user/user/userCommandHandler";
|
|
|
|
import MailHelper from "../../../helpers/mailHelper";
|
|
|
|
import { CLUB_NAME } from "../../../env.defaults";
|
|
|
|
import { UpdateUserPermissionsCommand } from "../../../command/user/user/userPermissionCommand";
|
|
|
|
import UserPermissionCommandHandler from "../../../command/user/user/userPermissionCommandHandler";
|
2025-01-24 09:20:08 +01:00
|
|
|
import BadRequestException from "../../../exceptions/badRequestException";
|
2024-09-01 14:55:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get All users
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllUsers(req: Request, res: Response): Promise<any> {
|
|
|
|
let users = await UserService.getAll();
|
|
|
|
|
|
|
|
res.json(UserFactory.mapToBase(users));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get user by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getUserById(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
let user = await UserService.getById(id);
|
|
|
|
|
|
|
|
res.json(UserFactory.mapToSingle(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get permissions by user
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getUserPermissions(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
let permissions = await UserPermissionService.getByUser(id);
|
|
|
|
|
|
|
|
res.json(PermissionHelper.convertToObject(permissions.map((p) => p.permission)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get assigned roles by user
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getUserRoles(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
|
|
|
|
let roles = await UserService.getAssignedRolesByUserId(id);
|
|
|
|
|
|
|
|
res.json(RoleFactory.mapToBase(roles));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update user data
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateUser(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
let mail = req.body.mail;
|
|
|
|
let firstname = req.body.firstname;
|
|
|
|
let lastname = req.body.lastname;
|
|
|
|
let username = req.body.username;
|
|
|
|
|
|
|
|
let updateUser: UpdateUserCommand = {
|
|
|
|
id: id,
|
|
|
|
mail: mail,
|
|
|
|
firstname: firstname,
|
|
|
|
lastname: lastname,
|
|
|
|
username: username,
|
|
|
|
};
|
|
|
|
await UserCommandHandler.update(updateUser);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update user assigned permission strings
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateUserPermissions(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
let permissions = req.body.permissions;
|
|
|
|
|
|
|
|
let permissionStrings = PermissionHelper.convertToStringArray(permissions);
|
|
|
|
|
|
|
|
let updateUserPermissions: UpdateUserPermissionsCommand = {
|
|
|
|
userId: id,
|
|
|
|
permissions: permissionStrings,
|
|
|
|
};
|
|
|
|
await UserPermissionCommandHandler.updatePermissions(updateUserPermissions);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update user assigned roles
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateUserRoles(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
let roleIds = req.body.roleIds as Array<number>;
|
|
|
|
|
|
|
|
let updateRoles: UpdateUserRolesCommand = {
|
|
|
|
id: id,
|
|
|
|
roleIds: roleIds,
|
|
|
|
};
|
|
|
|
await UserCommandHandler.updateRoles(updateRoles);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description delete user by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function deleteUser(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
|
2025-01-24 09:20:08 +01:00
|
|
|
let { mail, isOwner } = await UserService.getById(id);
|
|
|
|
|
|
|
|
if (isOwner) {
|
|
|
|
throw new BadRequestException("Owner cannot be deleted");
|
|
|
|
}
|
2024-09-01 14:55:05 +02:00
|
|
|
|
|
|
|
let deleteUser: DeleteUserCommand = {
|
|
|
|
id: id,
|
|
|
|
};
|
|
|
|
await UserCommandHandler.delete(deleteUser);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// sendmail
|
2024-12-28 18:03:33 +01:00
|
|
|
await MailHelper.sendMail(
|
2025-01-24 09:20:08 +01:00
|
|
|
mail,
|
2024-09-01 14:55:05 +02:00
|
|
|
`Email Bestätigung für Mitglieder Admin-Portal von ${CLUB_NAME}`,
|
|
|
|
`Ihr Nutzerkonto des Adminportals wurde erfolgreich gelöscht.`
|
|
|
|
);
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|