sort ตำแหน่งลูกจ้าง

This commit is contained in:
Kittapath 2024-03-18 10:40:28 +07:00
parent 2972d6ef5e
commit 222a2a5b6b
4 changed files with 71 additions and 22 deletions

View file

@ -58,7 +58,6 @@ export class EmployeePosLevelController extends Controller {
const EmpPosType = await this.employeePosTypeRepository.findOne({ const EmpPosType = await this.employeePosTypeRepository.findOne({
where: { id: requestBody.posTypeId }, where: { id: requestBody.posTypeId },
}); });
if (!EmpPosType) { if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้");
@ -112,11 +111,11 @@ export class EmployeePosLevelController extends Controller {
const chkEmpPosLevel = await this.employeePosLevelRepository.findOne({ const chkEmpPosLevel = await this.employeePosLevelRepository.findOne({
where: { where: {
id: Not(id), id: Not(id),
posLevelName: requestBody.posLevelName, posLevelName: requestBody.posLevelName,
posTypeId: requestBody.posTypeId, posTypeId: requestBody.posTypeId,
}, },
}); });
if (chkEmpPosLevel) { if (chkEmpPosLevel) {
throw new HttpError( throw new HttpError(
@ -198,6 +197,7 @@ export class EmployeePosLevelController extends Controller {
const empPosLevel = await this.employeePosLevelRepository.find({ const empPosLevel = await this.employeePosLevelRepository.find({
relations: ["posType"], relations: ["posType"],
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"], select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"],
order: { posLevelRank: "ASC" },
}); });
const mapEmpPosLevel = empPosLevel.map((item) => ({ const mapEmpPosLevel = empPosLevel.map((item) => ({
id: item.id, id: item.id,

View file

@ -166,12 +166,14 @@ export class EmployeePosTypeController extends Controller {
posTypeName: getEmpPosType.posTypeName, posTypeName: getEmpPosType.posTypeName,
posTypeRank: getEmpPosType.posTypeRank, posTypeRank: getEmpPosType.posTypeRank,
posTypeShortName: getEmpPosType.posTypeShortName, posTypeShortName: getEmpPosType.posTypeShortName,
posLevels: getEmpPosType.posLevels.map((empPosLevel) => ({ posLevels: getEmpPosType.posLevels
id: empPosLevel.id, .sort((a, b) => a.posLevelRank - b.posLevelRank)
posLevelName: empPosLevel.posLevelName, .map((empPosLevel) => ({
posLevelRank: empPosLevel.posLevelRank, id: empPosLevel.id,
posLevelAuthority: empPosLevel.posLevelAuthority, posLevelName: empPosLevel.posLevelName,
})), posLevelRank: empPosLevel.posLevelRank,
posLevelAuthority: empPosLevel.posLevelAuthority,
})),
}; };
return new HttpSuccess(mapGetEmpPosType); return new HttpSuccess(mapGetEmpPosType);
@ -188,6 +190,7 @@ export class EmployeePosTypeController extends Controller {
const empPosType = await this.employeePosTypeRepository.find({ const empPosType = await this.employeePosTypeRepository.find({
relations: ["posLevels"], relations: ["posLevels"],
select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"], select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"],
order: { posTypeRank: "ASC" },
}); });
const mapEmpPosType = empPosType.map((item) => ({ const mapEmpPosType = empPosType.map((item) => ({
@ -195,12 +198,14 @@ export class EmployeePosTypeController extends Controller {
posTypeName: item.posTypeName, posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank, posTypeRank: item.posTypeRank,
posTypeShortName: item.posTypeShortName, posTypeShortName: item.posTypeShortName,
posLevels: item.posLevels.map((empPosLevel) => ({ posLevels: item.posLevels
id: empPosLevel.id, .sort((a, b) => a.posLevelRank - b.posLevelRank)
posLevelName: empPosLevel.posLevelName, .map((empPosLevel) => ({
posLevelRank: empPosLevel.posLevelRank, id: empPosLevel.id,
posLevelAuthority: empPosLevel.posLevelAuthority, posLevelName: empPosLevel.posLevelName,
})), posLevelRank: empPosLevel.posLevelRank,
posLevelAuthority: empPosLevel.posLevelAuthority,
})),
})); }));
return new HttpSuccess(mapEmpPosType); return new HttpSuccess(mapEmpPosType);
} }

View file

@ -14,7 +14,7 @@ import {
Get, Get,
Example, Example,
} from "tsoa"; } from "tsoa";
import { Not } from "typeorm" import { Not } from "typeorm";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
@ -54,6 +54,27 @@ export class PosExecutiveController extends Controller {
} }
const posExecutive = Object.assign(new PosExecutive(), requestBody); 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.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name; posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateUserId = request.user.sub;
@ -84,17 +105,40 @@ export class PosExecutiveController extends Controller {
const checkName = await this.posExecutiveRepository.findOne({ const checkName = await this.posExecutiveRepository.findOne({
where: { where: {
id: Not(id), id: Not(id),
posExecutiveName: requestBody.posExecutiveName posExecutiveName: requestBody.posExecutiveName,
}, },
}); });
if (checkName) { if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); 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.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name; posExecutive.lastUpdateFullName = request.user.name;
this.posExecutiveRepository.merge(posExecutive, requestBody); // this.posExecutiveRepository.merge(posExecutive, requestBody);
await this.posExecutiveRepository.save(posExecutive); await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess(); return new HttpSuccess();
} }

View file

@ -32,7 +32,7 @@ export class CreatePosExecutive {
posExecutiveName: string; posExecutiveName: string;
@Column() @Column()
posExecutivePriority: number; posExecutivePriority: number | null;
} }
export type UpdatePosExecutive = Partial<CreatePosExecutive>; export type UpdatePosExecutive = Partial<CreatePosExecutive>;