แก้เช็คซ้ำ posDict

This commit is contained in:
Bright 2024-02-15 10:25:10 +07:00
parent 45050a279f
commit 8756279309

View file

@ -112,75 +112,21 @@ export class PositionController extends Controller {
}
}
const chk_posDictName = await this.posDictRepository.findOne({
where: { posDictName: posDict.posDictName },
const rowRepeated = await this.posDictRepository.findOne({
where: {
posDictName: Like(`%${posDict.posDictName.trim()}%`),
posDictField: Like(`%${posDict.posDictField.trim()}%`),
posTypeId: posDict.posTypeId,
posLevelId: posDict.posLevelId,
posExecutiveId: String(posDict.posExecutiveId),
posDictExecutiveField: Like(`%${posDict.posDictExecutiveField.trim()}%`),
posDictArea: Like(`%${posDict.posDictArea.trim()}%`)
},
});
if (chk_posDictName) {
if (rowRepeated) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ตำแหน่งในสายงาน: " + chk_posDictName.posDictName + " มีอยู่ในระบบแล้ว",
);
}
const chk_posDictField = await this.posDictRepository.findOne({
where: { posDictField: posDict.posDictField },
});
if (chk_posDictField) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"สายงาน: " + chk_posDictField.posDictField + " มีอยู่ในระบบแล้ว",
);
}
const chk_posTypeId = await this.posDictRepository.findOne({
where: { posTypeId: posDict.posTypeId },
});
if (chk_posTypeId) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ตำแหน่งประเภท: " + chk_posTypeId.posTypeId + " มีอยู่ในระบบแล้ว",
);
}
const chk_posLevelId = await this.posDictRepository.findOne({
where: { posLevelId: posDict.posLevelId },
});
if (chk_posLevelId) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ระดับตำแหน่ง: " + chk_posLevelId.posLevelId + " มีอยู่ในระบบแล้ว",
);
}
const chk_posExecutiveId = await this.posDictRepository.findOne({
where: { posExecutiveId: String(posDict.posExecutiveId) },
});
if (chk_posExecutiveId) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ตำแหน่งทางการบริหาร: " + chk_posExecutiveId.posExecutiveId + " มีอยู่ในระบบแล้ว",
);
}
const chk_posDictExecutiveField = await this.posDictRepository.findOne({
where: { posDictExecutiveField: posDict.posDictExecutiveField },
});
if (chk_posDictExecutiveField) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ด้านทางการบริหาร: " +
chk_posDictExecutiveField.posDictExecutiveField +
" มีอยู่ในระบบแล้ว",
);
}
const chk_posDictArea = await this.posDictRepository.findOne({
where: { posDictArea: posDict.posDictArea },
});
if (chk_posDictArea) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ด้าน/สาขา: " + chk_posDictArea.posDictArea + " มีอยู่ในระบบแล้ว",
"ข้อมูล Row นี้มีอยู่ในระบบแล้ว",
);
}