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

124 lines
4.1 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";
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();
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("{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");
}
2024-06-11 16:09:12 +07:00
2024-06-11 14:20:19 +07:00
const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId });
if (!chkAuthSys) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
}
2024-06-11 16:09:12 +07:00
body.attrOwnership = body.attrOwnership?.toUpperCase();
body.attrPrivilege = body.attrPrivilege?.toUpperCase();
body.authSysId = body.authSysId?.toUpperCase();
2024-06-11 14:20:19 +07:00
const data = new AuthRoleAttr();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
2024-08-30 18:02:34 +07:00
createdAt: new Date(),
lastUpdatedAt: new Date(),
2024-06-11 14:20:19 +07:00
};
Object.assign(data, { ...body, ...meta });
await this.authRoleAttrRepo.save(data);
2024-06-11 18:27:54 +07:00
return new HttpSuccess(data.id);
2024-06-11 14:20:19 +07:00
}
@Patch("{roleAttrId}")
public async editAuthRoleAttr(
2024-06-11 16:09:12 +07:00
@Body() body: UpdateAuthRoleAttr,
2024-06-11 14:20:19 +07:00
@Request() req: RequestWithUser,
@Path() roleAttrId: string,
) {
const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-06-11 16:09:12 +07:00
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
2024-06-11 14:20:19 +07:00
if (!chkAuthRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
}
2024-06-11 16:09:12 +07:00
const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId });
2024-06-11 14:20:19 +07:00
if (!chkAuthSys) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
}
2024-06-11 16:09:12 +07:00
body.attrOwnership = body.attrOwnership?.toUpperCase();
body.attrPrivilege = body.attrPrivilege?.toUpperCase();
body.authSysId = body.authSysId?.toUpperCase();
Object.assign(record, body);
2024-08-30 18:02:34 +07:00
record.lastUpdateUserId = req.user.sub;
2024-06-11 14:20:19 +07:00
record.lastUpdateFullName = req.user.name;
2024-08-30 18:02:34 +07:00
record.lastUpdatedAt = new Date();
2024-06-11 14:20:19 +07:00
await Promise.all([this.authRoleAttrRepo.save(record)]);
return new HttpSuccess();
}
@Delete("{roleAttrId}")
public async deleteRole(@Path() roleAttrId: string) {
2024-06-11 16:33:51 +07:00
const result = await this.authRoleAttrRepo.delete({ id: roleAttrId });
2024-06-11 14:20:19 +07:00
if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess();
}
}