diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 66c2a8a2..d296a653 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -21,6 +21,8 @@ import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile"; import { Brackets, In, IsNull, Like, Not } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { PosMaster } from "../entities/PosMaster"; +import { PosLevel } from "../entities/PosLevel"; +import { PosType } from "../entities/PosType"; @Route("api/v1/org/profile") @Tags("Profile") @@ -34,6 +36,8 @@ export class ProfileController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private posMasterRepository = AppDataSource.getRepository(PosMaster); private profileRepository = AppDataSource.getRepository(Profile); + private posLevelRepository = AppDataSource.getRepository(PosLevel); + private posTypeRepository = AppDataSource.getRepository(PosType); /** * API สร้างทะเบียนประวัติ @@ -47,6 +51,30 @@ export class ProfileController extends Controller { requestBody: CreateProfile, @Request() request: { user: Record }, ) { + if (requestBody.posLevelId) { + const checkPosLevel = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkPosLevel) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId, + ); + } + } + + if (requestBody.posTypeId) { + const checkPosType = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkPosType) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId, + ); + } + } + try { const profile = Object.assign(new Profile(), requestBody); profile.createdUserId = request.user.sub; @@ -79,6 +107,30 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } + if (requestBody.posLevelId) { + const checkPosLevel = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkPosLevel) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId, + ); + } + } + + if (requestBody.posTypeId) { + const checkPosType = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkPosType) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId, + ); + } + } + try { profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name;