From 86e4208310ae33f97eff04debcef2d213b8b046a Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:48:15 +0700 Subject: [PATCH 1/5] feat: profile certificate endpoints --- .../ProfileCertificateController.ts | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/controllers/ProfileCertificateController.ts diff --git a/src/controllers/ProfileCertificateController.ts b/src/controllers/ProfileCertificateController.ts new file mode 100644 index 00000000..b4e17a21 --- /dev/null +++ b/src/controllers/ProfileCertificateController.ts @@ -0,0 +1,116 @@ +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { + CreateProfileCertificate, + ProfileCertificate, + UpdateProfileCertificate, +} from "../entities/ProfileCertificate"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; +import { RequestWithUser } from "../middlewares/user"; + +@Route("api/v1/org/profile/certificate") +@Tags("ProfileCertificate") +@Security("bearerAuth") +export class ProfileCertificateController extends Controller { + private certificateRepo = AppDataSource.getRepository(ProfileCertificate); + private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); + + @Get("{profileId}") + public async getCertificate(@Path() profileId: string) { + const record = await this.certificateRepo.findBy({ profileId }); + return new HttpSuccess(record); + } + + @Get("history/{certificateId}") + public async certificateHistory(@Path() certificateId: string) { + const record = await this.certificateHistoryRepo.findBy({ + profileCertificateId: certificateId, + }); + return record; + } + + @Post() + public async newCertificate( + @Request() req: RequestWithUser, + @Body() body: CreateProfileCertificate, + ) { + const data = new ProfileCertificate(); + const history = new ProfileCertificateHistory(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + Object.assign(history, { ...body, ...meta }); + + const result = await this.certificateRepo.save(data); + + history.profileCertificateId = result.id; + + await this.certificateHistoryRepo.save(history); + + return new HttpSuccess(); + } + + @Patch("{certificateId}") + public async editCertificate( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileCertificate, + @Path() certificateId: string, + ) { + const record = await this.certificateRepo.findOneBy({ id: certificateId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileCertificateHistory(); + + Object.assign(record, body); + Object.assign(history, { ...body, id: undefined }); + history.profileCertificateId = certificateId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.certificateRepo.save(record), + this.certificateHistoryRepo.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{certificateId}") + public async deleteCertificate(@Path() certificateId: string) { + await this.certificateHistoryRepo.delete({ + profileCertificateId: certificateId, + }); + + const certificateResult = await this.certificateRepo.delete({ + id: certificateId, + }); + + if (certificateResult.affected && certificateResult.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} From 0e81e22ae3631ccd3a65b9f438c965d31c0b2814 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:48:33 +0700 Subject: [PATCH 2/5] feat: profile honor endpoints --- src/controllers/ProfileHonorController.ts | 104 ++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/controllers/ProfileHonorController.ts diff --git a/src/controllers/ProfileHonorController.ts b/src/controllers/ProfileHonorController.ts new file mode 100644 index 00000000..f5e34158 --- /dev/null +++ b/src/controllers/ProfileHonorController.ts @@ -0,0 +1,104 @@ +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { CreateProfileHonor, ProfileHonor, UpdateProfileHonor } from "../entities/ProfileHonor"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; +import { RequestWithUser } from "../middlewares/user"; + +@Route("api/v1/org/profile/honor") +@Tags("ProfileHonor") +@Security("bearerAuth") +export class ProfileHonorController extends Controller { + private honorRepo = AppDataSource.getRepository(ProfileHonor); + private honorHistoryRepo = AppDataSource.getRepository(ProfileHonorHistory); + + @Get("{profileId}") + public async getHonor(@Path() profileId: string) { + const record = await this.honorRepo.findBy({ profileId }); + return new HttpSuccess(record); + } + + @Get("history/{honorId}") + public async honorHistory(@Path() honorId: string) { + const record = await this.honorHistoryRepo.findBy({ + profileHonorId: honorId, + }); + return record; + } + + @Post() + public async newHonor(@Request() req: RequestWithUser, @Body() body: CreateProfileHonor) { + const data = new ProfileHonor(); + const history = new ProfileHonorHistory(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + Object.assign(history, { ...body, ...meta }); + + const result = await this.honorRepo.save(data); + + history.profileHonorId = result.id; + + await this.honorHistoryRepo.save(history); + + return new HttpSuccess(); + } + + @Patch("{honorId}") + public async editHonor( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileHonor, + @Path() honorId: string, + ) { + const record = await this.honorRepo.findOneBy({ id: honorId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileHonorHistory(); + + Object.assign(record, body); + Object.assign(history, { ...body, id: undefined }); + history.profileHonorId = honorId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{honorId}") + public async deleteTraning(@Path() honorId: string) { + await this.honorHistoryRepo.delete({ + profileHonorId: honorId, + }); + + const result = await this.honorRepo.delete({ id: honorId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} From 8ac8339a6baa96631817b894d69338c1fbe6fa58 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:48:44 +0700 Subject: [PATCH 3/5] feat: profile insignia endpoints --- src/controllers/ProfileInsigniaController.ts | 108 +++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/controllers/ProfileInsigniaController.ts diff --git a/src/controllers/ProfileInsigniaController.ts b/src/controllers/ProfileInsigniaController.ts new file mode 100644 index 00000000..a24c6730 --- /dev/null +++ b/src/controllers/ProfileInsigniaController.ts @@ -0,0 +1,108 @@ +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { + CreateProfileInsignia, + ProfileInsignia, + UpdateProfileInsignia, +} from "../entities/ProfileInsignia"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileInsigniaHistory } from "../entities/ProfileInsigniaHistory"; +import { RequestWithUser } from "../middlewares/user"; + +@Route("api/v1/org/profile/insignia") +@Tags("ProfileInsignia") +@Security("bearerAuth") +export class ProfileInsigniaController extends Controller { + private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); + private insigniaHistoryRepo = AppDataSource.getRepository(ProfileInsigniaHistory); + + @Get("{profileId}") + public async getInsignia(@Path() profileId: string) { + const record = await this.insigniaRepo.findBy({ profileId }); + return new HttpSuccess(record); + } + + @Get("history/{InsigniaId}") + public async getInsigniaHistory(@Path() InsigniaId: string) { + const record = await this.insigniaHistoryRepo.findBy({ + profileInsigniaId: InsigniaId, + }); + return record; + } + + @Post() + public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) { + const data = new ProfileInsignia(); + const history = new ProfileInsigniaHistory(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + Object.assign(history, { ...body, ...meta }); + + const result = await this.insigniaRepo.save(data); + + history.profileInsigniaId = result.id; + + await this.insigniaHistoryRepo.save(data); + + return new HttpSuccess(); + } + + @Patch("{insigniaId}") + public async editInsignia( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileInsignia, + @Path() insigniaId: string, + ) { + const record = await this.insigniaRepo.findOneBy({ id: insigniaId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileInsigniaHistory(); + + Object.assign(record, body); + Object.assign(history, { ...body, id: undefined }); + history.profileInsigniaId = insigniaId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{insigniaId}") + public async deleteInsignia(@Path() insigniaId: string) { + await this.insigniaHistoryRepo.delete({ + profileInsigniaId: insigniaId, + }); + + const result = await this.insigniaRepo.delete({ id: insigniaId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} From 294411a1147c07f880175c2293309cf04ee5f0c5 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:48:54 +0700 Subject: [PATCH 4/5] feat: profile training endpoints --- src/controllers/ProfileTrainingController.ts | 108 +++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/controllers/ProfileTrainingController.ts diff --git a/src/controllers/ProfileTrainingController.ts b/src/controllers/ProfileTrainingController.ts new file mode 100644 index 00000000..e5a02183 --- /dev/null +++ b/src/controllers/ProfileTrainingController.ts @@ -0,0 +1,108 @@ +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { + CreateProfileTraining, + ProfileTraining, + UpdateProfileTraining, +} from "../entities/ProfileTraining"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; +import { RequestWithUser } from "../middlewares/user"; + +@Route("api/v1/org/profile/training") +@Tags("ProfileTraining") +@Security("bearerAuth") +export class ProfileTrainingController extends Controller { + private trainingRepo = AppDataSource.getRepository(ProfileTraining); + private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); + + @Get("{profileId}") + public async getTraining(@Path() profileId: string) { + const record = await this.trainingRepo.findBy({ profileId }); + return new HttpSuccess(record); + } + + @Get("history/{trainingId}") + public async trainingHistory(@Path() trainingId: string) { + const record = await this.trainingHistoryRepo.findBy({ + profileTrainingId: trainingId, + }); + return record; + } + + @Post() + public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileTraining) { + const data = new ProfileTraining(); + const history = new ProfileTrainingHistory(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + Object.assign(history, { ...body, ...meta }); + + const result = await this.trainingRepo.save(data); + + history.profileTrainingId = result.id; + + await this.trainingHistoryRepo.save(history); + + return new HttpSuccess(); + } + + @Patch("{trainingId}") + public async editTraining( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileTraining, + @Path() trainingId: string, + ) { + const record = await this.trainingRepo.findOneBy({ id: trainingId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileTrainingHistory(); + + Object.assign(record, body); + Object.assign(history, { ...body, id: undefined }); + history.profileTrainingId = trainingId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{trainingId}") + public async deleteTraining(@Path() trainingId: string) { + await this.trainingHistoryRepo.delete({ + profileTrainingId: trainingId, + }); + + const result = await this.trainingRepo.delete({ id: trainingId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} From 2f9314bc9c5d3ebe69fab268d5952f220274c326 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:49:07 +0700 Subject: [PATCH 5/5] fix: missing id --- src/middlewares/user.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/middlewares/user.ts b/src/middlewares/user.ts index 12c5d597..a35cdc4a 100644 --- a/src/middlewares/user.ts +++ b/src/middlewares/user.ts @@ -2,6 +2,7 @@ import type { Request } from "express"; export type RequestWithUser = Request & { user: { + sub: string; name: string; given_name: string; familiy_name: string;