validate ชื่อซ้ำ ใน DB และ CRUD Relationship

This commit is contained in:
AdisakKanthawilang 2024-02-02 16:03:22 +07:00
parent a7c6f80a7c
commit 2fa49ddfcc
2 changed files with 184 additions and 140 deletions

View file

@ -44,11 +44,17 @@ export class PosExecutiveController extends Controller {
requestBody: CreatePosExecutive,
@Request() request: { user: Record<string, any> },
) {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
const checkName = await this.posExecutiveRepository.findOne({
where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
try {
const posExecutive = Object.assign(new PosExecutive(), requestBody);
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
@ -78,6 +84,15 @@ export class PosExecutiveController extends Controller {
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
const checkName = await this.posExecutiveRepository.findOne({
where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
try {
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;