282 lines
8.5 KiB
TypeScript
282 lines
8.5 KiB
TypeScript
|
|
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 { Command } from "../entities/Command";
|
||
|
|
import { Brackets } from "typeorm";
|
||
|
|
import { CommandType } from "../entities/CommandType";
|
||
|
|
|
||
|
|
@Route("api/v1/org/command")
|
||
|
|
@Tags("Command")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class CommandController extends Controller {
|
||
|
|
private commandRepository = AppDataSource.getRepository(Command);
|
||
|
|
private commandTypeRepository = AppDataSource.getRepository(CommandType);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API list รายการคำสั่ง
|
||
|
|
*
|
||
|
|
* @summary API list รายการคำสั่ง
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get("list")
|
||
|
|
async GetResult(
|
||
|
|
@Query("page") page: number = 1,
|
||
|
|
@Query("pageSize") pageSize: number = 10,
|
||
|
|
@Query() keyword: string = "",
|
||
|
|
@Query() commandTypeId?: string | null,
|
||
|
|
@Query() year?: number,
|
||
|
|
@Query() status?: string | null,
|
||
|
|
) {
|
||
|
|
const [commands, total] = await this.commandRepository
|
||
|
|
.createQueryBuilder("command")
|
||
|
|
.andWhere(
|
||
|
|
new Brackets((qb) => {
|
||
|
|
qb.where(keyword != null && keyword != "" ? "command.commandNo LIKE :keyword" : "1=1", {
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
}).orWhere(
|
||
|
|
keyword != null && keyword != "" ? "command.createdFullName LIKE :keyword" : "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.andWhere(
|
||
|
|
status != null && status != undefined && status != ""
|
||
|
|
? "command.status LIKE :status"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
status:
|
||
|
|
status == null || status == undefined || status == ""
|
||
|
|
? null
|
||
|
|
: `${status.trim().toLocaleUpperCase()}`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.andWhere(year != null && year != undefined && year != 0 ? "command.year = :year" : "1=1", {
|
||
|
|
year: year == null || year == undefined || year == 0 ? null : `${year}`,
|
||
|
|
})
|
||
|
|
.andWhere(
|
||
|
|
commandTypeId != null && commandTypeId != undefined && commandTypeId != ""
|
||
|
|
? "command.commandTypeId = :commandTypeId"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
commandTypeId:
|
||
|
|
commandTypeId == null || commandTypeId == undefined || commandTypeId == ""
|
||
|
|
? null
|
||
|
|
: `${commandTypeId}`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.take(pageSize)
|
||
|
|
.getManyAndCount();
|
||
|
|
|
||
|
|
const data = commands.map((_data) => ({
|
||
|
|
id: _data.id,
|
||
|
|
commandNo: _data.commandNo,
|
||
|
|
commandYear: _data.commandYear,
|
||
|
|
commandAffectDate: _data.commandAffectDate,
|
||
|
|
commandExcecuteDate: _data.commandExcecuteDate,
|
||
|
|
assignFullName: _data.createdFullName,
|
||
|
|
createdFullName: _data.createdFullName,
|
||
|
|
status: _data.status,
|
||
|
|
}));
|
||
|
|
|
||
|
|
return new HttpSuccess({ data, total });
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สร้างรายการ body คำสั่ง
|
||
|
|
*
|
||
|
|
* @summary API สร้างรายการ body คำสั่ง
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Post()
|
||
|
|
async Post(
|
||
|
|
@Body()
|
||
|
|
requestBody: {
|
||
|
|
commandTypeId: string;
|
||
|
|
commandNo: string | null;
|
||
|
|
commandYear: number | null;
|
||
|
|
},
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const command = Object.assign(new Command(), requestBody);
|
||
|
|
|
||
|
|
const commandType = await this.commandTypeRepository.findOne({
|
||
|
|
where: { id: requestBody.commandTypeId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!commandType) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
|
||
|
|
}
|
||
|
|
command.status = "DRAFT";
|
||
|
|
command.createdUserId = request.user.sub;
|
||
|
|
command.createdFullName = request.user.name;
|
||
|
|
command.createdAt = new Date();
|
||
|
|
command.lastUpdateUserId = request.user.sub;
|
||
|
|
command.lastUpdateFullName = request.user.name;
|
||
|
|
command.lastUpdatedAt = new Date();
|
||
|
|
await this.commandRepository.save(command);
|
||
|
|
return new HttpSuccess(command.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API รายละเอียดรายการคำสั่ง tab1
|
||
|
|
*
|
||
|
|
* @summary API รายละเอียดรายการคำสั่ง tab1
|
||
|
|
*
|
||
|
|
* @param {string} id Id คำสั่ง
|
||
|
|
*/
|
||
|
|
@Get("tab1/{id}")
|
||
|
|
async GetByIdTab1(@Path() id: string) {
|
||
|
|
const command = await this.commandRepository.findOne({
|
||
|
|
where: { id },
|
||
|
|
select: [
|
||
|
|
"id",
|
||
|
|
"status",
|
||
|
|
"commandNo",
|
||
|
|
"commandYear",
|
||
|
|
"issue",
|
||
|
|
"detailHeader",
|
||
|
|
"detailBody",
|
||
|
|
"detailFooter",
|
||
|
|
],
|
||
|
|
});
|
||
|
|
if (!command) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
||
|
|
}
|
||
|
|
return new HttpSuccess(command);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API แก้ไขรายการ body คำสั่ง Tab1
|
||
|
|
*
|
||
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab1
|
||
|
|
*
|
||
|
|
* @param {string} id Id คำสั่ง
|
||
|
|
*/
|
||
|
|
@Put("tab1/{id}")
|
||
|
|
async PutTab1(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body()
|
||
|
|
requestBody: {
|
||
|
|
commandNo: string | null;
|
||
|
|
commandYear: number | null;
|
||
|
|
issue: string | null;
|
||
|
|
detailHeader: string | null;
|
||
|
|
detailBody: string | null;
|
||
|
|
detailFooter: string | null;
|
||
|
|
},
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
||
|
|
if (!command) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
||
|
|
}
|
||
|
|
const data = new Command();
|
||
|
|
Object.assign(data, { ...command, ...requestBody });
|
||
|
|
data.lastUpdateUserId = request.user.sub;
|
||
|
|
data.lastUpdateFullName = request.user.name;
|
||
|
|
data.lastUpdatedAt = new Date();
|
||
|
|
await this.commandRepository.save(data);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API คัดลอก
|
||
|
|
*
|
||
|
|
* @summary API คัดลอก
|
||
|
|
*
|
||
|
|
* @param {string} id Id คำสั่ง
|
||
|
|
*/
|
||
|
|
@Put("copy/{id}")
|
||
|
|
async PutCopy(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body()
|
||
|
|
requestBody: { commandNo?: string | null; commandYear?: string | null },
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
||
|
|
if (!command) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
||
|
|
}
|
||
|
|
const copy = new Command();
|
||
|
|
Object.assign(copy, { ...command, ...requestBody, id: undefined });
|
||
|
|
copy.createdUserId = request.user.sub;
|
||
|
|
copy.createdFullName = request.user.name;
|
||
|
|
copy.createdAt = new Date();
|
||
|
|
copy.lastUpdateUserId = request.user.sub;
|
||
|
|
copy.lastUpdateFullName = request.user.name;
|
||
|
|
copy.lastUpdatedAt = new Date();
|
||
|
|
await this.commandRepository.save(copy);
|
||
|
|
return new HttpSuccess(copy.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ยกเลิก
|
||
|
|
*
|
||
|
|
* @summary API ยกเลิก
|
||
|
|
*
|
||
|
|
* @param {string} id Id คำสั่ง
|
||
|
|
*/
|
||
|
|
@Put("cancel/{id}")
|
||
|
|
async PutCancel(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body()
|
||
|
|
requestBody: { reason?: string | null },
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
||
|
|
if (!command) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
||
|
|
}
|
||
|
|
command.status = "CANCEL";
|
||
|
|
command.lastUpdateUserId = request.user.sub;
|
||
|
|
command.lastUpdateFullName = request.user.name;
|
||
|
|
command.lastUpdatedAt = new Date();
|
||
|
|
await this.commandRepository.save(command);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ลบรายการคำสั่ง
|
||
|
|
*
|
||
|
|
* @summary API ลบรายการคำสั่ง
|
||
|
|
*
|
||
|
|
* @param {string} id Id คำสั่ง
|
||
|
|
*/
|
||
|
|
@Delete("{id}")
|
||
|
|
async Delete(@Path() id: string) {
|
||
|
|
const command = await this.commandRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!command) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
||
|
|
}
|
||
|
|
await this.commandRepository.delete(command.id);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
}
|