diff --git a/src/controllers/PersonalController.ts b/src/controllers/PersonalController.ts index 09d9c18..95b580d 100644 --- a/src/controllers/PersonalController.ts +++ b/src/controllers/PersonalController.ts @@ -21,13 +21,14 @@ import { Personal, PostPersonal } from "../entities/Personal"; import permission from "../interfaces/permission"; import { Assign } from "../entities/Assign"; import { Brackets } from "typeorm"; +import CallAPI from "../interfaces/call-api"; @Route("api/v1/probation/personal") @Tags("Personal") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" ) export class PersonalController extends Controller { private personalRepository = AppDataSource.getRepository(Personal); @@ -40,7 +41,10 @@ export class PersonalController extends Controller { * */ @Post("add") - async AddPersonal(@Body() requestBody: PostPersonal, @Request() request: RequestWithUser) { + async AddPersonal( + @Body() requestBody: PostPersonal, + @Request() request: RequestWithUser + ) { try { await new permission().PermissionCreate(request, "SYS_PROBATION"); @@ -50,19 +54,31 @@ export class PersonalController extends Controller { if (checkPersonal > 0) { throw new HttpError( HttpStatusCode.BAD_REQUEST, - "ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว", + "ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว" ); } - let organization = await (requestBody.orgChild4Name ? requestBody.orgChild4Name + "\n" : ""); - organization += await (requestBody.orgChild3Name ? requestBody.orgChild3Name + "\n" : ""); - organization += await (requestBody.orgChild2Name ? requestBody.orgChild2Name + "\n" : ""); - organization += await (requestBody.orgChild1Name ? requestBody.orgChild1Name + "\n" : ""); - organization += await (requestBody.orgRootName ? requestBody.orgRootName : ""); + let organization = await (requestBody.orgChild4Name + ? requestBody.orgChild4Name + "\n" + : ""); + organization += await (requestBody.orgChild3Name + ? requestBody.orgChild3Name + "\n" + : ""); + 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()); personalData.personal_id = requestBody.id; - personalData.order_number = requestBody.order_number ? requestBody.order_number : ""; + personalData.order_number = requestBody.order_number + ? requestBody.order_number + : ""; personalData.probation_status = 1; personalData.createdUserId = request.user.sub; personalData.createdFullName = request.user.name; @@ -74,8 +90,12 @@ export class PersonalController extends Controller { personalData.firstName = requestBody.firstName; personalData.lastName = requestBody.lastName; personalData.isProbation = requestBody.isProbation ? 1 : 0; - personalData.positionLevelName = requestBody.posLevelName ? requestBody.posLevelName : ""; - personalData.positionName = requestBody.position ? requestBody.position : ""; + personalData.positionLevelName = requestBody.posLevelName + ? requestBody.posLevelName + : ""; + personalData.positionName = requestBody.position + ? requestBody.position + : ""; personalData.positionLineName = requestBody.posLineName; personalData.positionTypeName = requestBody.posTypeName; personalData.posNo = requestBody.posNo ? requestBody.posNo : ""; @@ -94,7 +114,8 @@ export class PersonalController extends Controller { personalData.child3Dna = requestBody.orgChild3Dna; personalData.child4Dna = requestBody.orgChild4Dna; - const getFieldValue = (field: string | null | undefined): string => field ?? ""; + const getFieldValue = (field: string | null | undefined): string => + field ?? ""; personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name); personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name); personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name); @@ -110,7 +131,11 @@ export class PersonalController extends Controller { } catch (error: any) { if (error instanceof HttpError) { throw error; - } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); + } else + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + error.message + ); } } @@ -126,42 +151,122 @@ export class PersonalController extends Controller { @Query() keyword: string = "", @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, - @Request() request: RequestWithUser, + @Request() request: RequestWithUser ) { try { - await new permission().PermissionList(request, "SYS_PROBATION"); - const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION"); + // await new permission().PermissionList(request, "SYS_PROBATION"); + // const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION"); - const conditions: any = {}; - if (status) { - conditions.probation_status = status; - } + let _data = await new permission().PermissionOrgList( + request, + "SYS_DEV_PROJECT" + ); + await new CallAPI() + .PostData(request, "/org/finddna", _data) + .then((x) => { + _data = x; + }) + .catch((x) => {}); - if (_data.root != undefined && _data.root != null && _data.root[0] != null) { - conditions.root = _data.root; - } + // const conditions: any = {}; + // if (status) { + // conditions.probation_status = status; + // } - if (_data.child1 != undefined && _data.child1 != null && _data.child1[0] != null) { - conditions.child1 = _data.child1; - } + // if ( + // _data.root != undefined && + // _data.root != null && + // _data.root[0] != null + // ) { + // conditions.root = _data.root; + // } - if (_data.child2 != undefined && _data.child2 != null && _data.child2[0] != null) { - conditions.child2 = _data.child2; - } + // if ( + // _data.child1 != undefined && + // _data.child1 != null && + // _data.child1[0] != null + // ) { + // conditions.child1 = _data.child1; + // } - if (_data.child3 != undefined && _data.child3 != null && _data.child3[0] != null) { - conditions.child3 = _data.child3; - } + // if ( + // _data.child2 != undefined && + // _data.child2 != null && + // _data.child2[0] != null + // ) { + // conditions.child2 = _data.child2; + // } - if (_data.child4 != undefined && _data.child4 != null && _data.child4[0] != null) { - conditions.child4 = _data.child4; - } + // if ( + // _data.child3 != undefined && + // _data.child3 != null && + // _data.child3[0] != null + // ) { + // conditions.child3 = _data.child3; + // } + + // if ( + // _data.child4 != undefined && + // _data.child4 != null && + // _data.child4[0] != null + // ) { + // conditions.child4 = _data.child4; + // } const searchKeyword = await (keyword ? keyword.trim() : null); const [lists, total] = await AppDataSource.getRepository(Personal) .createQueryBuilder("personal") - .where(conditions) + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `personal.rootDna IN (:...root)` + : `personal.rootDna is null` + : "1=1", + { + root: _data.root, + } + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `personal.child1Dna IN (:...child1)` + : `personal.child1Dna is null` + : "1=1", + { + child1: _data.child1, + } + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `personal.child2Dna IN (:...child2)` + : `personal.child2Dna is null` + : "1=1", + { + child2: _data.child2, + } + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `personal.child3Dna IN (:...child3)` + : `personal.child3Dna is null` + : "1=1", + { + child3: _data.child3, + } + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `personal.child4Dna IN (:...child4)` + : `personal.child4Dna is null` + : "1=1", + { + child4: _data.child4, + } + ) .andWhere( new Brackets((qb) => { qb.orWhere( @@ -170,21 +275,33 @@ export class PersonalController extends Controller { : "1=1", { keyword: `%${searchKeyword}%`, - }, + } ); - qb.orWhere(searchKeyword ? `positionName like '%${keyword}%'` : "1=1", { - keyword: `%${searchKeyword}%`, - }); - qb.orWhere(searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", { - keyword: `%${searchKeyword}%`, - }); - qb.orWhere(searchKeyword ? `organization like '%${keyword}%'` : "1=1", { - keyword: `%${searchKeyword}%`, - }); - qb.orWhere(searchKeyword ? `order_number like '%${keyword}%'` : "1=1", { - keyword: `%${searchKeyword}%`, - }); - }), + qb.orWhere( + searchKeyword ? `positionName like '%${keyword}%'` : "1=1", + { + keyword: `%${searchKeyword}%`, + } + ); + qb.orWhere( + searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", + { + keyword: `%${searchKeyword}%`, + } + ); + qb.orWhere( + searchKeyword ? `organization like '%${keyword}%'` : "1=1", + { + keyword: `%${searchKeyword}%`, + } + ); + qb.orWhere( + searchKeyword ? `order_number like '%${keyword}%'` : "1=1", + { + keyword: `%${searchKeyword}%`, + } + ); + }) ) .orderBy("updatedAt", "DESC") .skip((page - 1) * pageSize) @@ -192,7 +309,10 @@ export class PersonalController extends Controller { .getManyAndCount(); if (!lists) { - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถแสดงข้อมูลได้"); + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถแสดงข้อมูลได้" + ); } let result: any = []; @@ -205,7 +325,8 @@ export class PersonalController extends Controller { await result.push({ personal_id: lists[i].personal_id, ordering: i + 1, - name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName, + name: + lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName, prefixName: lists[i].prefixName, firstName: lists[i].firstName, lastName: lists[i].lastName, @@ -250,7 +371,11 @@ export class PersonalController extends Controller { } catch (error: any) { if (error instanceof HttpError) { throw error; - } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); + } else + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + error.message + ); } } @@ -261,10 +386,18 @@ export class PersonalController extends Controller { * */ @Get("") - async GetPersonal(@Request() request: RequestWithUser, @Query() personal_id: string) { + async GetPersonal( + @Request() request: RequestWithUser, + @Query() personal_id: string + ) { try { - let _workflow = await new permission().Workflow(request, personal_id, "SYS_PROBATION"); - if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION"); + let _workflow = await new permission().Workflow( + request, + personal_id, + "SYS_PROBATION" + ); + if (_workflow == false) + await new permission().PermissionGet(request, "SYS_PROBATION"); const person = await this.personalRepository.findOne({ where: { personal_id: personal_id }, }); @@ -293,7 +426,11 @@ export class PersonalController extends Controller { } catch (error: any) { if (error instanceof HttpError) { throw error; - } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); + } else + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + error.message + ); } } }