diff --git a/src/controllers/EmployeePosLevelController.ts b/src/controllers/EmployeePosLevelController.ts index 4ae94e7d..31e1271f 100644 --- a/src/controllers/EmployeePosLevelController.ts +++ b/src/controllers/EmployeePosLevelController.ts @@ -58,7 +58,6 @@ export class EmployeePosLevelController extends Controller { const EmpPosType = await this.employeePosTypeRepository.findOne({ where: { id: requestBody.posTypeId }, - }); if (!EmpPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); @@ -112,12 +111,12 @@ export class EmployeePosLevelController extends Controller { const chkEmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { - id: Not(id), - posLevelName: requestBody.posLevelName, - posTypeId: requestBody.posTypeId, + id: Not(id), + posLevelName: requestBody.posLevelName, + posTypeId: requestBody.posTypeId, }, - }); - + }); + if (chkEmpPosLevel) { throw new HttpError( HttpStatusCode.NOT_FOUND, @@ -198,6 +197,7 @@ export class EmployeePosLevelController extends Controller { const empPosLevel = await this.employeePosLevelRepository.find({ relations: ["posType"], select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"], + order: { posLevelRank: "ASC" }, }); const mapEmpPosLevel = empPosLevel.map((item) => ({ id: item.id, diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts index 05209338..2dc61fa3 100644 --- a/src/controllers/EmployeePosTypeController.ts +++ b/src/controllers/EmployeePosTypeController.ts @@ -166,12 +166,14 @@ export class EmployeePosTypeController extends Controller { posTypeName: getEmpPosType.posTypeName, posTypeRank: getEmpPosType.posTypeRank, posTypeShortName: getEmpPosType.posTypeShortName, - posLevels: getEmpPosType.posLevels.map((empPosLevel) => ({ - id: empPosLevel.id, - posLevelName: empPosLevel.posLevelName, - posLevelRank: empPosLevel.posLevelRank, - posLevelAuthority: empPosLevel.posLevelAuthority, - })), + posLevels: getEmpPosType.posLevels + .sort((a, b) => a.posLevelRank - b.posLevelRank) + .map((empPosLevel) => ({ + id: empPosLevel.id, + posLevelName: empPosLevel.posLevelName, + posLevelRank: empPosLevel.posLevelRank, + posLevelAuthority: empPosLevel.posLevelAuthority, + })), }; return new HttpSuccess(mapGetEmpPosType); @@ -188,6 +190,7 @@ export class EmployeePosTypeController extends Controller { const empPosType = await this.employeePosTypeRepository.find({ relations: ["posLevels"], select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"], + order: { posTypeRank: "ASC" }, }); const mapEmpPosType = empPosType.map((item) => ({ @@ -195,12 +198,14 @@ export class EmployeePosTypeController extends Controller { posTypeName: item.posTypeName, posTypeRank: item.posTypeRank, posTypeShortName: item.posTypeShortName, - posLevels: item.posLevels.map((empPosLevel) => ({ - id: empPosLevel.id, - posLevelName: empPosLevel.posLevelName, - posLevelRank: empPosLevel.posLevelRank, - posLevelAuthority: empPosLevel.posLevelAuthority, - })), + posLevels: item.posLevels + .sort((a, b) => a.posLevelRank - b.posLevelRank) + .map((empPosLevel) => ({ + id: empPosLevel.id, + posLevelName: empPosLevel.posLevelName, + posLevelRank: empPosLevel.posLevelRank, + posLevelAuthority: empPosLevel.posLevelAuthority, + })), })); return new HttpSuccess(mapEmpPosType); } diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index 3da598d0..4e5b5b78 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -14,7 +14,7 @@ import { Get, Example, } from "tsoa"; -import { Not } from "typeorm" +import { Not } from "typeorm"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; @@ -54,6 +54,27 @@ export class PosExecutiveController extends Controller { } const posExecutive = Object.assign(new PosExecutive(), requestBody); + if (requestBody.posExecutivePriority == null) { + const checkPriority = await this.posExecutiveRepository.findOne({ + order: { posExecutivePriority: "DESC" }, + }); + if (checkPriority == null) { + posExecutive.posExecutivePriority = 1; + } else { + posExecutive.posExecutivePriority = checkPriority.posExecutivePriority + 1; + } + } else { + posExecutive.posExecutivePriority = requestBody.posExecutivePriority; + } + const _checkPriority = await this.posExecutiveRepository.findOne({ + where: { + posExecutivePriority: posExecutive.posExecutivePriority, + }, + }); + + if (_checkPriority) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับนี้มีอยู่ในระบบแล้ว"); + } posExecutive.createdUserId = request.user.sub; posExecutive.createdFullName = request.user.name; posExecutive.lastUpdateUserId = request.user.sub; @@ -84,17 +105,40 @@ export class PosExecutiveController extends Controller { const checkName = await this.posExecutiveRepository.findOne({ where: { id: Not(id), - posExecutiveName: requestBody.posExecutiveName + posExecutiveName: requestBody.posExecutiveName, }, }); if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + if (requestBody.posExecutivePriority == null) { + const checkPriority = await this.posExecutiveRepository.findOne({ + order: { posExecutivePriority: "DESC" }, + }); + if (checkPriority == null) { + posExecutive.posExecutivePriority = 1; + } else { + posExecutive.posExecutivePriority = checkPriority.posExecutivePriority + 1; + } + } else { + posExecutive.posExecutivePriority = requestBody.posExecutivePriority; + } + const _checkPriority = await this.posExecutiveRepository.findOne({ + where: { + id: Not(id), + posExecutivePriority: posExecutive.posExecutivePriority, + }, + }); + if (_checkPriority) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับนี้มีอยู่ในระบบแล้ว"); + } + + posExecutive.posExecutiveName = requestBody.posExecutiveName; posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateFullName = request.user.name; - this.posExecutiveRepository.merge(posExecutive, requestBody); + // this.posExecutiveRepository.merge(posExecutive, requestBody); await this.posExecutiveRepository.save(posExecutive); return new HttpSuccess(); } diff --git a/src/entities/PosExecutive.ts b/src/entities/PosExecutive.ts index 325383c9..696fbddf 100644 --- a/src/entities/PosExecutive.ts +++ b/src/entities/PosExecutive.ts @@ -32,7 +32,7 @@ export class CreatePosExecutive { posExecutiveName: string; @Column() - posExecutivePriority: number; + posExecutivePriority: number | null; } export type UpdatePosExecutive = Partial;