diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts index 32c0be05..eacb151d 100644 --- a/src/controllers/ProfileChildrenController.ts +++ b/src/controllers/ProfileChildrenController.ts @@ -25,7 +25,7 @@ import { UpdateProfileChildren, } from "../entities/ProfileChildren"; -@Route("api/v1/org/profile/children") +@Route("api/v1/org/profile/family/children") @Tags("ProfileChildren") @Security("bearerAuth") export class ProfileChildrenController extends Controller { @@ -59,7 +59,6 @@ export class ProfileChildrenController extends Controller { } const data = new ProfileChildren(); - const meta = { createdUserId: req.user.sub, createdFullName: req.user.name, @@ -68,9 +67,23 @@ export class ProfileChildrenController extends Controller { }; Object.assign(data, { ...body, ...meta }); - await this.childrenRepository.save(data); - + if(data){ + const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { + profileChildrenId: data.id, + childrenCareer: data.childrenCareer, + childrenFirstName: data.childrenFirstName, + childrenLastName: data.childrenLastName, + childrenPrefix: data.childrenPrefix, + childrenLive: data.childrenLive, + childrenCitizenId: data.childrenCitizenId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.childrenHistoryRepository.save(history); + } return new HttpSuccess(); } @@ -81,17 +94,22 @@ export class ProfileChildrenController extends Controller { @Path() childrenId: string, ) { const record = await this.childrenRepository.findOneBy({ id: childrenId }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - history.profileChildrenId = childrenId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.profileChildrenId = record.id; + history.childrenCareer = record.childrenCareer; + history.childrenFirstName = record.childrenFirstName; + history.childrenLastName = record.childrenLastName; + history.childrenPrefix = record.childrenPrefix; + history.childrenLive = record.childrenLive; + history.childrenCitizenId = record.childrenCitizenId; + history.lastUpdateUserId = req.user.sub, history.lastUpdateFullName = req.user.name; - await Promise.all([ this.childrenRepository.save(record), this.childrenHistoryRepository.save(history), @@ -108,7 +126,7 @@ export class ProfileChildrenController extends Controller { const result = await this.childrenRepository.delete({ id: childrenId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts index bb71274a..64c28c61 100644 --- a/src/controllers/ProfileChildrenEmployeeController.ts +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -27,7 +27,7 @@ import { } from "../entities/ProfileChildren"; import { ProfileEmployee } from "../entities/ProfileEmployee"; -@Route("api/v1/org/profile-employee/children") +@Route("api/v1/org/profile-employee/family/children") @Tags("ProfileChildren") @Security("bearerAuth") export class ProfileChildrenEmployeeController extends Controller { @@ -35,10 +35,10 @@ export class ProfileChildrenEmployeeController extends Controller { private childrenRepository = AppDataSource.getRepository(ProfileChildren); private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory); - @Get("{profileId}") - public async getChildren(@Path() profileId: string) { + @Get("{profileEmployeeId}") + public async getChildren(@Path() profileEmployeeId: string) { const lists = await this.childrenRepository.find({ - where: { profileEmployeeId: profileId }, + where: { profileEmployeeId: profileEmployeeId }, }); return new HttpSuccess(lists); } @@ -73,8 +73,23 @@ export class ProfileChildrenEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); - await this.childrenRepository.save(data); + if(data){ + const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { + profileChildrenId: data.id, + childrenCareer: data.childrenCareer, + childrenFirstName: data.childrenFirstName, + childrenLastName: data.childrenLastName, + childrenPrefix: data.childrenPrefix, + childrenLive: data.childrenLive, + childrenCitizenId: data.childrenCitizenId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.childrenHistoryRepository.save(history); + } return new HttpSuccess(); } @@ -86,15 +101,21 @@ export class ProfileChildrenEmployeeController extends Controller { @Path() childrenId: string, ) { const record = await this.childrenRepository.findOneBy({ id: childrenId }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - history.profileChildrenId = childrenId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.profileChildrenId = record.id; + history.childrenCareer = record.childrenCareer; + history.childrenFirstName = record.childrenFirstName; + history.childrenLastName = record.childrenLastName; + history.childrenPrefix = record.childrenPrefix; + history.childrenLive = record.childrenLive; + history.childrenCitizenId = record.childrenCitizenId; + history.lastUpdateUserId = req.user.sub, history.lastUpdateFullName = req.user.name; await Promise.all([ @@ -113,7 +134,7 @@ export class ProfileChildrenEmployeeController extends Controller { const result = await this.childrenRepository.delete({ id: childrenId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts index e79a5977..c2d3d2e9 100644 --- a/src/controllers/ProfileFamilyCoupleController.ts +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -33,7 +33,7 @@ export class ProfileFamilyCoupleController extends Controller { @Example({ status: 200, message: "สำเร็จ", - result: { + result: [{ id: "6207ae29-05ef-4abb-9a37-a887265d671e", couple: true, couplePrefix: "string", @@ -45,7 +45,7 @@ export class ProfileFamilyCoupleController extends Controller { coupleLive: true, relationship: "string", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - }, + }], }) public async getFamilyCouple(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ @@ -55,7 +55,7 @@ export class ProfileFamilyCoupleController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const familyCouple = await this.ProfileFamilyCouple.findOne({ + const familyCouple = await this.ProfileFamilyCouple.find({ select: [ "id", "couple", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileId", diff --git a/src/entities/ProfileChildren.ts b/src/entities/ProfileChildren.ts index 4e3c929b..c277713e 100644 --- a/src/entities/ProfileChildren.ts +++ b/src/entities/ProfileChildren.ts @@ -74,11 +74,14 @@ export class ProfileChildren extends EntityBase { @JoinColumn({ name: "profileEmployeeId" }) profileEmployee: ProfileEmployee; - @OneToMany( - () => ProfileChildrenHistory, - (profileChildrenHistory) => profileChildrenHistory.histories, - ) - profileChildrenHistories: ProfileChildrenHistory[]; + // @OneToMany( + // () => ProfileChildrenHistory, + // (profileChildrenHistory) => profileChildrenHistory.histories, + // ) + // profileChildrenHistories: ProfileChildrenHistory[]; + + @OneToMany(() => ProfileChildrenHistory, (v) => v.profileChildrenHistories) + histories: ProfileChildrenHistory[]; } export type CreateProfileChildren = { @@ -102,7 +105,7 @@ export type CreateProfileChildrenEmployee = { }; export type UpdateProfileChildren = { - id: string; + // id: string; childrenCareer?: string | null; childrenFirstName?: string | null; childrenLastName?: string | null; diff --git a/src/entities/ProfileChildrenHistory.ts b/src/entities/ProfileChildrenHistory.ts index 3c2dae6e..2ce86751 100644 --- a/src/entities/ProfileChildrenHistory.ts +++ b/src/entities/ProfileChildrenHistory.ts @@ -57,9 +57,13 @@ export class ProfileChildrenHistory extends EntityBase { }) profileChildrenId: string; - @ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories) + // @ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories) + // @JoinColumn({ name: "profileChildrenId" }) + // histories: ProfileChildren; + + @ManyToOne(() => ProfileChildren, (v) => v.histories) @JoinColumn({ name: "profileChildrenId" }) - histories: ProfileChildren; + profileChildrenHistories: ProfileChildren; } export type CreateChildren = {