fix validate edit

This commit is contained in:
AdisakKanthawilang 2024-04-25 17:47:40 +07:00
parent b296c22fdf
commit ce5c0ddbe7

View file

@ -701,93 +701,6 @@ export class PositionController extends Controller {
); );
} }
// let fullPosRootName = `CONCAT(posMaster.orgRootShortName).trim() + (requestBody.posMasterNo))`;
// let filterRootName = `CONCAT(posMaster.orgRootShortName,posMaster.posMasterNo)`;
// let fullPosChild1Name = `CONCAT(posMaster.orgChild1ShortName).trim() + (requestBody.posMasterNo))`;
// let filterChild1Name = `CONCAT(posMaster.orgChild1ShortName,posMaster.posMasterNo)`;
// let fullPosChild2Name = `CONCAT(posMaster.orgChild2ShortName).trim() + (requestBody.posMasterNo))`;
// let filterChild2Name = `CONCAT(posMaster.orgChild2ShortName,posMaster.posMasterNo)`;
// let fullPosChild3Name = `CONCAT(posMaster.orgChild3ShortName).trim() + (requestBody.posMasterNo))`;
// let filterChild3Name = `CONCAT(posMaster.orgChild3ShortName,posMaster.posMasterNo)`;
// let fullPosChild4Name = `CONCAT(posMaster.orgChild4ShortName).trim() + (requestBody.posMasterNo))`;
// let filterChild4Name = `CONCAT(posMaster.orgChild4ShortName,posMaster.posMasterNo)`;
// const chk_FullName0 = await AppDataSource.getRepository(PosMaster)
// .createQueryBuilder("posMaster")
// .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId })
// .andWhere(`CONCAT(posMaster.orgRootShortName.trim(), :posMasterNo) = :fullPosRootName`, { posMasterNo: requestBody.posMasterNo, fullPosRootName })
// .andWhere(`CONCAT(posMaster.orgChild1ShortName.trim(), :posMasterNo) = :fullPosChild1Name`, { posMasterNo: requestBody.posMasterNo, fullPosChild1Name })
// .andWhere(`CONCAT(posMaster.orgChild2ShortName.trim(), :posMasterNo) = :fullPosChild2Name`, { posMasterNo: requestBody.posMasterNo, fullPosChild2Name })
// .andWhere(`CONCAT(posMaster.orgChild3ShortName.trim(), :posMasterNo) = :fullPosChild3Name`, { posMasterNo: requestBody.posMasterNo, fullPosChild3Name })
// .andWhere(`CONCAT(posMaster.orgChild4ShortName.trim(), :posMasterNo) = :fullPosChild4Name`, { posMasterNo: requestBody.posMasterNo, fullPosChild4Name })
// .getOne();
// if (chk_FullName0 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้",
// );
// }
// const chk_FullName1 = await AppDataSource.getRepository(PosMaster)
// .createQueryBuilder("posMaster")
// .from("posMaster", "posMaster")
// .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId })
// .andWhere("posMaster.orgChild1ShortName = :orgChild1ShortName", { orgChild1ShortName: SName })
// .andWhere("posMaster.orgChild2Id IS NULL")
// .andWhere(`${filterName} = :fullPosName`, { fullPosName });
// if (chk_FullName1 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้",
// );
// }
// const chk_FullName2 = await AppDataSource.getRepository(PosMaster)
// .createQueryBuilder("posMaster")
// .from("posMaster", "posMaster")
// .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId })
// .andWhere("posMaster.orgChild2ShortName = :orgChild2ShortName", { orgChild2ShortName: SName })
// .andWhere("posMaster.orgChild3Id IS NULL")
// .andWhere(`${filterName} = :fullPosName`, { fullPosName });
// if (chk_FullName2 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้",
// );
// }
// const chk_FullName3 = await AppDataSource.getRepository(PosMaster)
// .createQueryBuilder("posMaster")
// .from("posMaster", "posMaster")
// .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId })
// .andWhere("posMaster.orgChild3ShortName = :orgChild3ShortName", { orgChild3ShortName: SName })
// .andWhere("posMaster.orgChild4Id IS NULL")
// .andWhere(`${filterName} = :fullPosName`, { fullPosName });
// if (chk_FullName3 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้",
// );
// }
// const chk_FullName4 = await AppDataSource.getRepository(PosMaster)
// .createQueryBuilder("posMaster")
// .from("posMaster", "posMaster")
// .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId })
// .andWhere("posMaster.orgChild4ShortName = :orgChild4ShortName", { orgChild4ShortName: SName })
// .andWhere(`${filterName} = :fullPosName`, { fullPosName });
// if (chk_FullName4 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้",
// );
// }
posMaster.createdUserId = request.user.sub; posMaster.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name; posMaster.createdFullName = request.user.name;
posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateUserId = request.user.sub;
@ -861,150 +774,133 @@ export class PositionController extends Controller {
posMaster.posMasterNoPrefix = requestBody.posMasterNoPrefix; posMaster.posMasterNoPrefix = requestBody.posMasterNoPrefix;
posMaster.posMasterNoSuffix = requestBody.posMasterNoSuffix; posMaster.posMasterNoSuffix = requestBody.posMasterNoSuffix;
posMaster.reason = requestBody.reason == null ? "" : requestBody.reason; posMaster.reason = requestBody.reason == null ? "" : requestBody.reason;
// posMaster.orgRootId = null;
// posMaster.orgChild1Id = null;
// posMaster.orgChild2Id = null; let orgRoot: any = null;
// posMaster.orgChild3Id = null; let SName: any = null;
// posMaster.orgChild4Id = null; if (requestBody.orgRootId != null)
orgRoot = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!orgRoot) {
let orgChild1: any = null;
if (requestBody.orgChild1Id != null)
orgChild1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!orgChild1) {
let orgChild2: any = null;
if (requestBody.orgChild2Id != null)
orgChild2 = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!orgChild2) {
let orgChild3: any = null;
if (requestBody.orgChild3Id != null)
orgChild3 = await this.child3Repository.findOne({
where: { id: requestBody.orgChild3Id },
});
if (!orgChild3) {
let orgChild4: any = null;
if (requestBody.orgChild4Id != null)
orgChild4 = await this.child4Repository.findOne({
where: { id: requestBody.orgChild4Id },
});
if (!orgChild4) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง");
} else {
SName = orgChild4.orgChild4ShortName;
}
} else {
SName = orgChild3.orgChild3ShortName;
}
} else {
SName = orgChild2.orgChild2ShortName;
}
} else {
SName = orgChild1.orgChild1ShortName;
}
} else {
SName = orgRoot.orgRootShortName;
}
const chk_SName0 = await this.posMasterRepository.findOne({
where: {
orgRevisionId: posMaster.orgRevisionId,
orgRoot: { orgRootShortName: SName },
orgChild1Id: IsNull(),
posMasterNo: requestBody.posMasterNo,
},
relations: ["orgRoot"],
});
if (chk_SName0 != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
);
}
// let orgRoot: any = null; const chk_SName1 = await this.posMasterRepository.findOne({
// let SName: any = null; where: {
// if (requestBody.orgRootId != null) orgRevisionId: posMaster.orgRevisionId,
// orgRoot = await this.orgRootRepository.findOne({ orgChild1: { orgChild1ShortName: SName },
// where: { id: requestBody.orgRootId }, orgChild2Id: IsNull(),
// }); posMasterNo: requestBody.posMasterNo,
// if (!orgRoot) { },
// let orgChild1: any = null; relations: ["orgChild1"],
// if (requestBody.orgChild1Id != null) });
// orgChild1 = await this.child1Repository.findOne({ if (chk_SName1 != null) {
// where: { id: requestBody.orgChild1Id }, throw new HttpError(
// }); HttpStatusCode.INTERNAL_SERVER_ERROR,
// if (!orgChild1) { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
// let orgChild2: any = null; );
// if (requestBody.orgChild2Id != null) }
// orgChild2 = await this.child2Repository.findOne({
// where: { id: requestBody.orgChild2Id },
// });
// if (!orgChild2) {
// let orgChild3: any = null;
// if (requestBody.orgChild3Id != null)
// orgChild3 = await this.child3Repository.findOne({
// where: { id: requestBody.orgChild3Id },
// });
// if (!orgChild3) {
// let orgChild4: any = null;
// if (requestBody.orgChild4Id != null)
// orgChild4 = await this.child4Repository.findOne({
// where: { id: requestBody.orgChild4Id },
// });
// if (!orgChild4) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง");
// } else {
// posMaster.orgRootId = orgChild4.orgRootId;
// posMaster.orgChild1Id = orgChild4.orgChild1Id;
// posMaster.orgChild2Id = orgChild4.orgChild2Id;
// posMaster.orgChild3Id = orgChild4.orgChild3Id;
// posMaster.orgChild4Id = orgChild4.id;
// posMaster.orgRevisionId = orgChild4.orgRevisionId;
// SName = orgChild4.orgChild4ShortName;
// }
// } else {
// posMaster.orgRootId = orgChild3.orgRootId;
// posMaster.orgChild1Id = orgChild3.orgChild1Id;
// posMaster.orgChild2Id = orgChild3.orgChild2Id;
// posMaster.orgChild3Id = orgChild3.id;
// posMaster.orgRevisionId = orgChild3.orgRevisionId;
// SName = orgChild3.orgChild3ShortName;
// }
// } else {
// posMaster.orgRootId = orgChild2.orgRootId;
// posMaster.orgChild1Id = orgChild2.orgChild1Id;
// posMaster.orgChild2Id = orgChild2.id;
// posMaster.orgRevisionId = orgChild2.orgRevisionId;
// SName = orgChild2.orgChild2ShortName;
// }
// } else {
// posMaster.orgRootId = orgChild1.orgRootId;
// posMaster.orgChild1Id = orgChild1.id;
// posMaster.orgRevisionId = orgChild1.orgRevisionId;
// SName = orgChild1.orgChild1ShortName;
// }
// } else {
// posMaster.orgRootId = orgRoot.id;
// posMaster.orgRevisionId = orgRoot.orgRevisionId;
// SName = orgRoot.orgRootShortName;
// }
if (posMaster.orgChild4Id != null) { const chk_SName2 = await this.posMasterRepository.findOne({
const chk_SName4 = await this.posMasterRepository.findOne({ where: {
where: { orgRevisionId: posMaster.orgRevisionId,
orgChild4Id: posMaster.orgChild4Id, orgChild2: { orgChild2ShortName: SName },
posMasterNo: requestBody.posMasterNo, orgChild3Id: IsNull(),
id: Not(posMaster.id), posMasterNo: requestBody.posMasterNo,
}, },
}); relations: ["orgChild2"],
if (chk_SName4 != null) { });
throw new HttpError( if (chk_SName2 != null) {
HttpStatusCode.INTERNAL_SERVER_ERROR, throw new HttpError(
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", HttpStatusCode.INTERNAL_SERVER_ERROR,
); "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
} );
} else if (posMaster.orgChild3Id != null) { }
const chk_SName3 = await this.posMasterRepository.findOne({
where: { const chk_SName3 = await this.posMasterRepository.findOne({
orgChild3Id: posMaster.orgChild3Id, where: {
posMasterNo: requestBody.posMasterNo, orgRevisionId: posMaster.orgRevisionId,
id: Not(posMaster.id), orgChild3: { orgChild3ShortName: SName },
}, orgChild4Id: IsNull(),
}); posMasterNo: requestBody.posMasterNo,
if (chk_SName3 != null) { },
throw new HttpError( relations: ["orgChild3"],
HttpStatusCode.INTERNAL_SERVER_ERROR, });
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", if (chk_SName3 != null) {
); throw new HttpError(
} HttpStatusCode.INTERNAL_SERVER_ERROR,
} else if (posMaster.orgChild2Id != null) { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
const chk_SName2 = await this.posMasterRepository.findOne({ );
where: { }
orgChild2Id: posMaster.orgChild2Id,
posMasterNo: requestBody.posMasterNo, const chk_SName4 = await this.posMasterRepository.findOne({
id: Not(posMaster.id), where: {
}, orgRevisionId: posMaster.orgRevisionId,
}); orgChild4: { orgChild4ShortName: SName },
if (chk_SName2 != null) { posMasterNo: requestBody.posMasterNo,
throw new HttpError( },
HttpStatusCode.INTERNAL_SERVER_ERROR, relations: ["orgChild4"],
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", });
); if (chk_SName4 != null) {
} throw new HttpError(
} else if (posMaster.orgChild1Id != null) { HttpStatusCode.INTERNAL_SERVER_ERROR,
const chk_SName1 = await this.posMasterRepository.findOne({ "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
where: { );
orgChild1Id: posMaster.orgChild1Id,
posMasterNo: requestBody.posMasterNo,
id: Not(posMaster.id),
},
});
if (chk_SName1 != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
);
}
} else if (posMaster.orgRootId != null) {
const chk_SName0 = await this.posMasterRepository.findOne({
where: {
orgRootId: posMaster.orgRootId,
posMasterNo: requestBody.posMasterNo,
id: Not(posMaster.id),
},
});
if (chk_SName0 != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้",
);
}
} }
posMaster.createdUserId = request.user.sub; posMaster.createdUserId = request.user.sub;