fix bug permission

This commit is contained in:
Warunee Tamkoo 2025-10-21 11:47:06 +07:00
parent 1719225725
commit 9d86cbb228

View file

@ -28,7 +28,7 @@ import CallAPI from "../interfaces/call-api";
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
) )
export class PersonalController extends Controller { export class PersonalController extends Controller {
private personalRepository = AppDataSource.getRepository(Personal); private personalRepository = AppDataSource.getRepository(Personal);
@ -41,10 +41,7 @@ export class PersonalController extends Controller {
* *
*/ */
@Post("add") @Post("add")
async AddPersonal( async AddPersonal(@Body() requestBody: PostPersonal, @Request() request: RequestWithUser) {
@Body() requestBody: PostPersonal,
@Request() request: RequestWithUser
) {
try { try {
await new permission().PermissionCreate(request, "SYS_PROBATION"); await new permission().PermissionCreate(request, "SYS_PROBATION");
@ -54,31 +51,19 @@ export class PersonalController extends Controller {
if (checkPersonal > 0) { if (checkPersonal > 0) {
throw new HttpError( throw new HttpError(
HttpStatusCode.BAD_REQUEST, HttpStatusCode.BAD_REQUEST,
"ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว" "ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว",
); );
} }
let organization = await (requestBody.orgChild4Name let organization = await (requestBody.orgChild4Name ? requestBody.orgChild4Name + "\n" : "");
? requestBody.orgChild4Name + "\n" organization += await (requestBody.orgChild3Name ? requestBody.orgChild3Name + "\n" : "");
: ""); organization += await (requestBody.orgChild2Name ? requestBody.orgChild2Name + "\n" : "");
organization += await (requestBody.orgChild3Name organization += await (requestBody.orgChild1Name ? requestBody.orgChild1Name + "\n" : "");
? requestBody.orgChild3Name + "\n" organization += await (requestBody.orgRootName ? requestBody.orgRootName : "");
: "");
organization += await (requestBody.orgChild2Name
? requestBody.orgChild2Name + "\n"
: "");
organization += await (requestBody.orgChild1Name
? requestBody.orgChild1Name + "\n"
: "");
organization += await (requestBody.orgRootName
? requestBody.orgRootName
: "");
const personalData = Object.assign(new Personal()); const personalData = Object.assign(new Personal());
personalData.personal_id = requestBody.id; personalData.personal_id = requestBody.id;
personalData.order_number = requestBody.order_number personalData.order_number = requestBody.order_number ? requestBody.order_number : "";
? requestBody.order_number
: "";
personalData.probation_status = 1; personalData.probation_status = 1;
personalData.createdUserId = request.user.sub; personalData.createdUserId = request.user.sub;
personalData.createdFullName = request.user.name; personalData.createdFullName = request.user.name;
@ -90,12 +75,8 @@ export class PersonalController extends Controller {
personalData.firstName = requestBody.firstName; personalData.firstName = requestBody.firstName;
personalData.lastName = requestBody.lastName; personalData.lastName = requestBody.lastName;
personalData.isProbation = requestBody.isProbation ? 1 : 0; personalData.isProbation = requestBody.isProbation ? 1 : 0;
personalData.positionLevelName = requestBody.posLevelName personalData.positionLevelName = requestBody.posLevelName ? requestBody.posLevelName : "";
? requestBody.posLevelName personalData.positionName = requestBody.position ? requestBody.position : "";
: "";
personalData.positionName = requestBody.position
? requestBody.position
: "";
personalData.positionLineName = requestBody.posLineName; personalData.positionLineName = requestBody.posLineName;
personalData.positionTypeName = requestBody.posTypeName; personalData.positionTypeName = requestBody.posTypeName;
personalData.posNo = requestBody.posNo ? requestBody.posNo : ""; personalData.posNo = requestBody.posNo ? requestBody.posNo : "";
@ -114,8 +95,7 @@ export class PersonalController extends Controller {
personalData.child3Dna = requestBody.orgChild3Dna; personalData.child3Dna = requestBody.orgChild3Dna;
personalData.child4Dna = requestBody.orgChild4Dna; personalData.child4Dna = requestBody.orgChild4Dna;
const getFieldValue = (field: string | null | undefined): string => const getFieldValue = (field: string | null | undefined): string => field ?? "";
field ?? "";
personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name); personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name);
personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name); personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name);
personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name); personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name);
@ -131,11 +111,7 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
@ -147,22 +123,19 @@ export class PersonalController extends Controller {
*/ */
@Get("list") @Get("list")
async ListPersonal( async ListPersonal(
@Request() request: RequestWithUser, @Request() request: RequestWithUser,
@Query() status: string = "", @Query() status: string = "",
@Query() keyword: string = "", @Query() keyword: string = "",
@Query("page") page: number = 1, @Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
@Query("sortBy") sortBy?: string, @Query("sortBy") sortBy?: string,
@Query("descending") descending?: boolean @Query("descending") descending?: boolean,
) { ) {
try { try {
// await new permission().PermissionList(request, "SYS_PROBATION"); // await new permission().PermissionList(request, "SYS_PROBATION");
// const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION"); // const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION");
let _data = await new permission().PermissionOrgList( let _data = await new permission().PermissionOrgList(request, "SYS_PROBATION");
request,
"SYS_DEV_PROJECT"
);
await new CallAPI() await new CallAPI()
.PostData(request, "/org/finddna", _data) .PostData(request, "/org/finddna", _data)
.then((x) => { .then((x) => {
@ -227,7 +200,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
root: _data.root, root: _data.root,
} },
) )
.andWhere( .andWhere(
_data.child1 != undefined && _data.child1 != null _data.child1 != undefined && _data.child1 != null
@ -237,7 +210,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child1: _data.child1, child1: _data.child1,
} },
) )
.andWhere( .andWhere(
_data.child2 != undefined && _data.child2 != null _data.child2 != undefined && _data.child2 != null
@ -247,7 +220,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child2: _data.child2, child2: _data.child2,
} },
) )
.andWhere( .andWhere(
_data.child3 != undefined && _data.child3 != null _data.child3 != undefined && _data.child3 != null
@ -257,7 +230,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child3: _data.child3, child3: _data.child3,
} },
) )
.andWhere( .andWhere(
_data.child4 != undefined && _data.child4 != null _data.child4 != undefined && _data.child4 != null
@ -267,7 +240,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child4: _data.child4, child4: _data.child4,
} },
) )
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
@ -277,76 +250,48 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
} },
); );
qb.orWhere( qb.orWhere(searchKeyword ? `positionName like '%${keyword}%'` : "1=1", {
searchKeyword ? `positionName like '%${keyword}%'` : "1=1", keyword: `%${searchKeyword}%`,
{ });
keyword: `%${searchKeyword}%`, qb.orWhere(searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", {
} keyword: `%${searchKeyword}%`,
); });
qb.orWhere( qb.orWhere(searchKeyword ? `organization like '%${keyword}%'` : "1=1", {
searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", keyword: `%${searchKeyword}%`,
{ });
keyword: `%${searchKeyword}%`, qb.orWhere(searchKeyword ? `order_number like '%${keyword}%'` : "1=1", {
} keyword: `%${searchKeyword}%`,
); });
qb.orWhere( }),
searchKeyword ? `organization like '%${keyword}%'` : "1=1", );
{ if (sortBy) {
keyword: `%${searchKeyword}%`, if (sortBy === "position_line") {
} query = query.orderBy(`personal.positionName`, descending ? "DESC" : "ASC");
); } else if (sortBy === "position_level") {
qb.orWhere( query = query.orderBy(`personal.positionLevelName`, descending ? "DESC" : "ASC");
searchKeyword ? `order_number like '%${keyword}%'` : "1=1", } else if (sortBy === "position_type") {
{ query = query.orderBy(`personal.positionTypeName`, descending ? "DESC" : "ASC");
keyword: `%${searchKeyword}%`, } else if (sortBy === "name") {
} query = query
); .orderBy(`personal.prefixName`, descending ? "DESC" : "ASC")
}) .addOrderBy(`personal.firstName`, descending ? "DESC" : "ASC")
) .addOrderBy(`personal.lastName`, descending ? "DESC" : "ASC");
if (sortBy) { } else {
if(sortBy === "position_line"){ query = query.orderBy(`personal.${sortBy}`, descending ? "DESC" : "ASC");
query = query.orderBy( }
`personal.positionName`, } else {
descending ? "DESC" : "ASC" query = query.orderBy("updatedAt", "DESC");
); }
}else if(sortBy === "position_level"){
query = query.orderBy(
`personal.positionLevelName`,
descending ? "DESC" : "ASC"
);
}else if(sortBy === "position_type"){
query = query.orderBy(
`personal.positionTypeName`,
descending ? "DESC" : "ASC"
);
}else if(sortBy === "name"){
query = query
.orderBy(`personal.prefixName`,descending ? "DESC" : "ASC")
.addOrderBy(`personal.firstName`,descending ? "DESC" : "ASC")
.addOrderBy(`personal.lastName`,descending ? "DESC" : "ASC");
}else{
query = query.orderBy(
`personal.${sortBy}`,
descending ? "DESC" : "ASC"
);
}
}else{
query = query.orderBy("updatedAt", "DESC")
}
const [lists, total] = await query
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const [lists, total] = await query
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
if (!lists) { if (!lists) {
throw new HttpError( throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถแสดงข้อมูลได้");
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถแสดงข้อมูลได้"
);
} }
let result: any = []; let result: any = [];
@ -359,8 +304,7 @@ export class PersonalController extends Controller {
await result.push({ await result.push({
personal_id: lists[i].personal_id, personal_id: lists[i].personal_id,
ordering: i + 1, ordering: i + 1,
name: name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
prefixName: lists[i].prefixName, prefixName: lists[i].prefixName,
firstName: lists[i].firstName, firstName: lists[i].firstName,
lastName: lists[i].lastName, lastName: lists[i].lastName,
@ -405,11 +349,7 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
@ -420,18 +360,10 @@ export class PersonalController extends Controller {
* *
*/ */
@Get("") @Get("")
async GetPersonal( async GetPersonal(@Request() request: RequestWithUser, @Query() personal_id: string) {
@Request() request: RequestWithUser,
@Query() personal_id: string
) {
try { try {
let _workflow = await new permission().Workflow( let _workflow = await new permission().Workflow(request, personal_id, "SYS_PROBATION");
request, if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION");
personal_id,
"SYS_PROBATION"
);
if (_workflow == false)
await new permission().PermissionGet(request, "SYS_PROBATION");
const person = await this.personalRepository.findOne({ const person = await this.personalRepository.findOne({
where: { personal_id: personal_id }, where: { personal_id: personal_id },
}); });
@ -460,11 +392,7 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
} }