From be804c356aae28ffa53e6dd7a1560b3b7732bdde Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Mon, 13 May 2024 17:38:21 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9?= =?UTF-8?q?=E0=B8=A5=E0=B8=AD=E0=B8=B7=E0=B9=88=E0=B8=99=E0=B9=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileEmployeeOtherController.ts | 155 ++++++++++++++++++ src/entities/ProfileEmployee.ts | 6 +- src/entities/ProfileOther.ts | 19 +++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/controllers/ProfileEmployeeOtherController.ts diff --git a/src/controllers/ProfileEmployeeOtherController.ts b/src/controllers/ProfileEmployeeOtherController.ts new file mode 100644 index 00000000..548bd458 --- /dev/null +++ b/src/controllers/ProfileEmployeeOtherController.ts @@ -0,0 +1,155 @@ +import { + Body, + Controller, + Delete, + Example, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileOtherHistory } from "../entities/ProfileOtherHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { + CreateProfileEmployeeOther, + ProfileOther, + UpdateProfileOther, +} from "../entities/ProfileOther"; + +@Route("api/v1/org/profile/other") +@Tags("ProfileOther") +@Security("bearerAuth") +export class ProfileOtherController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private otherRepository = AppDataSource.getRepository(ProfileOther); + private otherHistoryRepository = AppDataSource.getRepository(ProfileOtherHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getOther(@Path() profileId: string) { + const lists = await this.otherRepository.find({ + where: { profileEmployeeId: profileId }, + }); + return new HttpSuccess(lists); + } + + @Get("history/{otherId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + detail: "string", + }, + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + detail: "string", + }, + ], + }) + public async otherHistory(@Path() otherId: string) { + const record = await this.otherHistoryRepository.find({ + where: { profileOtherId: otherId }, + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newOther(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeOther) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileOther(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + + await this.otherRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{otherId}") + public async editOther( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileOther, + @Path() otherId: string, + ) { + const record = await this.otherRepository.findOneBy({ id: otherId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileOtherHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileOtherId = otherId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.otherRepository.save(record), + this.otherHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{otherId}") + public async deleteTraning(@Path() otherId: string) { + await this.otherHistoryRepository.delete({ + profileOtherId: otherId, + }); + + const result = await this.otherRepository.delete({ id: otherId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 614828f7..6490a24b 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -18,6 +18,7 @@ import { ProfileChangeName } from "./ProfileChangeName"; import { ProfileFamilyHistory } from "./ProfileFamily"; import { ProfileEducation } from "./ProfileEducation"; import { ProfileAbility } from "./ProfileAbility"; +import { ProfileOther } from "./ProfileOther"; @Entity("profileEmployee") export class ProfileEmployee extends EntityBase { @Column({ @@ -248,6 +249,9 @@ export class ProfileEmployee extends EntityBase { @OneToMany(() => ProfileAbility, (v) => v.profileEmployee) profileAbilities: ProfileAbility[]; + @OneToMany(() => ProfileOther, (v) => v.profileEmployee) + profileOthers: ProfileOther[]; + @ManyToOne(() => EmployeePosLevel, (v) => v.profiles) posLevel: EmployeePosLevel; @@ -291,7 +295,7 @@ export class CreateProfileEmployee { gender: string | null; relationship: string | null; bloodGroup: string | null; - email: string | null ; + email: string | null; phone: string | null; } diff --git a/src/entities/ProfileOther.ts b/src/entities/ProfileOther.ts index db84c3d2..b9ac7a34 100644 --- a/src/entities/ProfileOther.ts +++ b/src/entities/ProfileOther.ts @@ -1,6 +1,7 @@ import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; import { ProfileOtherHistory } from "./ProfileOtherHistory"; @Entity("profileOther") @@ -13,6 +14,14 @@ export class ProfileOther extends EntityBase { }) profileId: string; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; + @Column({ nullable: true, comment: "รายละเอียด", @@ -35,6 +44,10 @@ export class ProfileOther extends EntityBase { @ManyToOne(() => Profile, (profile) => profile.profileOthers) @JoinColumn({ name: "profileId" }) profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileOthers) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export class CreateProfileOther { @@ -43,6 +56,12 @@ export class CreateProfileOther { date: Date | null; } +export class CreateProfileEmployeeOther { + profileEmployeeId: string | null; + detail: string | null; + date: Date | null; +} + export type UpdateProfileOther = { detail?: string | null; date?: Date | null;