From cbe54233caa4d581d337f40cad19b02064132b9e Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 11 Jun 2024 14:20:19 +0700 Subject: [PATCH] crud auth role --- src/controllers/AuthRoleAttrController.ts | 113 ++++++++++++++++++++++ src/controllers/AuthRoleController.ts | 88 +++++++++++++++++ src/controllers/AuthSysController.ts | 9 +- 3 files changed, 203 insertions(+), 7 deletions(-) create mode 100644 src/controllers/AuthRoleAttrController.ts create mode 100644 src/controllers/AuthRoleController.ts diff --git a/src/controllers/AuthRoleAttrController.ts b/src/controllers/AuthRoleAttrController.ts new file mode 100644 index 00000000..50a1dff6 --- /dev/null +++ b/src/controllers/AuthRoleAttrController.ts @@ -0,0 +1,113 @@ +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 { AuthRoleAttr, CreateAuthRoleAttr, UpdateAuthRoleAttr } from "../entities/AuthRoleAttr"; +import { AuthRole } from "../entities/AuthRole"; +import { AuthSys } from "../entities/AuthSys"; + +@Route("api/v1/org/auth/authRoleAttr") +@Tags("AuthRoleAttr") +@Security("bearerAuth") +export class AuthRoleAttrController extends Controller { + private authRoleRepo = AppDataSource.getRepository(AuthRole); + private authSysRepo = AppDataSource.getRepository(AuthSys); + private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); + + @Get("list") + public async listAuthRoleAttr() { + const getList = await this.authRoleAttrRepo.find(); + if (!getList || getList.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getList); + } + + @Get("{roleAttrId}") + public async detailAuthRoleAttr(@Path() roleAttrId: string) { + const getDetail = await this.authRoleAttrRepo.findBy({ id: roleAttrId }); + if (!getDetail) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getDetail); + } + + @Post() + public async newAuthRoleAttr(@Request() req: RequestWithUser, @Body() body: CreateAuthRoleAttr) { + + const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId }); + if (!chkAuthRole) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId"); + } + + const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId }); + if (!chkAuthSys) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId"); + } + + const data = new AuthRoleAttr(); + 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.authRoleAttrRepo.save(data); + + return new HttpSuccess(); + } + + @Patch("{roleAttrId}") + public async editAuthRoleAttr( + @Body() requestBody: UpdateAuthRoleAttr, + @Request() req: RequestWithUser, + @Path() roleAttrId: string, + ) { + const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId }); + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const chkAuthRole = await this.authRoleRepo.findOneBy({ id: requestBody.authRoleId }); + if (!chkAuthRole) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId"); + } + + const chkAuthSys = await this.authSysRepo.findOneBy({ id: requestBody.authSysId }); + if (!chkAuthSys) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId"); + } + + Object.assign(record, requestBody); + record.lastUpdateFullName = req.user.name; + + await Promise.all([this.authRoleAttrRepo.save(record)]); + + return new HttpSuccess(); + } + + @Delete("{roleAttrId}") + public async deleteRole(@Path() roleAttrId: string) { + const result = await this.authRoleAttrRepo.delete({ id: roleAttrId }); + + if (result.affected == undefined || result.affected <= 0) + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + return new HttpSuccess(); + } +} diff --git a/src/controllers/AuthRoleController.ts b/src/controllers/AuthRoleController.ts new file mode 100644 index 00000000..eb3a6182 --- /dev/null +++ b/src/controllers/AuthRoleController.ts @@ -0,0 +1,88 @@ +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 { AuthRole, CreateAuthRole, UpdateAuthRole } from "../entities/AuthRole"; + +@Route("api/v1/org/auth/authRole") +@Tags("AuthRole") +@Security("bearerAuth") +export class AuthRoleController extends Controller { + private authRoleRepo = AppDataSource.getRepository(AuthRole); + + @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.findBy({ id: roleId }); + if (!getDetail) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getDetail); + } + + @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(); + } + + @Patch("{roleId}") + public async editAuthRole( + @Body() requestBody: UpdateAuthRole, + @Request() req: RequestWithUser, + @Path() roleId: string, + ) { + const record = await this.authRoleRepo.findOneBy({ id: roleId }); + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + Object.assign(record, requestBody); + record.lastUpdateFullName = req.user.name; + + await Promise.all([this.authRoleRepo.save(record)]); + + return new HttpSuccess(); + } + + @Delete("{roleId}") + public async deleteRole(@Path() roleId: string) { + const result = await this.authRoleRepo.delete({ id: roleId }); + + if (result.affected == undefined || result.affected <= 0) + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + return new HttpSuccess(); + } +} diff --git a/src/controllers/AuthSysController.ts b/src/controllers/AuthSysController.ts index 98114750..aecbaec2 100644 --- a/src/controllers/AuthSysController.ts +++ b/src/controllers/AuthSysController.ts @@ -25,11 +25,7 @@ export class AuthSysController extends Controller { private authSysRepo = AppDataSource.getRepository(AuthSys); @Get("list") - public async listAuthSys(@Request() request: { user: Record }) { - const profile = await this.authSysRepo.findOneBy({ id: request.user.sub }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } + public async listAuthSys() { const getList = await this.authSysRepo.find(); if (!getList || getList.length === 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); @@ -84,8 +80,7 @@ export class AuthSysController extends Controller { } @Delete("{systemId}") - public async deleteProfileAbility(@Path() systemId: string) { - + public async deleteAuthSys(@Path() systemId: string) { const result = await this.authSysRepo.delete({ id: systemId }); if (result.affected == undefined || result.affected <= 0)