From 76a2213cd82136b28467e7ba3dea0722ab1505cc Mon Sep 17 00:00:00 2001 From: kittapath Date: Sat, 8 Mar 2025 00:46:01 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=20commandcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release.yaml | 72 +++++----- src/controllers/CommandCodeController.ts | 165 +++++++++++++++++++++++ src/entities/CommandCode.ts | 30 +++++ 3 files changed, 231 insertions(+), 36 deletions(-) create mode 100644 src/controllers/CommandCodeController.ts create mode 100644 src/entities/CommandCode.ts diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c4b02bcc..21dc9ba3 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -67,40 +67,40 @@ jobs: docker compose pull docker compose up -d echo "${{ steps.gen_ver.outputs.image_ver }}"> success - - name: Notify Discord Success - if: success() - run: | - curl -H "Content-Type: application/json" \ - -X POST \ - -d '{ - "embeds": [{ - "title": "✅ Deployment Success!", - "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", - "color": 3066993, - "footer": { - "text": "Release Notification", - "icon_url": "https://example.com/success-icon.png" - }, - "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - }] - }' \ - ${{ secrets.DISCORD_WEBHOOK }} + # - name: Notify Discord Success + # if: success() + # run: | + # curl -H "Content-Type: application/json" \ + # -X POST \ + # -d '{ + # "embeds": [{ + # "title": "✅ Deployment Success!", + # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", + # "color": 3066993, + # "footer": { + # "text": "Release Notification", + # "icon_url": "https://example.com/success-icon.png" + # }, + # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + # }] + # }' \ + # ${{ secrets.DISCORD_WEBHOOK }} - - name: Notify Discord Failure - if: failure() - run: | - curl -H "Content-Type: application/json" \ - -X POST \ - -d '{ - "embeds": [{ - "title": "❌ Deployment Failed!", - "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", - "color": 15158332, - "footer": { - "text": "Release Notification", - "icon_url": "https://example.com/failure-icon.png" - }, - "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - }] - }' \ - ${{ secrets.DISCORD_WEBHOOK }} + # - name: Notify Discord Failure + # if: failure() + # run: | + # curl -H "Content-Type: application/json" \ + # -X POST \ + # -d '{ + # "embeds": [{ + # "title": "❌ Deployment Failed!", + # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", + # "color": 15158332, + # "footer": { + # "text": "Release Notification", + # "icon_url": "https://example.com/failure-icon.png" + # }, + # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + # }] + # }' \ + # ${{ secrets.DISCORD_WEBHOOK }} diff --git a/src/controllers/CommandCodeController.ts b/src/controllers/CommandCodeController.ts new file mode 100644 index 00000000..1049376b --- /dev/null +++ b/src/controllers/CommandCodeController.ts @@ -0,0 +1,165 @@ +import { + Controller, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Response, + Get, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; +import { CommandCode, CreateCommandCode, UpdateCommandCode } from "../entities/CommandCode"; + +@Route("api/v1/org/metadata/commandCode") +@Tags("CommandCode") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +export class CommandCodeController extends Controller { + private commandCodeRepository = AppDataSource.getRepository(CommandCode); + + /** + * API list รายการเชื่อมโยงคำสั่ง + * + * @summary ORG_058 - CRUD เชื่อมโยงคำสั่ง (ADMIN) #62 + * + */ + @Get() + async GetResult() { + const _commandCode = await this.commandCodeRepository.find({ + select: [ + "id", + "name", + "code", + "createdAt", + "lastUpdatedAt", + "createdFullName", + "lastUpdateFullName", + ], + order: { code: "ASC" }, + }); + return new HttpSuccess(_commandCode); + } + + /** + * API รายละเอียดรายการเชื่อมโยงคำสั่ง + * + * @summary ORG_058 - CRUD เชื่อมโยงคำสั่ง (ADMIN) #62 + * + * @param {string} id Id เชื่อมโยงคำสั่ง + */ + @Get("{id}") + async GetById(@Path() id: string) { + const _commandCode = await this.commandCodeRepository.findOne({ + where: { id }, + select: ["id", "name", "code"], + }); + if (!_commandCode) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงคำสั่งนี้"); + } + + return new HttpSuccess(_commandCode); + } + + /** + * API สร้างรายการ body เชื่อมโยงคำสั่ง + * + * @summary ORG_058 - CRUD เชื่อมโยงคำสั่ง (ADMIN) #62 + * + */ + @Post() + async Post( + @Body() + requestBody: CreateCommandCode, + @Request() request: RequestWithUser, + ) { + const _commandCode = Object.assign(new CommandCode(), requestBody); + if (!_commandCode) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงคำสั่งนี้"); + } + + const checkName = await this.commandCodeRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + const before = null; + _commandCode.createdUserId = request.user.sub; + _commandCode.createdFullName = request.user.name; + _commandCode.lastUpdateUserId = request.user.sub; + _commandCode.lastUpdateFullName = request.user.name; + _commandCode.createdAt = new Date(); + _commandCode.lastUpdatedAt = new Date(); + await this.commandCodeRepository.save(_commandCode, { data: request }); + setLogDataDiff(request, { before, after: _commandCode }); + return new HttpSuccess(); + } + + /** + * API แก้ไขรายการ body เชื่อมโยงคำสั่ง + * + * @summary ORG_058 - CRUD เชื่อมโยงคำสั่ง (ADMIN) #62 + * + * @param {string} id Id เชื่อมโยงคำสั่ง + */ + @Put("{id}") + async Put( + @Path() id: string, + @Body() + requestBody: UpdateCommandCode, + @Request() request: RequestWithUser, + ) { + const _commandCode = await this.commandCodeRepository.findOne({ where: { id: id } }); + if (!_commandCode) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงคำสั่งนี้"); + } + const checkName = await this.commandCodeRepository.findOne({ + where: { id: Not(id), name: requestBody.name }, + }); + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + const before = structuredClone(_commandCode); + _commandCode.lastUpdateUserId = request.user.sub; + _commandCode.lastUpdateFullName = request.user.name; + _commandCode.lastUpdatedAt = new Date(); + this.commandCodeRepository.merge(_commandCode, requestBody); + await this.commandCodeRepository.save(_commandCode, { data: request }); + setLogDataDiff(request, { before, after: _commandCode }); + return new HttpSuccess(); + } + + /** + * API ลบรายการเชื่อมโยงคำสั่ง + * + * @summary ORG_058 - CRUD เชื่อมโยงคำสั่ง (ADMIN) #62 + * + * @param {string} id Id เชื่อมโยงคำสั่ง + */ + @Delete("{id}") + async Delete(@Path() id: string, @Request() request: RequestWithUser) { + const _delCommandCode = await this.commandCodeRepository.findOne({ + where: { id: id }, + }); + if (!_delCommandCode) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเชื่อมโยงคำสั่งนี้"); + } + await this.commandCodeRepository.remove(_delCommandCode, { data: request }); + return new HttpSuccess(); + } +} diff --git a/src/entities/CommandCode.ts b/src/entities/CommandCode.ts new file mode 100644 index 00000000..3372fc8c --- /dev/null +++ b/src/entities/CommandCode.ts @@ -0,0 +1,30 @@ +import { Entity, Column } from "typeorm"; +import { EntityBase } from "./base/Base"; + +@Entity("commandCode") +export class CommandCode extends EntityBase { + @Column({ + nullable: true, + comment: "คำสั่ง", + length: 255, + default: null, + }) + name: string; + + @Column({ + nullable: true, + comment: "HRMS Id", + default: null, + }) + code: number; +} + +export class CreateCommandCode { + @Column() + name: string; + + @Column() + code: number; +} + +export type UpdateCommandCode = Partial;