260 lines
8.3 KiB
TypeScript
260 lines
8.3 KiB
TypeScript
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, CreateAddAuthRole } from "../entities/AuthRole";
|
|
import { AuthRoleAttr } from "../entities/AuthRoleAttr";
|
|
import { PosMaster } from "../entities/PosMaster";
|
|
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
|
|
|
const REDIS_HOST = process.env.REDIS_HOST;
|
|
const REDIS_PORT = process.env.REDIS_PORT;
|
|
@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);
|
|
private posMasterRepository = AppDataSource.getRepository(PosMaster);
|
|
private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster);
|
|
private redis = require("redis");
|
|
|
|
@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);
|
|
}
|
|
|
|
@Post("govoment")
|
|
public async AddAuthRoleGovoment(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: CreateAddAuthRole,
|
|
) {
|
|
let NULL_: any = null;
|
|
let getDetail;
|
|
|
|
if (body.authRoleId == "") {
|
|
body.authRoleId = NULL_;
|
|
} else {
|
|
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
|
|
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
|
}
|
|
|
|
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);
|
|
|
|
// เช็คว่าถ้ามีค่า current_holderId ให้ลบ key สิทธิ์ใน redis
|
|
if (posMaster.current_holderId) {
|
|
const redisClient = await this.redis.createClient({
|
|
host: REDIS_HOST,
|
|
port: REDIS_PORT,
|
|
});
|
|
redisClient.del("role_" + posMaster.current_holderId, (err: Error, response: Response) => {
|
|
if (err) throw err;
|
|
console.log(response);
|
|
});
|
|
}
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
@Post("employee")
|
|
public async AddAuthRoleEmployee(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: CreateAddAuthRole,
|
|
) {
|
|
let NULL_: any = null;
|
|
let getDetail;
|
|
|
|
if (body.authRoleId == "") {
|
|
body.authRoleId = NULL_;
|
|
} else {
|
|
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
|
|
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
|
}
|
|
|
|
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);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
@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)),
|
|
// ]);
|
|
|
|
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,
|
|
});
|
|
return newAttr;
|
|
});
|
|
|
|
await Promise.all([
|
|
this.authRoleRepo.save(record),
|
|
...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)),
|
|
]);
|
|
|
|
const redisClient = await this.redis.createClient({
|
|
host: REDIS_HOST,
|
|
port: REDIS_PORT,
|
|
});
|
|
|
|
await redisClient.flushdb(function (err: any, succeeded: any) {
|
|
console.log(succeeded); // will be true if successfull
|
|
});
|
|
|
|
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();
|
|
}
|
|
}
|