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-13 14:41:47 +07:00
|
|
|
import { AuthRole, CreateAuthRole, UpdateAuthRole, CreateAddAuthRole } from "../entities/AuthRole";
|
2024-06-11 18:27:54 +07:00
|
|
|
import { AuthRoleAttr } from "../entities/AuthRoleAttr";
|
2024-06-13 14:41:47 +07:00
|
|
|
import { PosMaster } from "../entities/PosMaster";
|
|
|
|
|
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
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-13 15:35:59 +07:00
|
|
|
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
|
|
|
|
|
private posMasterRepository = AppDataSource.getRepository(PosMaster);
|
|
|
|
|
private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster)
|
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
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:35:59 +07:00
|
|
|
@Post("govoment")
|
|
|
|
|
public async AddAuthRoleGovoment(@Request() req: RequestWithUser, @Body() body: CreateAddAuthRole) {
|
|
|
|
|
|
2024-06-17 14:18:06 +07:00
|
|
|
let NULL_ : any = null;
|
2024-06-14 14:06:40 +07:00
|
|
|
let getDetail
|
|
|
|
|
|
|
|
|
|
if(body.authRoleId == "") {
|
2024-06-17 14:18:06 +07:00
|
|
|
body.authRoleId = NULL_
|
2024-06-14 14:06:40 +07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
|
|
|
|
|
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:35:59 +07:00
|
|
|
const posMaster = await this.posMasterRepository.findOneBy({ id : body.posMasterId })
|
|
|
|
|
if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง");
|
|
|
|
|
|
|
|
|
|
posMaster.lastUpdateUserId = req.user.sub;
|
|
|
|
|
posMaster.lastUpdateFullName = req.user.name;
|
|
|
|
|
posMaster.authRoleId = body.authRoleId
|
|
|
|
|
await this.posMasterRepository.save(posMaster);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("employee")
|
|
|
|
|
public async AddAuthRoleEmployee(@Request() req: RequestWithUser, @Body() body: CreateAddAuthRole) {
|
2024-06-14 14:06:40 +07:00
|
|
|
|
2024-06-17 14:18:06 +07:00
|
|
|
let NULL_ : any = null;
|
2024-06-14 14:06:40 +07:00
|
|
|
let getDetail
|
|
|
|
|
|
|
|
|
|
if(body.authRoleId == "") {
|
2024-06-17 14:18:06 +07:00
|
|
|
body.authRoleId = NULL_
|
2024-06-14 14:06:40 +07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
|
|
|
|
|
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:35:59 +07:00
|
|
|
|
|
|
|
|
const posMaster = await this.employeePosMasterRepository.findOneBy({ id : body.posMasterId })
|
|
|
|
|
if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง");
|
|
|
|
|
|
|
|
|
|
posMaster.lastUpdateUserId = req.user.sub;
|
|
|
|
|
posMaster.lastUpdateFullName = req.user.name;
|
|
|
|
|
posMaster.authRoleId = body.authRoleId
|
|
|
|
|
await this.employeePosMasterRepository.save(posMaster);
|
2024-06-13 14:41:47 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
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 },
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-12 15:10:36 +07:00
|
|
|
// 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)),
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
|
|
await this.authRoleAttrRepo.remove(roleAttrData);
|
|
|
|
|
|
|
|
|
|
const newAttrs = body.authRoleAttrs.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,
|
2024-06-12 14:55:20 +07:00
|
|
|
});
|
2024-06-12 15:10:36 +07:00
|
|
|
return newAttr;
|
|
|
|
|
});
|
2024-06-12 14:55:20 +07:00
|
|
|
|
2024-06-11 18:27:54 +07:00
|
|
|
await Promise.all([
|
|
|
|
|
this.authRoleRepo.save(record),
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|