Template สำหรับลงในตำแหน่ง/เงินเดือน

This commit is contained in:
kittapath 2024-09-16 10:06:18 +07:00
parent 40440ca250
commit 9d755fc8ff
5 changed files with 285 additions and 18 deletions

View file

@ -0,0 +1,219 @@
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
} 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 { CommandSalary, CreateCommandSalary, UpdateCommandSalary } from "../entities/CommandSalary";
import { Not } from "typeorm";
import { CommandSys } from "../entities/CommandSys";
@Route("api/v1/org/commandSalary")
@Tags("CommandSalary")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class CommandSalaryController extends Controller {
private commandSalaryRepository = AppDataSource.getRepository(CommandSalary);
private commandSysRepository = AppDataSource.getRepository(CommandSys);
/**
* API list
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
*/
@Get("list")
async Get() {
const _commandSalary = await this.commandSalaryRepository.find({
where: { isActive: true },
select: [
"id",
"name",
"commandSysId",
"createdAt",
"lastUpdatedAt",
"createdFullName",
"lastUpdateFullName",
],
order: { name: "ASC" },
});
return new HttpSuccess(_commandSalary);
}
/**
* API list
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
*/
@Get("admin")
async GetAdmin(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query() commandSysId?: string | null,
@Query() isActive?: boolean | null,
) {
const [commandSalarys, total] = await this.commandSalaryRepository
.createQueryBuilder("commandSalary")
.andWhere(
isActive != null && isActive != undefined ? "commandSalary.isActive = :isActive" : "1=1",
{
isActive:
isActive == null || isActive == undefined ? null : `${isActive == true ? 1 : 0}`,
},
)
.andWhere(
commandSysId != null && commandSysId != undefined && commandSysId != ""
? "commandSalary.commandSysId = :commandSysId"
: "1=1",
{
commandSysId:
commandSysId == null || commandSysId == undefined || commandSysId == ""
? null
: `${commandSysId}`,
},
)
.orderBy("commandSalary.name", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
return new HttpSuccess({ commandSalarys, total });
}
/**
* API
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
*/
@Get("{id}")
async GetById(@Path() id: string) {
const _commandSalary = await this.commandSalaryRepository.findOne({
where: { id },
select: ["id", "name", "commandSysId", "isActive"],
});
if (!_commandSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(_commandSalary);
}
/**
* API body
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
*/
@Post()
async Post(
@Body()
requestBody: CreateCommandSalary,
@Request() request: { user: Record<string, any> },
) {
const _commandSalary = Object.assign(new CommandSalary(), requestBody);
// const checkName = await this.commandSalaryRepository.findOne({
// where: { name: requestBody.name },
// });
// if (checkName) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อ Template นี้มีอยู่ในระบบแล้ว");
// }
const checkNameSys = await this.commandSysRepository.findOne({
where: { id: requestBody.commandSysId },
});
if (!checkNameSys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทนี้มีอยู่ในระบบ");
}
_commandSalary.createdUserId = request.user.sub;
_commandSalary.createdFullName = request.user.name;
_commandSalary.lastUpdateUserId = request.user.sub;
_commandSalary.lastUpdateFullName = request.user.name;
_commandSalary.createdAt = new Date();
_commandSalary.lastUpdatedAt = new Date();
await this.commandSalaryRepository.save(_commandSalary);
return new HttpSuccess(_commandSalary.id);
}
/**
* API body
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
*/
@Put("{id}")
async Put(
@Path() id: string,
@Body()
requestBody: UpdateCommandSalary,
@Request() request: { user: Record<string, any> },
) {
const _commandSalary = await this.commandSalaryRepository.findOne({ where: { id: id } });
if (!_commandSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล Template นี้");
}
// const checkName = await this.commandSalaryRepository.findOne({
// where: { id: Not(id), name: requestBody.name },
// });
// if (checkName) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
// }
const checkNameSys = await this.commandSysRepository.findOne({
where: { id: requestBody.commandSysId },
});
if (!checkNameSys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทนี้มีอยู่ในระบบ");
}
_commandSalary.lastUpdateUserId = request.user.sub;
_commandSalary.lastUpdateFullName = request.user.name;
_commandSalary.lastUpdatedAt = new Date();
this.commandSalaryRepository.merge(_commandSalary, requestBody);
await this.commandSalaryRepository.save(_commandSalary);
return new HttpSuccess(_commandSalary.id);
}
/**
* API
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
*/
@Delete("{id}")
async Delete(@Path() id: string) {
const _delCommandSalary = await this.commandSalaryRepository.findOne({
where: { id: id },
});
if (!_delCommandSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล Template นี้");
}
await this.commandSalaryRepository.delete(_delCommandSalary.id);
return new HttpSuccess();
}
}

View file

@ -1,21 +1,4 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { Controller, Get, Route, Security, Tags, SuccessResponse, Response } from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";

View file

@ -0,0 +1,45 @@
import { Entity, Column, JoinColumn, ManyToOne } from "typeorm";
import { EntityBase } from "./base/Base";
import { CommandSys } from "./CommandSys";
@Entity("commandSalary")
export class CommandSalary extends EntityBase {
@Column({
nullable: true,
comment: "ชื่อคำสั่ง",
length: 255,
default: null,
})
name: string;
@Column({
comment: "สถานะการใช้งาน",
default: true,
})
isActive: boolean;
@Column({
nullable: true,
length: 255,
comment: "คีย์นอก(FK)ของตาราง CommandSys",
default: null,
})
commandSysId: string;
@ManyToOne(() => CommandSys, (commandSys) => commandSys.commandSalarys)
@JoinColumn({ name: "commandSysId" })
commandSalarySys: CommandSys;
}
export class CreateCommandSalary {
@Column()
name: string;
@Column()
isActive: boolean;
@Column()
commandSysId: string;
}
export type UpdateCommandSalary = Partial<CreateCommandSalary>;

View file

@ -7,6 +7,7 @@ import {
OneToMany,
} from "typeorm";
import { CommandType } from "./CommandType";
import { CommandSalary } from "./CommandSalary";
@Entity("commandSys")
export class CommandSys {
@ -60,6 +61,9 @@ export class CommandSys {
@OneToMany(() => CommandType, (commandType) => commandType.commandTypeSys)
commandTypes: CommandType[];
@OneToMany(() => CommandSalary, (commandSalary) => commandSalary.commandSalarySys)
commandSalarys: CommandSalary[];
}
export class CreateCommandSys {

View file

@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddCommandSalary1726455828396 implements MigrationInterface {
name = 'AddCommandSalary1726455828396'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`commandSalary\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`name\` varchar(255) NULL COMMENT 'ชื่อคำสั่ง', \`isActive\` tinyint NOT NULL COMMENT 'สถานะการใช้งาน' DEFAULT 1, \`commandSysId\` varchar(255) NULL COMMENT 'คีย์นอก(FK)ของตาราง CommandSys', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`commandSalary\` ADD CONSTRAINT \`FK_52519043506061e558b992ba0dd\` FOREIGN KEY (\`commandSysId\`) REFERENCES \`commandSys\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`commandSalary\` DROP FOREIGN KEY \`FK_52519043506061e558b992ba0dd\``);
await queryRunner.query(`DROP TABLE \`commandSalary\``);
}
}