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: { id: "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(); } }