diff --git a/src/controllers/ProfileAddressEmployeeController.ts b/src/controllers/ProfileAddressEmployeeController.ts new file mode 100644 index 00000000..8619d92c --- /dev/null +++ b/src/controllers/ProfileAddressEmployeeController.ts @@ -0,0 +1,135 @@ +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 { RequestWithUser } from "../middlewares/user"; +import { Profile, ProfileAddressHistory, UpdateProfileAddress } from "../entities/Profile"; +import { AppDataSource } from "../database/data-source"; +import { Province } from "../entities/Province"; +import { District } from "../entities/District"; +import { SubDistrict } from "../entities/SubDistrict"; +import { ProfileEmployee, UpdateProfileAddressEmployee } from "../entities/ProfileEmployee"; + +@Route("api/v1/org/profile-employee/address") +@Tags("ProfileAddressEmployee") +@Security("bearerAuth") +export class ProfileAddressEmployeeController extends Controller { + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); + private profileAddressHistoryRepo = AppDataSource.getRepository(ProfileAddressHistory); + + /** + * + * @summary ข้อมูลที่อยู่ + * + */ + @Get("{profileEmployeeId}") + public async detailProfileAddress(@Path() profileEmployeeId: string) { + const getProfileAddress = await this.profileEmployeeRepo.findOne({ + where: { id: profileEmployeeId }, + select: [ + "id", + "registrationAddress", + "registrationProvinceId", + "registrationDistrictId", + "registrationSubDistrictId", + "registrationZipCode", + "currentAddress", + "currentProvinceId", + "currentDistrictId", + "currentSubDistrictId", + "currentZipCode", + ], + }); + if (!getProfileAddress) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getProfileAddress); + } + + /** + * + * @summary ประวัติแก้ไขที่อยู่ + * + */ + @Get("history/{addressId}") + public async getProfileAddressHistory(@Path() addressId: string) { + const record = await this.profileAddressHistoryRepo.find({ + where: { profileEmployeeId: addressId }, + relations: { + registrationProvince: true, + registrationDistrict: true, + registrationSubDistrict: true, + currentProvince: true, + currentDistrict: true, + currentSubDistrict: true, + }, + select: [ + "registrationAddress", + "registrationProvinceId", + "registrationDistrictId", + "registrationSubDistrictId", + "registrationZipCode", + "currentAddress", + "currentProvinceId", + "currentDistrictId", + "currentSubDistrictId", + "currentZipCode", + "lastUpdateFullName", + "lastUpdatedAt", + ], + }); + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(record); + } + + /** + * + * @summary แก้ไขที่อยู่ + * + */ + @Patch("{addressId}") + public async editProfileAddress( + @Body() requestBody: UpdateProfileAddressEmployee, + @Request() req: RequestWithUser, + @Path() addressId: string, + ) { + const record = await this.profileEmployeeRepo.findOneBy({ id: addressId }); + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileAddressHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, requestBody); + + history.profileEmployeeId = addressId; + history.lastUpdateFullName = req.user.name; + record.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.profileEmployeeRepo.save(record), + this.profileAddressHistoryRepo.save(history), + ]); + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 8f0a7d4d..d2c87cf1 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -242,7 +242,7 @@ export class ProfileController extends Controller { profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name; profile.dateRetire = calculateRetireDate(profile.birthDate); - profile.dateRetireLaw = calculateRetireDate(profile.birthDate); + // profile.dateRetireLaw = calculateRetireDate(profile.birthDate); await this.profileRepo.save(profile); @@ -383,7 +383,7 @@ export class ProfileController extends Controller { record.lastUpdateUserId = request.user.sub; record.lastUpdateFullName = request.user.name; record.dateRetire = calculateRetireDate(record.birthDate); - record.dateRetireLaw = calculateRetireDate(record.birthDate); + // record.dateRetireLaw = calculateRetireDate(record.birthDate); await this.profileRepo.save(record); return new HttpSuccess(); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 2049cb0b..05d58bd1 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -115,7 +115,7 @@ export class ProfileEmployeeController extends Controller { profile.lastUpdateUserId = request.user.sub; profile.lastUpdateFullName = request.user.name; profile.dateRetire = calculateRetireDate(profile.birthDate); - profile.dateRetireLaw = calculateRetireDate(profile.birthDate); + // profile.dateRetireLaw = calculateRetireDate(profile.birthDate); await this.profileRepo.save(profile); return new HttpSuccess(); @@ -205,7 +205,7 @@ export class ProfileEmployeeController extends Controller { record.lastUpdateUserId = request.user.sub; record.lastUpdateFullName = request.user.name; record.dateRetire = calculateRetireDate(record.birthDate); - record.dateRetireLaw = calculateRetireDate(record.birthDate); + // record.dateRetireLaw = calculateRetireDate(record.birthDate); await this.profileRepo.save(record); diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 8be9074f..bfca8489 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -26,6 +26,7 @@ import { ProfileFamilyMother } from "./ProfileFamilyMother"; import { ProfileFamilyCouple } from "./ProfileFamilyCouple"; import { ProfileChildren } from "./ProfileChildren"; import { ProfileDiscipline } from "./ProfileDiscipline"; +import { ProfileEmployee } from "./ProfileEmployee"; @Entity("profile") export class Profile extends EntityBase { @@ -580,6 +581,18 @@ export class ProfileAddressHistory extends EntityBase { @ManyToOne(() => Profile, (v) => v.histories, { onDelete: "CASCADE" }) profile: Profile; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileAddressHistories) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export class CreateProfile { diff --git a/src/entities/ProfileEducation.ts b/src/entities/ProfileEducation.ts index 5d8db29e..cb8fb542 100644 --- a/src/entities/ProfileEducation.ts +++ b/src/entities/ProfileEducation.ts @@ -189,7 +189,7 @@ export class CreateProfileEducation { country: string | null; degree: string | null; duration: string | null; - durationYear: number; + durationYear?: number | null; field: string | null; finishDate: Date | null; fundName: string | null; @@ -212,7 +212,7 @@ export class CreateProfileEducationEmployee { country: string | null; degree: string | null; duration: string | null; - durationYear: number; + durationYear?: number | null; field: string | null; finishDate: Date | null; fundName: string | null; @@ -234,7 +234,7 @@ export type UpdateProfileEducation = { country?: string | null; degree?: string | null; duration?: string | null; - durationYear?: number; + durationYear?: number | null; field?: string | null; finishDate?: Date | null; fundName?: string | null; diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 6d0e334a..4421a7fb 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -25,6 +25,10 @@ import { ProfileFamilyMother } from "./ProfileFamilyMother"; import { ProfileFamilyCouple } from "./ProfileFamilyCouple"; import { ProfileChildren } from "./ProfileChildren"; +import { Profile, ProfileAddressHistory } from "./Profile"; +import { Province } from "./Province"; +import { District } from "./District"; +import { SubDistrict } from "./SubDistrict"; @Entity("profileEmployee") export class ProfileEmployee extends EntityBase { @Column({ @@ -285,6 +289,9 @@ export class ProfileEmployee extends EntityBase { @OneToMany(() => ProfileEmployeeHistory, (v) => v.profileEmployee) histories: ProfileEmployeeHistory[]; + @OneToMany(() => ProfileAddressHistory, (v) => v.profileEmployee) + profileAddressHistories: ProfileAddressHistory[]; + @OneToMany(() => ProfileGovernment, (v) => v.profileEmployee) profileGovernment: ProfileGovernment[]; @@ -296,6 +303,105 @@ export class ProfileEmployee extends EntityBase { @OneToMany(() => ProfileFamilyCouple, (v) => v.profile) profileFamilyCouple: ProfileFamilyCouple[]; + + //ที่อยู่ + @Column({ + nullable: true, + comment: "ที่อยู่ตามทะเบียนบ้าน", + default: null, + length: 255, + }) + registrationAddress: string; + + @Column({ + nullable: true, + comment: "จังหวัดตามทะเบียนบ้าน", + length: 255, + default: null, + }) + registrationProvinceId: string; + + @ManyToOne(() => Province, (v) => v.registrationProvinces) + registrationProvince: Province; + + @Column({ + nullable: true, + comment: "เขตตามทะเบียนบ้าน", + length: 255, + default: null, + }) + registrationDistrictId: string; + + @ManyToOne(() => District, (v) => v.registrationDistricts) + registrationDistrict: District; + + @Column({ + nullable: true, + comment: "แขวงตามทะเบียนบ้าน", + length: 255, + default: null, + }) + registrationSubDistrictId: string; + + @ManyToOne(() => SubDistrict, (v) => v.registrationSubDistricts) + registrationSubDistrict: SubDistrict; + + @Column({ + nullable: true, + comment: "รหัสไปรษณีย์ตามทะเบียนบ้าน", + default: null, + length: 5, + }) + registrationZipCode: string; + + @Column({ + nullable: true, + comment: "ที่อยู่ตามปัจจุบัน", + default: null, + length: 255, + }) + currentAddress: string; + + @Column({ + nullable: true, + comment: "จังหวัดตามปัจจุบัน", + length: 255, + default: null, + }) + currentProvinceId: string; + + @ManyToOne(() => Province, (v) => v.currentProvinces) + currentProvince: Province; + + @Column({ + nullable: true, + comment: "เขตตามปัจจุบัน", + length: 255, + default: null, + }) + currentDistrictId: string; + + @ManyToOne(() => District, (v) => v.currentDistricts) + currentDistrict: District; + + @Column({ + nullable: true, + comment: "แขวงตามปัจจุบัน", + length: 255, + default: null, + }) + currentSubDistrictId: string; + + @ManyToOne(() => SubDistrict, (v) => v.currentSubDistricts) + currentSubDistrict: SubDistrict; + + @Column({ + nullable: true, + comment: "รหัสไปรษณีย์ตามปัจจุบัน", + default: null, + length: 5, + }) + currentZipCode: string; } @Entity("profileEmployeeHistory") @@ -357,3 +463,16 @@ export type UpdateProfileEmployee = { email: string | null; phone: string | null; }; + +export type UpdateProfileAddressEmployee = { + registrationAddress?: string | null; + registrationProvinceId?: string | null; + registrationDistrictId?: string | null; + registrationSubDistrictId?: string | null; + registrationZipCode?: string | null; + currentAddress?: string | null; + currentProvinceId?: string | null; + currentDistrictId?: string | null; + currentSubDistrictId?: string | null; + currentZipCode?: string | null; +}; \ No newline at end of file