From 88b567070ad132698973f59006140b88d05eb48f Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:45:41 +0700 Subject: [PATCH 1/2] feat: complete family history endpoint --- .../ProfileFamilyHistoryController.ts | 41 ++++++++++++++++--- src/entities/ProfileFamily.ts | 4 +- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts index b61ddc86..0ee69de9 100644 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ b/src/controllers/ProfileFamilyHistoryController.ts @@ -14,6 +14,7 @@ import { } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { + CreateChildren, CreateProfileFamily, ProfileChildren, ProfileChildrenHistory, @@ -118,11 +119,12 @@ export class ProfileFamilyHistoryController extends Controller { }) public async familyHistory(@Path() profileId: string) { const family = await this.familyHistoryRepo.find({ - skip: 1, order: { lastUpdatedAt: "DESC" }, where: { profileId }, }); + family.pop(); + const record = await Promise.all( family.map(async (v) => ({ ...v, @@ -196,20 +198,49 @@ export class ProfileFamilyHistoryController extends Controller { if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const childrenRecord = await this.familyHistoryRepo.findBy({ + const childrenRecord = await this.childrenRepo.findBy({ profileId: familyRecord.profileId, }); const historyData = new ProfileFamilyHistory(); - Object.assign(historyData, { ...familyRecord, ...body, id: undefined }); + const { children, ...family } = body; + + Object.assign(historyData, { ...familyRecord, ...family, id: undefined }); historyData.lastUpdateFullName = req.user.name; historyData.lastUpdateFullName = req.user.name; + const newChild: CreateChildren[] = []; + + for (let child of children) { + let match = childrenRecord.find((v) => v.id === child.id); + + if (match) Object.assign(match, child); + else { + let newProfileChild = new ProfileChildren(); + Object.assign(newProfileChild, { + ...child, + profileId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + id: undefined, + }); + newChild.push(newProfileChild); + } + } + await Promise.all([ - this.familyHistoryRepo.save(familyRecord), + this.familyHistoryRepo.save(historyData), + ...newChild.map(async (v) => { + return await this.childrenRepo.save(v); + }), ...childrenRecord.map(async (v) => { - await this.childrenHistoryRepo.save( + return await this.childrenRepo.save(v); + }), + ...childrenRecord.map(async (v) => { + return await this.childrenHistoryRepo.save( this.childrenHistoryRepo.create({ ...v, profileFamilyHistoryId: familyRecord.id, diff --git a/src/entities/ProfileFamily.ts b/src/entities/ProfileFamily.ts index f7377b70..c939534a 100644 --- a/src/entities/ProfileFamily.ts +++ b/src/entities/ProfileFamily.ts @@ -261,7 +261,7 @@ export class ProfileChildrenHistory extends ProfileChildren { profileChildren: ProfileChildren; } -type CreateChildren = { +export type CreateChildren = { isActive: boolean; childrenCareer: string; childrenFirstName: string; @@ -271,7 +271,7 @@ type CreateChildren = { childrenCitizenId: string; }; -type UpdateChildren = { +export type UpdateChildren = { id: string; isActive?: boolean | null; childrenCareer?: string | null; From 3858ea1415a3e5f48674b2dc2232b6761b2a7ce9 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:47:12 +0700 Subject: [PATCH 2/2] chore: update response example --- .../ProfileFamilyHistoryController.ts | 172 ++++++++++++++---- 1 file changed, 136 insertions(+), 36 deletions(-) diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts index 0ee69de9..d671a7f8 100644 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ b/src/controllers/ProfileFamilyHistoryController.ts @@ -41,9 +41,78 @@ export class ProfileFamilyHistoryController extends Controller { status: 200, message: "สำเร็จ", result: { - status: 200, - message: "สำเร็จ", - result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + createdAt: "2024-03-19T11:00:29.769Z", + createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + lastUpdatedAt: "2024-03-19T11:00:29.769Z", + lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + createdFullName: "สาวิตรี ศรีสมัย", + lastUpdateFullName: "สาวิตรี ศรีสมัย", + isActive: true, + couple: true, + couplePrefix: "string", + coupleFirstName: "string", + coupleLastName: "string", + coupleLastNameOld: "string", + coupleCareer: "string", + coupleCitizenId: "string", + coupleLive: true, + fatherPrefix: "string", + fatherFirstName: "string", + fatherLastName: "string", + fatherCareer: "string", + fatherCitizenId: "string", + fatherLive: true, + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + children: [ + { + id: "9717f95a-cd20-4edb-a8b9-443c1dd99f90", + createdAt: "2024-03-19T11:00:29.785Z", + createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + lastUpdatedAt: "2024-03-19T11:00:29.785Z", + lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + createdFullName: "สาวิตรี ศรีสมัย", + lastUpdateFullName: "สาวิตรี ศรีสมัย", + isActive: true, + childrenCareer: "string", + childrenFirstName: "string", + childrenLastName: "string", + childrenPrefix: "string", + childrenLive: "string", + childrenCitizenId: "string", + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + }, + ], + }, + }) + public async getFamilyHistory(@Path() profileId: string) { + const family = await this.familyHistoryRepo.find({ + take: 1, + order: { createdAt: "DESC" }, + where: { profileId }, + }); + const children = await this.childrenRepo.find({ + order: { createdAt: "DESC" }, + where: { profileId }, + }); + return new HttpSuccess({ + ...family[0], + children, + }); + } + + @Get("history/{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { id: "6207ae29-05ef-4abb-9a37-a887265d671e", createdAt: "2024-03-19T11:00:29.769Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", @@ -75,47 +144,78 @@ export class ProfileFamilyHistoryController extends Controller { profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", children: [ { - id: "9717f95a-cd20-4edb-a8b9-443c1dd99f90", - createdAt: "2024-03-19T11:00:29.785Z", + id: "980835f8-d766-4b6b-b7dc-1726db889352", + createdAt: "2024-03-19T12:42:11.437Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", - lastUpdatedAt: "2024-03-19T11:00:29.785Z", + lastUpdatedAt: "2024-03-19T12:42:11.437Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", - isActive: true, - childrenCareer: "string", - childrenFirstName: "string", - childrenLastName: "string", - childrenPrefix: "string", - childrenLive: "string", - childrenCitizenId: "string", + isActive: false, + childrenCareer: "hey", + childrenFirstName: "hey", + childrenLastName: "hey", + childrenPrefix: "hey", + childrenLive: "hey", + childrenCitizenId: "hey", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", + profileChildrenId: "222db098-3f08-4d0b-ac58-7914fa41032b", + }, + { + id: "cd539e67-3f34-4b8c-a9da-b7eb193fa0b4", + createdAt: "2024-03-19T12:42:11.438Z", + createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + lastUpdatedAt: "2024-03-19T12:42:11.438Z", + lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + createdFullName: "สาวิตรี ศรีสมัย", + lastUpdateFullName: "สาวิตรี ศรีสมัย", + isActive: false, + childrenCareer: "ay", + childrenFirstName: "ay", + childrenLastName: "ay", + childrenPrefix: "ay", + childrenLive: "ay", + childrenCitizenId: "ay", + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e", + profileChildrenId: "d0495491-9e9f-4d2e-bc3b-c2833955fa12", }, ], }, - }, - }) - public async getFamilyHistory(@Path() profileId: string) { - const family = await this.familyHistoryRepo.find({ - take: 1, - order: { createdAt: "DESC" }, - where: { profileId }, - }); - const children = await this.childrenRepo.find({ - order: { createdAt: "DESC" }, - where: { profileId }, - }); - return new HttpSuccess({ - ...family[0], - children, - }); - } - - @Get("history/{profileId}") - @Example({ - status: 200, - message: "สำเร็จ", - result: [], + { + id: "60bbf8d1-f674-4d88-b3fb-b5603e408319", + createdAt: "2024-03-19T11:00:29.769Z", + createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + lastUpdatedAt: "2024-03-19T11:00:29.769Z", + lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", + createdFullName: "สาวิตรี ศรีสมัย", + lastUpdateFullName: "สาวิตรี ศรีสมัย", + isActive: true, + couple: true, + couplePrefix: "string", + coupleFirstName: "string", + coupleLastName: "string", + coupleLastNameOld: "string", + coupleCareer: "yeah", + coupleCitizenId: "yeah", + coupleLive: true, + fatherPrefix: "yeah", + fatherFirstName: "haaa", + fatherLastName: "yeah", + fatherCareer: "haaa", + fatherCitizenId: "yeah", + fatherLive: true, + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + children: [], + }, + ], }) public async familyHistory(@Path() profileId: string) { const family = await this.familyHistoryRepo.find({