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"; import HttpStatusCode from "../interfaces/http-status"; import { AuthRole, CreateAuthRole, UpdateAuthRole } from "../entities/AuthRole"; import { AuthRoleAttr } from "../entities/AuthRoleAttr"; @Route("api/v1/org/auth/authRole") @Tags("AuthRole") @Security("bearerAuth") export class AuthRoleController extends Controller { private authRoleRepo = AppDataSource.getRepository(AuthRole); private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); @Get("list") public async listAuthRole() { const getList = await this.authRoleRepo.find(); // if (!getList || getList.length === 0) { // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); // } return new HttpSuccess(getList); } @Get("{roleId}") public async detailAuthRole(@Path() roleId: string) { const getDetail = await this.authRoleRepo.findOneBy({ id: roleId }); if (!getDetail) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } const roleAttrData = await this.authRoleAttrRepo.find({ where: { authRoleId: getDetail.id }, }); const formattedData = { ...getDetail, roleAttributes: roleAttrData, }; return new HttpSuccess(formattedData); } @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); return new HttpSuccess(data.id); } @Patch("{roleId}") public async editAuthRole( @Request() req: RequestWithUser, @Path() roleId: string, @Body() body: { roleName: string; roleDescription: string; authRoleAttrs: Array<{ // id: string; authSysId: string; attrOwnership: string; attrIsCreate: boolean; attrIsList: boolean; attrIsGet: boolean; attrIsUpdate: boolean; attrIsDelete: boolean; attrPrivilege: string; parentNode: string; }>; }, ) { const record = await this.authRoleRepo.findOneBy({ id: roleId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); if (body.authRoleAttrs) { body.authRoleAttrs = body.authRoleAttrs.map((attr) => ({ ...attr, attrOwnership: attr.attrOwnership.toUpperCase(), attrPrivilege: attr.attrPrivilege.toUpperCase(), authSysId: attr.authSysId.toUpperCase(), parentNode: attr.parentNode.toUpperCase(), })); 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) => { const updatedAttr = body.authRoleAttrs.find((a) => a.authSysId === attr.authSysId); if (updatedAttr) { return Object.assign(attr, updatedAttr, { lastUpdateFullName: req.user.name }); } return attr; }); 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; }); await Promise.all([ this.authRoleRepo.save(record), ...updatedRoleAttrData.map((attr) => this.authRoleAttrRepo.save(attr)), ...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)), ]); return new HttpSuccess(); } @Delete("{roleId}") public async deleteRole(@Path() roleId: string) { let result: any; try { result = await this.authRoleRepo.delete({ id: roleId }); } catch { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); } if (result.affected == undefined || result.affected <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); return new HttpSuccess(); } }