From 72abe0cae5d82c858ebe05540bdb04a26a1a2e86 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 8 Feb 2024 15:40:47 +0700 Subject: [PATCH] checkpoint --- src/controllers/ProfileController.ts | 73 +++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index bb93a4af..f7f02ac3 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile"; import { Not } from "typeorm"; +import { PosLevel } from "../entities/PosLevel"; +import { PosType } from "../entities/PosType"; @Route("api/v1/org/profile") @Tags("Profile") @@ -30,6 +32,8 @@ import { Not } from "typeorm"; @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ProfileController extends Controller { private profileRepository = AppDataSource.getRepository(Profile); + private posLevelRepository = AppDataSource.getRepository(PosLevel); + private posTypeRepository = AppDataSource.getRepository(PosType); /** * API สร้างทะเบียนประวัติ @@ -43,6 +47,29 @@ 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); @@ -76,6 +103,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; @@ -121,7 +172,16 @@ export class ProfileController extends Controller { async detailProfile(@Path() id: string) { const profile = await this.profileRepository.findOne({ where: { id }, - select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"], + select: [ + "id", + "prefix", + "firstName", + "lastName", + "citizenId", + "position", + "posLevelId", + "posTypeId", + ], }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); @@ -142,7 +202,16 @@ export class ProfileController extends Controller { @Get() async listProfile() { const profile = await this.profileRepository.find({ - select: ["id","prefix","firstName","lastName","citizenId","position","posLevelId","posTypeId"], + select: [ + "id", + "prefix", + "firstName", + "lastName", + "citizenId", + "position", + "posLevelId", + "posTypeId", + ], order: { createdAt: "ASC" }, });