From 4d0321e925c630d4bc97c31865808a8991f2f740 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 1 Nov 2024 16:47:01 +0700 Subject: [PATCH] fix req update info --- src/controllers/ProfileController.ts | 51 +++++++++++++++++++++------- src/entities/Profile.ts | 3 ++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 3bdbd19d..f556adb4 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -535,6 +535,7 @@ export class ProfileController extends Controller { const certs = await this.certificateRepository.find({ where: { profileId: id }, select: ["certificateType", "issuer", "certificateNo", "issueDate"], + order: { createdAt: "ASC" }, }); const Cert = certs.length > 0 @@ -555,6 +556,7 @@ export class ProfileController extends Controller { const trainings = await this.trainingRepository.find({ select: ["startDate", "endDate", "place", "department", "name"], where: { profileId: id }, + order: { createdAt: "ASC" }, }); const Training = trainings.length > 0 @@ -591,6 +593,7 @@ export class ProfileController extends Controller { const disciplines = await this.disciplineRepository.find({ select: ["refCommandDate", "refCommandNo", "detail"], where: { profileId: id }, + order: { createdAt: "ASC" }, }); const Discipline = disciplines.length > 0 @@ -612,7 +615,8 @@ export class ProfileController extends Controller { const educations = await this.educationRepository.find({ select: ["startDate", "endDate", "educationLevel", "degree", "field", "institute"], where: { profileId: id }, - order: { lastUpdatedAt: "DESC" }, + // order: { lastUpdatedAt: "DESC" }, + order: { createdAt: "ASC" }, }); const Education = educations.length > 0 @@ -765,6 +769,10 @@ export class ProfileController extends Controller { }, ]; + // const data = { + // FullName: , + // }; + return new HttpSuccess({ Profile: [Profile], Cert, @@ -775,6 +783,11 @@ export class ProfileController extends Controller { Insignia, Leave, }); + // return new HttpSuccess({ + // template: "kk1", + // reportName: "docx-report", + // data: data, + // }); } /** * @@ -2747,18 +2760,22 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const lastestData = await this.profileFamilyFatherRepository.findOne({ + let lastestData:any = 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]; }); + + if (!lastestData) { + lastestData = new ProfileFamilyFather(); + Object.assign(lastestData, body); + lastestData.profileId = id; + } + Object.assign(lastestData, body); lastestData.createdUserId = req.user.sub; lastestData.createdFullName = req.user.name; @@ -2795,18 +2812,22 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const lastestData = await this.profileFamilyMotherRepository.findOne({ + let 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]; }); + + if (!lastestData) { + lastestData = new ProfileFamilyMother(); + Object.assign(lastestData, body); + lastestData.profileId = id; + } + Object.assign(lastestData, body); lastestData.createdUserId = req.user.sub; lastestData.createdFullName = req.user.name; @@ -2843,18 +2864,22 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const lastestData = await this.profileFamilyCoupleRepository.findOne({ + let 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]; }); + + if (!lastestData) { + lastestData = new ProfileFamilyCouple(); + Object.assign(lastestData, body); + lastestData.profileId = id; + } + Object.assign(lastestData, body); lastestData.createdUserId = req.user.sub; lastestData.createdFullName = req.user.name; diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 08f282a8..5426a665 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -773,6 +773,7 @@ export type UpdateProfileFather = { fatherLastName?: string | null; fatherCareer?: string | null; fatherLive?: boolean | null; + fatherCitizenId?: string | null; }; export type UpdateProfileMother = { @@ -781,6 +782,7 @@ export type UpdateProfileMother = { motherLastName?: string | null; motherCareer?: string | null; motherLive?: boolean | null; + motherCitizenId?: string | null; }; export type UpdateProfileCouple = { @@ -791,6 +793,7 @@ export type UpdateProfileCouple = { coupleCareer?: string | null; coupleLive?: boolean | null; relationship?: string | null; + coupleCitizenId?: string | null; }; export type UpdateProfileAddress = {