From ba065b4299e487c3832947c209f2e8fea6b22450 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 11 Jun 2024 18:27:54 +0700 Subject: [PATCH] fix api role --- src/controllers/AuthRoleAttrController.ts | 8 +-- src/controllers/AuthRoleController.ts | 73 +++++++++++++++++---- src/controllers/AuthSysController.ts | 8 +-- src/controllers/ProfileAbilityController.ts | 2 +- 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/controllers/AuthRoleAttrController.ts b/src/controllers/AuthRoleAttrController.ts index e3f5779a..72cf6b44 100644 --- a/src/controllers/AuthRoleAttrController.ts +++ b/src/controllers/AuthRoleAttrController.ts @@ -31,9 +31,9 @@ export class AuthRoleAttrController extends Controller { @Get("list") public async listAuthRoleAttr() { const getList = await this.authRoleAttrRepo.find(); - if (!getList || getList.length === 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } + // if (!getList || getList.length === 0) { + // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + // } return new HttpSuccess(getList); } @@ -74,7 +74,7 @@ export class AuthRoleAttrController extends Controller { await this.authRoleAttrRepo.save(data); - return new HttpSuccess(); + return new HttpSuccess(data.id); } @Patch("{roleAttrId}") diff --git a/src/controllers/AuthRoleController.ts b/src/controllers/AuthRoleController.ts index d9382464..361a55f7 100644 --- a/src/controllers/AuthRoleController.ts +++ b/src/controllers/AuthRoleController.ts @@ -18,29 +18,41 @@ 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, "ไม่พบข้อมูล"); - } + // 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.findBy({ id: roleId }); + const getDetail = await this.authRoleRepo.findOneBy({ id: roleId }); if (!getDetail) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } - return new HttpSuccess(getDetail); + + const roleAttrData = await this.authRoleAttrRepo.find({ + where: { authRoleId: getDetail.id }, + }); + + const formattedData = { + ...getDetail, + roleAttributes: roleAttrData, + }; + + return new HttpSuccess(formattedData); } @Post() @@ -57,21 +69,60 @@ export class AuthRoleController extends Controller { await this.authRoleRepo.save(data); - return new HttpSuccess(); + return new HttpSuccess(data.id); } @Patch("{roleId}") public async editAuthRole( - @Body() body: UpdateAuthRole, @Request() req: RequestWithUser, @Path() roleId: string, + @Body() + body: { + roleName: string; + roleDescription: string; + authRoleAttrs: Array<{ + id: string; + attrOwnership: string; + attrIsCreate: boolean; + attrIsList: boolean; + attrIsGet: boolean; + attrIsUpdate: boolean; + attrIsDelete: boolean; + attrPrivilege: string; + }>; + }, ) { const record = await this.authRoleRepo.findOneBy({ id: roleId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - Object.assign(record, body); - record.lastUpdateFullName = req.user.name; + if (body.authRoleAttrs) { + body.authRoleAttrs = body.authRoleAttrs.map((attr) => ({ + ...attr, + attrOwnership: attr.attrOwnership.toUpperCase(), + attrPrivilege: attr.attrPrivilege.toUpperCase(), + })); - await Promise.all([this.authRoleRepo.save(record)]); + 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.id === attr.id); + if (updatedAttr) { + return Object.assign(attr, updatedAttr, { lastUpdateFullName: req.user.name }); + } + return attr; + }); + + await Promise.all([ + this.authRoleRepo.save(record), + ...updatedRoleAttrData.map((attr) => this.authRoleAttrRepo.save(attr)), + ]); return new HttpSuccess(); } @@ -80,7 +131,7 @@ export class AuthRoleController extends Controller { public async deleteRole(@Path() roleId: string) { let result: any; try { - result = await this.authRoleRepo.delete({ id: roleId }); + result = await this.authRoleRepo.delete({ id: roleId }); } catch { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); } diff --git a/src/controllers/AuthSysController.ts b/src/controllers/AuthSysController.ts index cbb9abad..756bd368 100644 --- a/src/controllers/AuthSysController.ts +++ b/src/controllers/AuthSysController.ts @@ -28,9 +28,9 @@ export class AuthSysController extends Controller { @Get("list") public async listAuthSys() { const getList = await this.authSysRepo.find(); - if (!getList || getList.length === 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } + // if (!getList || getList.length === 0) { + // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + // } return new HttpSuccess(getList); } @@ -63,7 +63,7 @@ export class AuthSysController extends Controller { await this.authSysRepo.save(data); - return new HttpSuccess(); + return new HttpSuccess(data.id); } @Patch("{systemId}") diff --git a/src/controllers/ProfileAbilityController.ts b/src/controllers/ProfileAbilityController.ts index 30e2b683..8bae21a6 100644 --- a/src/controllers/ProfileAbilityController.ts +++ b/src/controllers/ProfileAbilityController.ts @@ -188,7 +188,7 @@ export class ProfileAbilityController extends Controller { await this.profileAbilityHistoryRepo.delete({ profileAbilityId: abilityId, }); - + const result = await this.profileAbilityRepo.delete({ id: abilityId }); if (result.affected == undefined || result.affected <= 0)