เพิ่ม commandcode
This commit is contained in:
parent
2b0865ea9f
commit
76a2213cd8
3 changed files with 231 additions and 36 deletions
165
src/controllers/CommandCodeController.ts
Normal file
165
src/controllers/CommandCodeController.ts
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
30
src/entities/CommandCode.ts
Normal file
30
src/entities/CommandCode.ts
Normal file
|
|
@ -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<CreateCommandCode>;
|
||||
Loading…
Add table
Add a link
Reference in a new issue