From c3044ac0b4904763155b409163d813a83fc64a0d Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 31 Oct 2024 15:18:46 +0700 Subject: [PATCH 1/2] =?UTF-8?q?fix=20#708=20=E0=B8=A3=E0=B8=B2=E0=B8=A2?= =?UTF-8?q?=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=97=E0=B8=B5=E0=B9=88?= =?UTF-8?q?=E0=B8=96=E0=B8=B9=E0=B8=81=E0=B8=A5=E0=B8=9A=E0=B9=84=E0=B8=A1?= =?UTF-8?q?=E0=B9=88=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87=E0=B9=83=E0=B8=99?= =?UTF-8?q?=E0=B8=A3=E0=B8=B0=E0=B8=9A=E0=B8=9A=E0=B9=82=E0=B8=84=E0=B8=A3?= =?UTF-8?q?=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index a1d8b781..d63f83c0 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3121,7 +3121,7 @@ export class CommandController extends Controller { }[]; }, ) { - const posMasters = await this.posMasterRepository.find({ where: { id: In(body.refIds) } }); + const posMasters = await this.posMasterRepository.find({ where: { id: In(body.refIds.map(x => x.refId)) } }); const data = posMasters.map((_data) => ({ ..._data, statusReport: "PENDING", From 2725328dd0cde1d22a3bb1ce47d03915d5de37c1 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 31 Oct 2024 17:47:01 +0700 Subject: [PATCH 2/2] =?UTF-8?q?API=20=E0=B8=9A=E0=B8=B1=E0=B8=99=E0=B8=97?= =?UTF-8?q?=E0=B8=B6=E0=B8=81=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9?= =?UTF-8?q?=E0=B8=A5=E0=B8=97=E0=B8=B0=E0=B9=80=E0=B8=9A=E0=B8=B5=E0=B8=A2?= =?UTF-8?q?=E0=B8=99=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1=E0=B8=95?= =?UTF-8?q?=E0=B8=B4=20=E0=B8=88=E0=B8=B2=E0=B8=81=E0=B8=81=E0=B8=A3?= =?UTF-8?q?=E0=B8=A1=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=9B=E0=B8=81=E0=B8=84?= =?UTF-8?q?=E0=B8=A3=E0=B8=AD=E0=B8=87=20#91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileController.ts | 207 +++++++++++++++++++++++++++ src/entities/Profile.ts | 45 ++++++ 2 files changed, 252 insertions(+) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 361d51d1..26ba8992 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -14,6 +14,7 @@ import { Get, Query, Example, + Patch, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -25,6 +26,10 @@ import { UpdateProfile, ProfileHistory, CreateProfileAllFields, + UpdateProfileReqEdit, + UpdateProfileFather, + UpdateProfileMother, + UpdateProfileCouple, } from "../entities/Profile"; import { Any, Brackets, In, IsNull, Like, Not } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; @@ -65,6 +70,9 @@ import { PosMasterAct } from "../entities/PosMasterAct"; import axios from "axios"; import { OrgChild1 } from "../entities/OrgChild1"; import { viewCommanderDirector } from "../entities/view/viewCommanderDirector"; +import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; +import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; +import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -87,8 +95,11 @@ export class ProfileController extends Controller { private subDistrict = AppDataSource.getRepository(SubDistrict); private certificateRepository = AppDataSource.getRepository(ProfileCertificate); private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple); + private profileFamilyCoupleHistoryRepo = AppDataSource.getRepository(ProfileFamilyCoupleHistory); private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother); + private profileFamilyMotherHistoryRepo = AppDataSource.getRepository(ProfileFamilyMotherHistory); private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather); + private profileFamilyFatherHistoryRepo = AppDataSource.getRepository(ProfileFamilyFatherHistory); private trainingRepository = AppDataSource.getRepository(ProfileTraining); private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); private educationRepository = AppDataSource.getRepository(ProfileEducation); @@ -2666,6 +2677,200 @@ export class ProfileController extends Controller { return new HttpSuccess({ retireDate, age }); } + + /** + * API แก้ไขประวัติในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @summary แก้ไขประวัติในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @param {string} id Id ทะเบียนประวัติ + */ + @Patch("request-edit/myprofile/{id}") + async updateProfileReEdit( + @Request() request: RequestWithUser, + @Path() id: string, + @Body() body: UpdateProfileReqEdit, + ) { + const record = await this.profileRepo.findOneBy({ id }); + const before = structuredClone(record); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); + + Object.keys(body).forEach((key) => { + const typedKey = key as keyof UpdateProfileReqEdit; // บอก TypeScript ว่า key เป็นฟิลด์ของ UpdateProfileReqEdit + if (body[typedKey] === null || body[typedKey] === "") delete body[typedKey]; + }); + + Object.assign(record, body); + record.createdUserId = request.user.sub; + record.createdFullName = request.user.name; + record.createdAt = new Date(); + record.lastUpdateUserId = request.user.sub; + record.lastUpdateFullName = request.user.name; + record.lastUpdatedAt = new Date(); + await this.profileHistoryRepo.save( + Object.assign(new ProfileHistory(), { + ...record, + profileId: id, + id: undefined, + }), + ); + await this.profileRepo.save(record); + // setLogDataDiff(request, { before, after: record }); + + if (record != null && record.keycloak != null) { + const result = await updateName(record.keycloak, record.firstName, record.lastName); + if (!result) { + throw new Error(result.errorMessage); + } + } + + return new HttpSuccess(); + } + + /** + * API แก้ไขประวัติบิดาในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @summary แก้ไขประวัติบิดาในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @param {string} id Id ทะเบียนประวัติ + */ + @Patch("request-edit/father/{id}") + public async UpdateFamilyFather( + @Request() req: RequestWithUser, + @Path() id: string, + @Body() body: UpdateProfileFather, + ) { + + const profile = await this.profileRepo.findOneBy({ id:id }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const lastestData = await this.profileFamilyFatherRepository.findOne({ + where:{ profileId:id }, + order:{ createdAt: "DESC" } + }); + if (!lastestData) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลดังกล่าว"); + } + + Object.keys(body).forEach((key) => { + const typedKey = key as keyof UpdateProfileFather; // บอก TypeScript ว่า key เป็นฟิลด์ของ UpdateProfileFather + if (body[typedKey] === null || body[typedKey] === "") delete body[typedKey]; + }); + Object.assign(lastestData, body); + lastestData.createdUserId = req.user.sub; + lastestData.createdFullName = req.user.name; + lastestData.lastUpdateUserId = req.user.sub; + lastestData.lastUpdateFullName = req.user.name; + lastestData.createdAt = new Date(); + lastestData.lastUpdatedAt = new Date(); + + const history = new ProfileFamilyFatherHistory(); + Object.assign(history, { ...lastestData, id: undefined }); + + await this.profileFamilyFatherRepository.save(lastestData, { data: req }); + history.profileFamilyFatherId = lastestData.id; + await this.profileFamilyFatherHistoryRepo.save(history, { data: req }); + return new HttpSuccess(lastestData.id); + } + + /** + * API แก้ไขประวัติมารดาในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @summary แก้ไขประวัติมารดาในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @param {string} id Id ทะเบียนประวัติ + */ + @Patch("request-edit/mother/{id}") + public async UpdateFamilyMother( + @Request() req: RequestWithUser, + @Path() id: string, + @Body() body: UpdateProfileMother, + ) { + + const profile = await this.profileRepo.findOneBy({ id:id }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const lastestData = await this.profileFamilyMotherRepository.findOne({ + where:{ profileId:id }, + order:{ createdAt: "DESC" } + }); + if (!lastestData) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลดังกล่าว"); + } + + Object.keys(body).forEach((key) => { + const typedKey = key as keyof UpdateProfileMother; // บอก TypeScript ว่า key เป็นฟิลด์ของ UpdateProfileMother + if (body[typedKey] === null || body[typedKey] === "") delete body[typedKey]; + }); + Object.assign(lastestData, body); + lastestData.createdUserId = req.user.sub; + lastestData.createdFullName = req.user.name; + lastestData.lastUpdateUserId = req.user.sub; + lastestData.lastUpdateFullName = req.user.name; + lastestData.createdAt = new Date(); + lastestData.lastUpdatedAt = new Date(); + + const history = new ProfileFamilyMotherHistory(); + Object.assign(history, { ...lastestData, id: undefined }); + + await this.profileFamilyMotherRepository.save(lastestData, { data: req }); + history.profileFamilyMotherId = lastestData.id; + await this.profileFamilyMotherHistoryRepo.save(history, { data: req }); + return new HttpSuccess(lastestData.id); + } + + /** + * API แก้ไขประวัติคู่สมรสในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @summary แก้ไขประวัติคู่สมรสในเมนูร้องขอแก้ไขทะเบียนประวัติ + * + * @param {string} id Id ทะเบียนประวัติ + */ + @Patch("request-edit/couple/{id}") + public async UpdateFamilyCouple( + @Request() req: RequestWithUser, + @Path() id: string, + @Body() body: UpdateProfileCouple, + ) { + + const profile = await this.profileRepo.findOneBy({ id:id }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const lastestData = await this.profileFamilyCoupleRepository.findOne({ + where:{ profileId:id }, + order:{ createdAt: "DESC" } + }); + if (!lastestData) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลดังกล่าว"); + } + + Object.keys(body).forEach((key) => { + const typedKey = key as keyof UpdateProfileCouple; // บอก TypeScript ว่า key เป็นฟิลด์ของ UpdateProfileCouple + if (body[typedKey] === null || body[typedKey] === "") delete body[typedKey]; + }); + Object.assign(lastestData, body); + lastestData.createdUserId = req.user.sub; + lastestData.createdFullName = req.user.name; + lastestData.lastUpdateUserId = req.user.sub; + lastestData.lastUpdateFullName = req.user.name; + lastestData.createdAt = new Date(); + lastestData.lastUpdatedAt = new Date(); + + const history = new ProfileFamilyCoupleHistory(); + Object.assign(history, { ...lastestData, id: undefined }); + + await this.profileFamilyCoupleRepository.save(lastestData, { data: req }); + history.profileFamilyCoupleId = lastestData.id; + await this.profileFamilyCoupleHistoryRepo.save(history, { data: req }); + return new HttpSuccess(lastestData.id); + } /** * API แก้ไขทะเบียนประวัติ @@ -2774,6 +2979,8 @@ export class ProfileController extends Controller { return new HttpSuccess(); } + + /** * API ลบทะเบียนประวัติ * diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index b5c822f2..08f282a8 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -747,6 +747,51 @@ export type UpdateProfile = { relationship?: string | null; bloodGroup?: string | null; }; +export type UpdateProfileReqEdit = { + rank?: string | null; + prefix?: string | null; + firstName?: string | null; + lastName?: string | null; + birthDate?: Date | null; + ethnicity?: string | null; + telephoneNumber?: string | null; + nationality?: string | null; + religion?: string | null; + gender?: string | null; + relationship?: string | null; + bloodGroup?: string | null; + registrationAddress?: string | null; + registrationProvinceId?: string | null; + registrationDistrictId?: string | null; + registrationSubDistrictId?: string | null; + registrationZipCode?: string | null; +}; + +export type UpdateProfileFather = { + fatherPrefix?: string | null; + fatherFirstName?: string | null; + fatherLastName?: string | null; + fatherCareer?: string | null; + fatherLive?: boolean | null; +}; + +export type UpdateProfileMother = { + motherPrefix?: string | null; + motherFirstName?: string | null; + motherLastName?: string | null; + motherCareer?: string | null; + motherLive?: boolean | null; +}; + +export type UpdateProfileCouple = { + couplePrefix?: string | null; + coupleFirstName?: string | null; + coupleLastName?: string | null; + coupleLastNameOld?: string | null; + coupleCareer?: string | null; + coupleLive?: boolean | null; + relationship?: string | null; +}; export type UpdateProfileAddress = { registrationAddress?: string | null;