แก้ query ค้นหา

This commit is contained in:
Kittapath 2024-01-31 15:02:54 +07:00
parent d74c3140fa
commit 53b2146057
2 changed files with 27 additions and 61 deletions

View file

@ -787,6 +787,5 @@ export class OrganizationController extends Controller {
}));
return new HttpSuccess(_data);
}
return new HttpSuccess(_data);
}
}

View file

@ -24,7 +24,7 @@ import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { CreatePosDict, PosDict } from "../entities/PosDict";
import HttpError from "../interfaces/http-error";
import { Like } from "typeorm";
import { In, Like } from "typeorm";
import { CreatePosMaster, PosMaster } from "../entities/PosMaster";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
@ -267,13 +267,14 @@ export class PositionController extends Controller {
*
*/
@Get("position")
async findPosition(@Query("keyword") keyword: string, @Query("type") type: string) {
async findPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) {
try {
let findPosDict: any;
switch (type) {
case "positionName":
findPosDict = await this.posDictRepository.find({
where: { posDictName: Like(`%${keyword}%`) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
@ -283,6 +284,7 @@ export class PositionController extends Controller {
case "positionField":
findPosDict = await this.posDictRepository.find({
where: { posDictField: Like(`%${keyword}%`) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
@ -292,61 +294,40 @@ export class PositionController extends Controller {
case "positionType":
const findTypes: PosType[] = await this.posTypeRepository.find({
where: { posTypeName: Like(`%${keyword}%`) },
select: ["id"],
});
findPosDict = await this.posDictRepository.find({
where: { posTypeId: In(findTypes.map((x) => x.id)) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findTypes) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword + " ใน posType");
}
for (const types of findTypes) {
findPosDict = await this.posDictRepository.find({ where: { posTypeId: types.id } });
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
}
break;
case "positionLevel":
const findLevel: PosLevel[] = await this.posLevelRepository.find({
where: { posLevelName: Like(`%${keyword}%`) },
select: ["id"],
});
findPosDict = await this.posDictRepository.find({
where: { posLevelId: In(findLevel.map((x) => x.id)) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูล " + keyword + " ใน posLevel",
);
}
for (const types of findLevel) {
findPosDict = await this.posDictRepository.find({ where: { posLevelId: types.id } });
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
}
break;
case "positionExecutive":
const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({
where: { posExecutiveName: Like(`%${keyword}%`) },
select: ["id"],
});
findPosDict = await this.posDictRepository.find({
where: { posExecutiveId: In(findExecutive.map((x) => x.id)) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findExecutive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูล " + keyword + " ใน posExecutive",
);
}
for (const types of findExecutive) {
findPosDict = await this.posDictRepository.find({
where: { posExecutiveId: types.id },
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
}
break;
case "positionExecutiveField":
findPosDict = await this.posDictRepository.find({
where: { posDictExecutiveField: Like(`%${keyword}%`) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
@ -356,6 +337,7 @@ export class PositionController extends Controller {
case "positionArea":
findPosDict = await this.posDictRepository.find({
where: { posDictArea: Like(`%${keyword}%`) },
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
@ -363,42 +345,27 @@ export class PositionController extends Controller {
break;
default:
findPosDict = await this.posDictRepository.find();
findPosDict = await this.posDictRepository.find({
relations: ["posType", "posLevel", "posExecutive"],
});
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
break;
}
if (!findPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
}
const mapDataPosDict = await Promise.all(
findPosDict.map(async (item: any) => {
const posTypeName = await this.posTypeRepository.findOne({
where: { id: item.posTypeId },
select: ["posTypeName"],
});
const posLevelName = await this.posLevelRepository.findOne({
where: { id: item.posLevelId },
select: ["posLevelName"],
});
const posExecutiveName = await this.posExecutiveRepository.findOne({
where: { id: item.posExecutiveId },
select: ["posExecutiveName"],
});
return {
id: item.id,
positionName: item.posDictName,
positionField: item.posDictField,
posTypeId: item.posTypeId,
posTypeName: posTypeName ? posTypeName.posTypeName : null,
posTypeName: item.posType == null ? null : item.posType.posTypeName,
posLevelId: item.posLevelId,
posLevelName: posLevelName ? posLevelName.posLevelName : null,
posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName,
posExecutiveId: item.posExecutiveId,
posExecutiveName: posExecutiveName ? posExecutiveName.posExecutiveName : null,
posExecutiveName: item.posExecutive == null ? null : item.posExecutive.posExecutiveName,
positionExecutiveField: item.posDictExecutiveField,
positionArea: item.posDictArea,
positionIsSelected: false,