From 8a1400d84057cfd80a56ea1834b9263288e06c46 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 21 Mar 2024 10:15:00 +0700 Subject: [PATCH] refactor!: merge information into profile endpoint --- src/controllers/ProfileController.ts | 295 ++++++------------ .../ProfileInformationController.ts | 204 ------------ 2 files changed, 102 insertions(+), 397 deletions(-) delete mode 100644 src/controllers/ProfileInformationController.ts diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 76b226c2..2c8f4463 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -16,31 +16,32 @@ import { } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; -import HttpStatusCode from "../interfaces/http-status"; +import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; -import { Profile, CreateProfile, UpdateProfile } from "../entities/Profile"; -import { Brackets, In, IsNull, Like, Not } from "typeorm"; +import { Profile, CreateProfile, UpdateProfile, ProfileHistory } from "../entities/Profile"; +import { Brackets, 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"; -import { request } from "http"; import { calculateRetireDate } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @Response( - HttpStatusCode.INTERNAL_SERVER_ERROR, + HttpStatus.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) -@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +@SuccessResponse(HttpStatus.OK, "สำเร็จ") 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); + private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); + private posMasterRepo = AppDataSource.getRepository(PosMaster); + private profileRepo = AppDataSource.getRepository(Profile); + private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory); + private posLevelRepo = AppDataSource.getRepository(PosLevel); + private posTypeRepo = AppDataSource.getRepository(PosType); /** * API สร้างทะเบียนประวัติ @@ -49,58 +50,32 @@ export class ProfileController extends Controller { * */ @Post() - async createProfile( - @Body() - requestBody: CreateProfile, - @Request() request: { user: Record }, - ) { - const _profile = await this.profileRepository.findOne({ - where: { citizenId: requestBody.citizenId }, - }); - if (_profile) { + async createProfile(@Request() request: RequestWithUser, @Body() body: CreateProfile) { + if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) { throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, + HttpStatus.INTERNAL_SERVER_ERROR, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", ); } - if (requestBody.posLevelId == "") { - requestBody.posLevelId = null; - } - if (requestBody.posLevelId) { - const checkPosLevel = await this.posLevelRepository.findOne({ - where: { id: requestBody.posLevelId }, - }); - if (!checkPosLevel) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); - } + + if (!body.posLevelId) body.posLevelId = null; + if (!body.posTypeId) body.posTypeId = null; + + if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); } - if (requestBody.posTypeId == "") { - requestBody.posTypeId = null; - } - if (requestBody.posTypeId) { - const checkPosType = await this.posTypeRepository.findOne({ - where: { id: requestBody.posTypeId }, - }); - if (!checkPosType) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); - } + if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - const checkCitizenId = await this.profileRepository.findOne({ - where: { citizenId: requestBody.citizenId }, - }); - - if (checkCitizenId) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); - } - - const profile = Object.assign(new Profile(), requestBody); + const profile = Object.assign(new Profile(), body); profile.createdUserId = request.user.sub; profile.createdFullName = request.user.name; profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name; - await this.profileRepository.save(profile); + await this.profileRepo.save(profile); + return new HttpSuccess(); } @@ -113,65 +88,48 @@ export class ProfileController extends Controller { */ @Put("{id}") async updateProfile( + @Request() request: RequestWithUser, @Path() id: string, - @Body() - requestBody: CreateProfile, - @Request() request: { user: Record }, + @Body() body: UpdateProfile, ) { - const profile = await this.profileRepository.findOne({ where: { id: id } }); - if (!profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); + const exists = + !!body.citizenId && + (await this.profileRepo.findOne({ + where: { id: Not(id), citizenId: body.citizenId }, + })); + + if (exists) { + throw new HttpError(HttpStatus.CONFLICT, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); } - const _profile = await this.profileRepository.findOne({ - where: { id: Not(id), citizenId: requestBody.citizenId }, - }); - if (_profile) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", - ); + if (!body.posLevelId) body.posLevelId = null; + if (!body.posTypeId) body.posTypeId = null; + + if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); } - if (requestBody.posLevelId == "") { - requestBody.posLevelId = null; - } - if (requestBody.posLevelId) { - const checkPosLevel = await this.posLevelRepository.findOne({ - where: { id: requestBody.posLevelId }, - }); - if (!checkPosLevel) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); - } + if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - if (requestBody.posTypeId == "") { - requestBody.posTypeId = null; - } - if (requestBody.posTypeId) { - const checkPosType = await this.posTypeRepository.findOne({ - where: { id: requestBody.posTypeId }, - }); - if (!checkPosType) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); - } - } + const record = await this.profileRepo.findOneBy({ id }); - const checkCitizenId = await this.profileRepository.findOne({ - where: { - id: Not(id), - citizenId: requestBody.citizenId, - }, - }); + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); - if (checkCitizenId) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว"); - } + await this.profileHistoryRepo.save( + Object.assign(new ProfileHistory(), { + ...record, + id: undefined, + }), + ); + + Object.assign(record, body); + record.lastUpdateUserId = request.user.sub; + record.lastUpdateFullName = request.user.name; + + await this.profileRepo.save(record); - profile.lastUpdateUserId = request.user.sub; - profile.lastUpdateFullName = request.user.name; - this.profileRepository.merge(profile, requestBody); - await this.profileRepository.save(profile); return new HttpSuccess(); } @@ -184,13 +142,12 @@ export class ProfileController extends Controller { */ @Delete("{id}") async deleteProfile(@Path() id: string) { - const delProfile = await this.profileRepository.findOne({ - where: { id }, - }); - if (!delProfile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); + const result = await this.profileRepo.delete({ id }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } - await this.profileRepository.delete({ id: id }); + return new HttpSuccess(); } @@ -202,27 +159,20 @@ export class ProfileController extends Controller { * @param {string} id Id ทะเบียนประวัติ */ @Get("{id}") - async detailProfile(@Path() id: string) { - const profile = await this.profileRepository.findOne({ + async getProfile(@Path() id: string) { + const profile = await this.profileRepo.findOne({ relations: { posLevel: true, posType: true, + gender: true, + relationship: true, + bloodGroup: true, }, where: { id }, - select: [ - "id", - "prefix", - "firstName", - "lastName", - "citizenId", - "position", - "posLevelId", - "posTypeId", - ], }); - if (!profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } + + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + return new HttpSuccess(profile); } @@ -238,73 +188,32 @@ export class ProfileController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - const [profile, total] = await this.profileRepository.findAndCount({ - select: [ - "id", - "prefix", - "firstName", - "lastName", - "citizenId", - "position", - "posLevelId", - "posTypeId", - ], + const [record, total] = await this.profileRepo.findAndCount({ + relations: { + posLevel: true, + posType: true, + gender: true, + relationship: true, + bloodGroup: true, + }, where: { citizenId: Like(`%${keyword}%`), position: Like(`%${keyword}%`), prefix: Like(`%${keyword}%`), firstName: Like(`%${keyword}%`), lastName: Like(`%${keyword}%`), - // posLevel: { posLevelName: keyword }, - // posType: { posTypeName: Like(`%${keyword}%`) }, - // salaryEmployeeMins: { group: keyword }, - // salaryEmployee: { group: keyword }, }, order: { createdAt: "ASC" }, skip: (page - 1) * pageSize, take: pageSize, }); - // if (keyword != undefined && keyword !== "") { - // const formattedKeyword = keyword.toLowerCase().replace(/\s+/g, ""); - // const filteredProfile = profile.filter( - // (x) => - // (x.prefix + x.firstName + x.lastName).replace(/\s+/g, "").includes(formattedKeyword) || - // x.citizenId?.toString().includes(keyword) || - // x.position?.toString().includes(keyword), - // ); - - // const formattedData = filteredProfile.map((item) => ({ - // id: item.id, - // prefix: item.prefix, - // firstName: item.firstName, - // lastName: item.lastName, - // citizenId: item.citizenId, - // position: item.position, - // posLevelId: item.posLevelId, - // posTypeId: item.posTypeId, - // })); - - // return new HttpSuccess({ data: formattedData, total: formattedData.length }); - // } - - const formattedData = profile.map((item) => ({ - id: item.id, - prefix: item.prefix, - firstName: item.firstName, - lastName: item.lastName, - citizenId: item.citizenId, - position: item.position, - posLevelId: item.posLevelId, - posTypeId: item.posTypeId, - })); - return new HttpSuccess({ data: formattedData, total }); + return new HttpSuccess({ data: record, total }); } /** * API ค้นหารายชื่อไปครองตำแหน่ง * * @summary ORG_063 - ค้นหารายชื่อไปครองตำแหน่ง (ADMIN) #68 - * */ @Post("search") async searchProfileOrg( @@ -318,7 +227,7 @@ export class ProfileController extends Controller { keyword?: string; }, ) { - const orgRevision = await this.orgRevisionRepository.findOne({ + const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false, @@ -326,9 +235,9 @@ export class ProfileController extends Controller { relations: ["posMasters"], }); if (!orgRevision) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } - const [profiles, total] = await this.profileRepository + const [profiles, total] = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.next_holders", "next_holders") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -428,21 +337,21 @@ export class ProfileController extends Controller { */ @Get("keycloak/position") async getProfileByKeycloak(@Request() request: { user: Record }) { - const profile = await this.profileRepository.findOne({ + const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], }); if (!profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); } - const orgRevisionPublish = await this.orgRevisionRepository + const orgRevisionPublish = await this.orgRevisionRepo .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); if (!orgRevisionPublish) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); } const _profile = { @@ -549,28 +458,28 @@ export class ProfileController extends Controller { let findProfile: any; switch (body.fieldName) { case "idcard": - findProfile = await this.profileRepository.find({ + findProfile = await this.profileRepo.find({ where: { citizenId: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; case "firstname": - findProfile = await this.profileRepository.find({ + findProfile = await this.profileRepo.find({ where: { firstName: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; case "lastname": - findProfile = await this.profileRepository.find({ + findProfile = await this.profileRepo.find({ where: { lastName: Like(`%${body.keyword}%`) }, relations: ["posType", "posLevel"], }); break; default: - findProfile = await this.profileRepository.find({ + findProfile = await this.profileRepo.find({ relations: ["posType", "posLevel"], }); break; @@ -609,17 +518,17 @@ export class ProfileController extends Controller { let commanderFullname_: any = {}; let commanderPosition_: any = {}; - const findProfile = await this.profileRepository.findOne({ + const findProfile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, }); - const findRevision = await this.orgRevisionRepository.findOne({ + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, }, }); - const findPosMaster = await this.posMasterRepository.findOne({ + const findPosMaster = await this.posMasterRepo.findOne({ where: { current_holderId: findProfile?.id, orgRevisionId: findRevision?.id, @@ -648,7 +557,7 @@ export class ProfileController extends Controller { condition = { orgRootId: childId, orgChild1Id: IsNull() }; } - const findCmd = await this.posMasterRepository.findOne({ + const findCmd = await this.posMasterRepo.findOne({ where: { current_holderId: Not(IsNull()) || Not(""), orgRevisionId: findRevision?.id, @@ -791,12 +700,12 @@ export class ProfileController extends Controller { @Body() requestBody: { citizenId: string }, ) { - const profile = await this.profileRepository.findOne({ + const profile = await this.profileRepo.findOne({ where: { id: Not(id), citizenId: requestBody.citizenId }, }); if (profile) { throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, + HttpStatus.INTERNAL_SERVER_ERROR, "เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว", ); } @@ -838,7 +747,7 @@ export class ProfileController extends Controller { .take(body.pageSize) .getManyAndCount(); - const orgRevisionActive = await this.orgRevisionRepository.findOne({ + const orgRevisionActive = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); @@ -928,11 +837,11 @@ export class ProfileController extends Controller { period: string; }, ) { - const findRevision = await this.orgRevisionRepository.findOne({ + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true }, }); if (!findRevision) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } const [findPosMaster, total] = await AppDataSource.getRepository(PosMaster) @@ -1024,7 +933,7 @@ export class ProfileController extends Controller { .take(body.pageSize) .getManyAndCount(); if (!findPosMaster) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. PosMaster"); + throw new HttpError(HttpStatus.NOT_FOUND, "not found. PosMaster"); } const formattedData = findPosMaster.map((item) => { @@ -1142,12 +1051,12 @@ export class ProfileController extends Controller { @Path() revisionId: string, @Request() request: { user: Record }, ) { - const profile = await this.profileRepository.findOne({ + const profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub }, relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], }); if (!profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); } const _profile = { diff --git a/src/controllers/ProfileInformationController.ts b/src/controllers/ProfileInformationController.ts deleted file mode 100644 index 8f106478..00000000 --- a/src/controllers/ProfileInformationController.ts +++ /dev/null @@ -1,204 +0,0 @@ -import { - Controller, - Post, - Put, - Delete, - Route, - Security, - Tags, - Body, - Path, - Request, - SuccessResponse, - Response, - Get, - Query, - Patch, - Example, -} from "tsoa"; - -import HttpSuccess from "../interfaces/http-success"; -import HttpError from "../interfaces/http-error"; -import HttpStatus from "../interfaces/http-status"; - -import { - ProfileInformationHistory, - ProfileInformation, - CreateProfileInformation, - UpdateProfileInformation, -} from "../entities/ProfileInformation"; -import { RequestWithUser } from "../middlewares/user"; -import { Profile } from "../entities/Profile"; - -import { AppDataSource } from "../database/data-source"; - -@Route("api/v1/org/profile/information") -@Tags("ProfileInformation") -@Security("bearerAuth") -export class ProfileInformationController extends Controller { - private profileRepo = AppDataSource.getRepository(Profile); - private profileInformationRepo = AppDataSource.getRepository(ProfileInformation); - private profileInformationHistoryRepo = AppDataSource.getRepository(ProfileInformationHistory); - - @Get("{profileId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "42deba79-0725-403f-898d-6c142b2842a5", - createdAt: "2024-03-19T19:47:26.512Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T20:01:15.000Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "สาวิตรี ศรีสมัย", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - citizenId: "1849900687228", - prefix: "นาย", - firstName: "ธนพนธ์", - lastName: "แสงจันทร์", - birthDate: "2024-03-20T02:59:27.000Z", - ethnicity: "ไทย", - religion: "-", - telephoneNumber: "0639195701", - genderId: "74ec022c-b961-47f4-985e-2d9cbb10984c", - relationshipId: "5872f993-6dc3-44c7-85d3-f56825abd96d", - bloodGroupId: "fab11ded-8177-4e23-a791-78a10bc92333", - profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - }, - ], - }) - public async detailProfileInformation(@Path() profileId: string) { - const getProfileInformation = await this.profileInformationRepo.findOne({ - relations: { - bloodGroup: true, - relationship: true, - gender: true, - }, - where: { profileId }, - }); - if (!getProfileInformation) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } - return new HttpSuccess(getProfileInformation); - } - - @Get("history/{informationId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [ - { - id: "9abba9df-5fa0-4056-847d-4b3680b61ee5", - createdAt: "2024-03-19T20:07:24.320Z", - createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T20:07:24.320Z", - lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - createdFullName: "string", - lastUpdateFullName: "สาวิตรี ศรีสมัย", - citizenId: "1849900687228", - prefix: "นาย", - firstName: "ธนพนธ์", - lastName: "แสงจันทร์", - birthDate: "2024-03-20T03:05:41.000Z", - ethnicity: "ไทย", - religion: "-", - telephoneNumber: "0639195701", - genderId: "74ec022c-b961-47f4-985e-2d9cbb10984c", - relationshipId: "5872f993-6dc3-44c7-85d3-f56825abd96d", - bloodGroupId: "fab11ded-8177-4e23-a791-78a10bc92333", - profileId: null, - profileInformationId: "42deba79-0725-403f-898d-6c142b2842a5", - }, - ], - }) - public async getProfileInformationHistory(@Path() informationId: string) { - const record = await this.profileInformationHistoryRepo.find({ - relations: { - bloodGroup: true, - relationship: true, - gender: true, - }, - where: { - profileInformationId: informationId, - }, - }); - if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } - return new HttpSuccess(record); - } - - @Post() - public async newProfileInformation( - @Request() req: RequestWithUser, - @Body() body: CreateProfileInformation, - ) { - if (!body.profileId) { - throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); - } - - const profile = await this.profileRepo.findOneBy({ id: body.profileId }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } - - const data = new ProfileInformation(); - const meta = { - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }; - - Object.assign(data, { ...body, ...meta }); - - await this.profileInformationRepo.save(data); - - return new HttpSuccess(); - } - - @Patch("{informationId}") - public async editProfileInformation( - @Body() requestBody: UpdateProfileInformation, - @Request() req: RequestWithUser, - @Path() informationId: string, - ) { - const record = await this.profileInformationRepo.findOneBy({ id: informationId }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - - const history = new ProfileInformationHistory(); - - console.log(requestBody); - - Object.assign(history, { ...record, id: undefined }); - Object.assign(record, requestBody); - - history.profileInformationId = informationId; - history.lastUpdateFullName = req.user.name; - history.createdUserId = req.user.sub; - history.lastUpdateUserId = req.user.sub; - record.lastUpdateFullName = req.user.name; - - await Promise.all([ - this.profileInformationRepo.save(record), - this.profileInformationHistoryRepo.save(history), - ]); - - return new HttpSuccess(); - } - - @Delete("{informationId}") - public async deleteProfileInformation(@Path() informationId: string) { - await this.profileInformationHistoryRepo.delete({ - profileInformationId: informationId, - }); - - const result = await this.profileInformationRepo.delete({ id: informationId }); - - if (result.affected && result.affected <= 0) - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - - return new HttpSuccess(); - } -}