hrms-api-org/src/controllers/AuthRoleController.ts

163 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-06-11 14:20:19 +07:00
import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { RequestWithUser } from "../middlewares/user";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
2024-06-11 16:33:51 +07:00
import HttpStatusCode from "../interfaces/http-status";
2024-06-11 14:20:19 +07:00
import { AuthRole, CreateAuthRole, UpdateAuthRole } from "../entities/AuthRole";
2024-06-11 18:27:54 +07:00
import { AuthRoleAttr } from "../entities/AuthRoleAttr";
2024-06-11 14:20:19 +07:00
@Route("api/v1/org/auth/authRole")
@Tags("AuthRole")
@Security("bearerAuth")
export class AuthRoleController extends Controller {
private authRoleRepo = AppDataSource.getRepository(AuthRole);
2024-06-11 18:27:54 +07:00
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
2024-06-11 14:20:19 +07:00
@Get("list")
public async listAuthRole() {
const getList = await this.authRoleRepo.find();
2024-06-11 18:27:54 +07:00
// if (!getList || getList.length === 0) {
// throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
// }
2024-06-11 14:20:19 +07:00
return new HttpSuccess(getList);
}
@Get("{roleId}")
public async detailAuthRole(@Path() roleId: string) {
2024-06-11 18:27:54 +07:00
const getDetail = await this.authRoleRepo.findOneBy({ id: roleId });
2024-06-11 14:20:19 +07:00
if (!getDetail) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-06-11 18:27:54 +07:00
const roleAttrData = await this.authRoleAttrRepo.find({
where: { authRoleId: getDetail.id },
});
const formattedData = {
...getDetail,
roleAttributes: roleAttrData,
};
return new HttpSuccess(formattedData);
2024-06-11 14:20:19 +07:00
}
@Post()
public async newAuthRole(@Request() req: RequestWithUser, @Body() body: CreateAuthRole) {
const data = new AuthRole();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
await this.authRoleRepo.save(data);
2024-06-11 18:27:54 +07:00
return new HttpSuccess(data.id);
2024-06-11 14:20:19 +07:00
}
@Patch("{roleId}")
public async editAuthRole(
@Request() req: RequestWithUser,
@Path() roleId: string,
2024-06-11 18:27:54 +07:00
@Body()
body: {
roleName: string;
roleDescription: string;
authRoleAttrs: Array<{
2024-06-12 10:11:13 +07:00
// id: string;
authSysId: string;
2024-06-11 18:27:54 +07:00
attrOwnership: string;
attrIsCreate: boolean;
attrIsList: boolean;
attrIsGet: boolean;
attrIsUpdate: boolean;
attrIsDelete: boolean;
attrPrivilege: string;
2024-06-12 11:22:28 +07:00
parentNode: string;
2024-06-11 18:27:54 +07:00
}>;
},
2024-06-11 14:20:19 +07:00
) {
const record = await this.authRoleRepo.findOneBy({ id: roleId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-06-11 18:27:54 +07:00
if (body.authRoleAttrs) {
body.authRoleAttrs = body.authRoleAttrs.map((attr) => ({
...attr,
attrOwnership: attr.attrOwnership.toUpperCase(),
attrPrivilege: attr.attrPrivilege.toUpperCase(),
2024-06-12 10:11:13 +07:00
authSysId: attr.authSysId.toUpperCase(),
2024-06-12 11:22:28 +07:00
parentNode: attr.parentNode.toUpperCase(),
2024-06-11 18:27:54 +07:00
}));
Object.assign(record, {
roleName: body.roleName,
roleDescription: body.roleDescription,
lastUpdateFullName: req.user.name,
});
}
const roleAttrData = await this.authRoleAttrRepo.find({
where: { authRoleId: roleId },
});
const updatedRoleAttrData = roleAttrData.map((attr) => {
2024-06-12 10:11:13 +07:00
const updatedAttr = body.authRoleAttrs.find((a) => a.authSysId === attr.authSysId);
2024-06-11 18:27:54 +07:00
if (updatedAttr) {
return Object.assign(attr, updatedAttr, { lastUpdateFullName: req.user.name });
}
return attr;
});
2024-06-11 14:20:19 +07:00
2024-06-12 14:55:20 +07:00
const newAttrs = body.authRoleAttrs
.filter((a) => !roleAttrData.some((attr) => attr.authSysId === a.authSysId))
.map((attr) => {
const newAttr = new AuthRoleAttr();
Object.assign(newAttr, attr, {
authRoleId: roleId,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
});
return newAttr;
});
2024-06-11 18:27:54 +07:00
await Promise.all([
this.authRoleRepo.save(record),
...updatedRoleAttrData.map((attr) => this.authRoleAttrRepo.save(attr)),
2024-06-12 14:55:20 +07:00
...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)),
2024-06-11 18:27:54 +07:00
]);
2024-06-11 14:20:19 +07:00
return new HttpSuccess();
}
@Delete("{roleId}")
public async deleteRole(@Path() roleId: string) {
2024-06-11 16:33:51 +07:00
let result: any;
try {
2024-06-11 18:27:54 +07:00
result = await this.authRoleRepo.delete({ id: roleId });
2024-06-11 16:33:51 +07:00
} catch {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้");
}
2024-06-11 14:20:19 +07:00
if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess();
}
}