fix api role

This commit is contained in:
AdisakKanthawilang 2024-06-11 18:27:54 +07:00
parent 543ef19664
commit ba065b4299
4 changed files with 71 additions and 20 deletions

View file

@ -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}")

View file

@ -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, "ไม่สามารถลบข้อมูลได้");
}

View file

@ -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}")

View file

@ -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)