From 13fc7945e5e4c5a3cb0373e1d76d779e796ad115 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 13 Mar 2024 15:33:02 +0700 Subject: [PATCH] crud profile discip,duty,nopaid,other --- .../ProfileDisciplineController.ts | 186 ++++++++++++++++++ src/controllers/ProfileDutyController.ts | 176 +++++++++++++++++ src/controllers/ProfileNopaidController.ts | 158 +++++++++++++++ src/controllers/ProfileOtherContorller.ts | 152 ++++++++++++++ src/entities/Profile.ts | 2 +- src/entities/ProfileDiscipline.ts | 29 +++ .../{ProfileDutys.ts => ProfileDuty.ts} | 0 src/entities/ProfileDutyHistory.ts | 2 +- src/entities/ProfileOtherHistory.ts | 6 +- 9 files changed, 706 insertions(+), 5 deletions(-) create mode 100644 src/controllers/ProfileDisciplineController.ts create mode 100644 src/controllers/ProfileDutyController.ts create mode 100644 src/controllers/ProfileNopaidController.ts create mode 100644 src/controllers/ProfileOtherContorller.ts rename src/entities/{ProfileDutys.ts => ProfileDuty.ts} (100%) diff --git a/src/controllers/ProfileDisciplineController.ts b/src/controllers/ProfileDisciplineController.ts new file mode 100644 index 00000000..9a36c40c --- /dev/null +++ b/src/controllers/ProfileDisciplineController.ts @@ -0,0 +1,186 @@ +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 { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { + CreateProfileDiscipline, + ProfileDiscipline, + UpdateProfileDiscipline, +} from "../entities/ProfileDiscipline"; + +@Route("api/v1/org/profile/disipline") +@Tags("ProfileDiscipline") +@Security("bearerAuth") +export class ProfileDisciplineController extends Controller { + private profileRepository = AppDataSource.getRepository(Profile); + private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); + private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getDiscipline(@Path() profileId: string) { + const lists = await this.disciplineRepository.find({ + where: { profileId: profileId }, + select: [ + "id", + "isActive", + "date", + "level", + "detail", + "unStigma", + "refCommandNo", + "refCommandDate", + ], + }); + return new HttpSuccess(lists); + } + + @Get("history/{disciplineId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + { + id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f", + isActive: true, + date: "2024-03-12T10:09:47.000Z", + level: "string", + detail: "string", + unStigma: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async disciplineHistory(@Path() disciplineId: string) { + const record = await this.disciplineHistoryRepository.find({ + where: { profileDisciplineId: disciplineId }, + select: [ + "id", + "isActive", + "date", + "level", + "detail", + "unStigma", + "refCommandNo", + "refCommandDate", + ], + order: { createdAt: "DESC" } + }); + return new HttpSuccess(record); + } + + @Post() + public async newDiscipline( + @Request() req: RequestWithUser, + @Body() body: CreateProfileDiscipline, + ) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileDiscipline(); + + 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.disciplineRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{disciplineId}") + public async editDiscipline( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileDiscipline, + @Path() disciplineId: string, + ) { + const record = await this.disciplineRepository.findOneBy({ id: disciplineId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileDisciplineHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileDisciplineId = disciplineId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.disciplineRepository.save(record), + this.disciplineHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{disciplineId}") + public async deleteTraning(@Path() disciplineId: string) { + await this.disciplineHistoryRepository.delete({ + profileDisciplineId: disciplineId, + }); + + const result = await this.disciplineRepository.delete({ id: disciplineId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileDutyController.ts b/src/controllers/ProfileDutyController.ts new file mode 100644 index 00000000..d13a36d9 --- /dev/null +++ b/src/controllers/ProfileDutyController.ts @@ -0,0 +1,176 @@ +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 { ProfileDutyHistory } from "../entities/ProfileDutyHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { CreateProfileDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; + +@Route("api/v1/org/profile/duty") +@Tags("ProfileDuty") +@Security("bearerAuth") +export class ProfileDutyController extends Controller { + private profileRepository = AppDataSource.getRepository(Profile); + private dutyRepository = AppDataSource.getRepository(ProfileDuty); + private dutyHistoryRepository = AppDataSource.getRepository(ProfileDutyHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async getDuty(@Path() profileId: string) { + const lists = await this.dutyRepository.find({ + where: { profileId: profileId }, + select: [ + "id", + "isActive", + "dateStart", + "dateEnd", + "reference", + "detail", + "refCommandNo", + "refCommandDate", + ], + }); + return new HttpSuccess(lists); + } + + @Get("history/{dutyId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: [ + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + { + id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", + isActive: true, + dateStart: "2024-03-12T10:09:47.000Z", + dateEnd: "string", + reference: "string", + detail: "string", + refCommandNo: "string", + refCommandDate: "2024-03-12T10:09:47.000Z", + }, + ], + }) + public async dutyHistory(@Path() dutyId: string) { + const record = await this.dutyHistoryRepository.find({ + where: { profileDutyId: dutyId }, + select: [ + "id", + "isActive", + "dateStart", + "dateEnd", + "reference", + "detail", + "refCommandNo", + "refCommandDate", + ], + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newDuty(@Request() req: RequestWithUser, @Body() body: CreateProfileDuty) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileDuty(); + + 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.dutyRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{dutyId}") + public async editDuty( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileDuty, + @Path() dutyId: string, + ) { + const record = await this.dutyRepository.findOneBy({ id: dutyId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileDutyHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileDutyId = dutyId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{dutyId}") + public async deleteTraning(@Path() dutyId: string) { + await this.dutyHistoryRepository.delete({ + profileDutyId: dutyId, + }); + + const result = await this.dutyRepository.delete({ id: dutyId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileNopaidController.ts b/src/controllers/ProfileNopaidController.ts new file mode 100644 index 00000000..eda2cc40 --- /dev/null +++ b/src/controllers/ProfileNopaidController.ts @@ -0,0 +1,158 @@ +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 { ProfileNopaidHistory } from "../entities/ProfileNopaidHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { CreateProfileNopaid, ProfileNopaid, UpdateProfileNopaid } from "../entities/ProfileNopaid"; + +@Route("api/v1/org/profile/nopaid") +@Tags("ProfileNopaid") +@Security("bearerAuth") +export class ProfileNopaidController extends Controller { + private profileRepository = AppDataSource.getRepository(Profile); + private nopaidRepository = AppDataSource.getRepository(ProfileNopaid); + private nopaidHistoryRepository = AppDataSource.getRepository(ProfileNopaidHistory); + + @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 getNopaid(@Path() profileId: string) { + const lists = await this.nopaidRepository.find({ + where: { profileId: profileId }, + select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"], + }); + return new HttpSuccess(lists); + } + + @Get("history/{nopaidId}") + @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", + }, + { + 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 nopaidHistory(@Path() nopaidId: string) { + const record = await this.nopaidHistoryRepository.find({ + where: { profileNopaidId: nopaidId }, + select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"], + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newNopaid(@Request() req: RequestWithUser, @Body() body: CreateProfileNopaid) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileNopaid(); + + 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.nopaidRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{nopaidId}") + public async editNopaid( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileNopaid, + @Path() nopaidId: string, + ) { + const record = await this.nopaidRepository.findOneBy({ id: nopaidId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileNopaidHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileNopaidId = nopaidId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.nopaidRepository.save(record), + this.nopaidHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{nopaidId}") + public async deleteTraning(@Path() nopaidId: string) { + await this.nopaidHistoryRepository.delete({ + profileNopaidId: nopaidId, + }); + + const result = await this.nopaidRepository.delete({ id: nopaidId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileOtherContorller.ts b/src/controllers/ProfileOtherContorller.ts new file mode 100644 index 00000000..2ac69301 --- /dev/null +++ b/src/controllers/ProfileOtherContorller.ts @@ -0,0 +1,152 @@ +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 { CreateProfileOther, ProfileOther, UpdateProfileOther } from "../entities/ProfileOther"; + +@Route("api/v1/org/profile/other") +@Tags("ProfileOther") +@Security("bearerAuth") +export class ProfileOtherController extends Controller { + private profileRepository = AppDataSource.getRepository(Profile); + 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: { profileId: profileId }, + select: ["id", "isActive", "date", "detail"], + }); + 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 }, + select: ["id", "isActive", "date", "detail"], + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newOther(@Request() req: RequestWithUser, @Body() body: CreateProfileOther) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + + 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/Profile.ts b/src/entities/Profile.ts index 4962137f..11bd6e0c 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -13,7 +13,7 @@ import { ProfileHonor } from "./ProfileHonor"; import { ProfileAssessment } from "./ProfileAssessment"; import { ProfileLeave } from "./ProfileLeave"; import { ProfileAbility } from "./ProfileAbility"; -import { ProfileDuty } from "./ProfileDutys"; +import { ProfileDuty } from "./ProfileDuty"; import { ProfileNopaid } from "./ProfileNopaid"; import { ProfileOther } from "./ProfileOther"; diff --git a/src/entities/ProfileDiscipline.ts b/src/entities/ProfileDiscipline.ts index 2272730a..0a4fbcc1 100644 --- a/src/entities/ProfileDiscipline.ts +++ b/src/entities/ProfileDiscipline.ts @@ -73,3 +73,32 @@ export class ProfileDiscipline extends EntityBase { @JoinColumn({ name: "profileId" }) profile: Profile; } + +export class CreateProfileDiscipline { + + @Column() + date: Date | null; + + @Column() + profileId: string; + + @Column() + isActive: boolean | null; + + @Column() + level: string | null; + + @Column() + detail: string | null; + + @Column() + refCommandDate: Date | null; + + @Column() + refCommandNo: string | null; + + @Column() + unStigma: string | null; +} + +export type UpdateProfileDiscipline = Partial; \ No newline at end of file diff --git a/src/entities/ProfileDutys.ts b/src/entities/ProfileDuty.ts similarity index 100% rename from src/entities/ProfileDutys.ts rename to src/entities/ProfileDuty.ts diff --git a/src/entities/ProfileDutyHistory.ts b/src/entities/ProfileDutyHistory.ts index f9f41395..8f7da298 100644 --- a/src/entities/ProfileDutyHistory.ts +++ b/src/entities/ProfileDutyHistory.ts @@ -1,7 +1,7 @@ import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; -import { ProfileDuty } from "./ProfileDutys"; +import { ProfileDuty } from "./ProfileDuty"; @Entity("profileDutyHistory") export class ProfileDutyHistory extends EntityBase { diff --git a/src/entities/ProfileOtherHistory.ts b/src/entities/ProfileOtherHistory.ts index c4f7350e..fce37b14 100644 --- a/src/entities/ProfileOtherHistory.ts +++ b/src/entities/ProfileOtherHistory.ts @@ -10,7 +10,7 @@ export class ProfileOtherHistory extends EntityBase { comment: "คีย์นอก(FK)ของตาราง Profile", default: null, }) - profileId: string; + profileOtherId: string; @Column({ comment: "สถานะการใช้งาน", @@ -35,13 +35,13 @@ export class ProfileOtherHistory extends EntityBase { date: Date; @ManyToOne(() => ProfileOther, (profileOther) => profileOther.profileOtherHistories) - @JoinColumn({ name: "profileId" }) + @JoinColumn({ name: "profileOtherId" }) histories: ProfileOther; } export class CreateProfileOtherHistory { @Column("uuid") - profileId: string | null; + profileOtherId: string | null; @Column() isActive: boolean;