transfer Ownership

This commit is contained in:
Julian Krauser 2024-11-21 15:58:47 +01:00
parent f87c7b4a7c
commit ea227433e6
4 changed files with 75 additions and 3 deletions

View file

@ -15,6 +15,11 @@ export interface UpdateUserCommand {
lastname: string; lastname: string;
} }
export interface TransferUserOwnerCommand {
fromId: number;
toId: number;
}
export interface UpdateUserRolesCommand { export interface UpdateUserRolesCommand {
id: number; id: number;
roleIds: Array<number>; roleIds: Array<number>;

View file

@ -2,7 +2,13 @@ import { EntityManager } from "typeorm";
import { dataSource } from "../data-source"; import { dataSource } from "../data-source";
import { user } from "../entity/user"; import { user } from "../entity/user";
import InternalException from "../exceptions/internalException"; import InternalException from "../exceptions/internalException";
import { CreateUserCommand, DeleteUserCommand, UpdateUserCommand, UpdateUserRolesCommand } from "./userCommand"; import {
CreateUserCommand,
DeleteUserCommand,
TransferUserOwnerCommand,
UpdateUserCommand,
UpdateUserRolesCommand,
} from "./userCommand";
import UserService from "../service/userService"; import UserService from "../service/userService";
export default abstract class UserCommandHandler { export default abstract class UserCommandHandler {
@ -90,6 +96,38 @@ export default abstract class UserCommandHandler {
return await manager.createQueryBuilder().relation(user, "roles").of(userId).remove(roleId); return await manager.createQueryBuilder().relation(user, "roles").of(userId).remove(roleId);
} }
/**
* @description transfer ownership
* @param TransferUserOwnerCommand
* @returns {Promise<void>}
*/
static async transferOwnership(transferOwnership: TransferUserOwnerCommand): Promise<void> {
return await dataSource.manager
.transaction(async (manager) => {
manager
.createQueryBuilder()
.update(user)
.set({
isOwner: false,
})
.where("id = :id", { id: transferOwnership.fromId })
.execute();
manager
.createQueryBuilder()
.update(user)
.set({
isOwner: true,
})
.where("id = :id", { id: transferOwnership.toId })
.execute();
})
.then(() => {})
.catch((err) => {
throw new InternalException("Failed transfering ownership", err);
});
}
/** /**
* @description delete user * @description delete user
* @param DeleteUserCommand * @param DeleteUserCommand

View file

@ -5,8 +5,9 @@ import InternalException from "../exceptions/internalException";
import { CLUB_NAME } from "../env.defaults"; import { CLUB_NAME } from "../env.defaults";
import UserService from "../service/userService"; import UserService from "../service/userService";
import UserFactory from "../factory/admin/user"; import UserFactory from "../factory/admin/user";
import { UpdateUserCommand } from "../command/userCommand"; import { TransferUserOwnerCommand, UpdateUserCommand } from "../command/userCommand";
import UserCommandHandler from "../command/userCommandHandler"; import UserCommandHandler from "../command/userCommandHandler";
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
/** /**
* @description get my by id * @description get my by id
@ -70,6 +71,30 @@ export async function verifyMyTotp(req: Request, res: Response): Promise<any> {
res.sendStatus(204); res.sendStatus(204);
} }
/**
* @description transferOwnership
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function transferOwnership(req: Request, res: Response): Promise<any> {
const userId = parseInt(req.userId);
let toId = req.body.toId;
let { isOwner } = await UserService.getById(userId);
if (!isOwner) {
throw new ForbiddenRequestException("Action only allowed to owner.");
}
let transfer: TransferUserOwnerCommand = {
toId: toId,
fromId: userId,
};
await UserCommandHandler.transferOwnership(transfer);
res.sendStatus(204);
}
/** /**
* @description update my data * @description update my data
* @param req {Request} Express req object * @param req {Request} Express req object

View file

@ -1,5 +1,5 @@
import express from "express"; import express from "express";
import { getMeById, getMyTotp, updateMe, verifyMyTotp } from "../controller/userController"; import { getMeById, getMyTotp, transferOwnership, updateMe, verifyMyTotp } from "../controller/userController";
var router = express.Router({ mergeParams: true }); var router = express.Router({ mergeParams: true });
@ -15,6 +15,10 @@ router.post("/verify", async (req, res) => {
await verifyMyTotp(req, res); await verifyMyTotp(req, res);
}); });
router.put("/transferOwner", async (req, res) => {
await transferOwnership(req, res);
});
router.patch("/me", async (req, res) => { router.patch("/me", async (req, res) => {
await updateMe(req, res); await updateMe(req, res);
}); });