1178 lines
39 KiB
TypeScript
1178 lines
39 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, LessThan, MoreThan, Double, In } from "typeorm";
|
|
import { CommandType } from "../entities/CommandType";
|
|
import { CommandSend } from "../entities/CommandSend";
|
|
import { Profile } from "../entities/Profile";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { OrgRevision } from "../entities/OrgRevision";
|
|
import { CommandSendCC } from "../entities/CommandSendCC";
|
|
import { CommandSalary } from "../entities/CommandSalary";
|
|
import { CommandRecive } from "../entities/CommandRecive";
|
|
import HttpStatus from "../interfaces/http-status";
|
|
import Extension from "../interfaces/extension";
|
|
import { ProfileEmployee } from "../entities/ProfileEmployee";
|
|
import CallAPI from "../interfaces/call-api";
|
|
|
|
@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);
|
|
private commandSendRepository = AppDataSource.getRepository(CommandSend);
|
|
private commandSendCCRepository = AppDataSource.getRepository(CommandSendCC);
|
|
private commandSalaryRepository = AppDataSource.getRepository(CommandSalary);
|
|
private commandReciveRepository = AppDataSource.getRepository(CommandRecive);
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
|
private profileEmployeeRepository = AppDataSource.getRepository(ProfileEmployee);
|
|
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
|
|
|
|
/**
|
|
* 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 IN (:...status)"
|
|
: "1=1",
|
|
{
|
|
status:
|
|
status == null || status == undefined || status == ""
|
|
? null
|
|
: status.trim().toLocaleUpperCase() == "NEW" ||
|
|
status.trim().toLocaleUpperCase() == "DRAFT"
|
|
? ["NEW", "DRAFT"]
|
|
: [status.trim().toLocaleUpperCase()],
|
|
},
|
|
)
|
|
.andWhere(
|
|
year != null && year != undefined && year != 0
|
|
? "command.commandYear = :commandYear"
|
|
: "1=1",
|
|
{
|
|
commandYear: 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}`,
|
|
},
|
|
)
|
|
.orderBy("command.createdAt", "DESC")
|
|
.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,
|
|
issue: _data.issue,
|
|
}));
|
|
|
|
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: RequestWithUser,
|
|
) {
|
|
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.detailHeader = commandType.detailHeader;
|
|
command.detailBody = commandType.detailBody;
|
|
command.detailFooter = commandType.detailFooter;
|
|
command.isAttachment = commandType.isAttachment;
|
|
command.status = "NEW";
|
|
command.issue = commandType.name;
|
|
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 },
|
|
relations: ["commandType", "commandType.commandTypeSys"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
const _command = {
|
|
id: command.id,
|
|
status: command.status,
|
|
commandNo: command.commandNo,
|
|
commandYear: command.commandYear,
|
|
issue: command.issue,
|
|
detailHeader: command.detailHeader,
|
|
detailBody: command.detailBody,
|
|
detailFooter: command.detailFooter,
|
|
isBangkok: command.isBangkok,
|
|
isAttachment: command.isAttachment,
|
|
commandAffectDate: command.commandAffectDate,
|
|
commandExcecuteDate: command.commandExcecuteDate,
|
|
commandTypeName: command.commandType?.name || null,
|
|
commandSysId: command.commandType?.commandSysId || null,
|
|
};
|
|
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;
|
|
commandAffectDate: Date | null;
|
|
commandExcecuteDate: Date | null;
|
|
isBangkok: boolean | null;
|
|
isAttachment: boolean | null;
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
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 รายละเอียดรายการคำสั่ง tab2
|
|
*
|
|
* @summary API รายละเอียดรายการคำสั่ง tab2
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Get("tab2/{id}")
|
|
async GetByIdTab2(@Path() id: string) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id },
|
|
relations: ["commandSalary", "commandRecives"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
const _command = {
|
|
id: command.id,
|
|
commandSalaryId: command.commandSalaryId,
|
|
commandSalary: command.commandSalary?.name || null,
|
|
positionDetail: command.positionDetail,
|
|
commandRecives: command.commandRecives
|
|
.sort((a, b) => a.order - b.order)
|
|
.map((x) => ({
|
|
id: x.id,
|
|
citizenId: x.citizenId,
|
|
prefix: x.prefix,
|
|
firstName: x.firstName,
|
|
lastName: x.lastName,
|
|
// profileId: x.profileId,
|
|
order: x.order,
|
|
remarkVertical: x.remarkVertical,
|
|
remarkHorizontal: x.remarkHorizontal,
|
|
amount: x.amount,
|
|
positionSalaryAmount: x.positionSalaryAmount,
|
|
mouthSalaryAmount: x.mouthSalaryAmount,
|
|
})),
|
|
};
|
|
return new HttpSuccess(_command);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body คำสั่ง Tab2
|
|
*
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab2
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("tab2/{id}")
|
|
async PutTab2(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: {
|
|
positionDetail: string | null;
|
|
commandSalaryId: string | null;
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
if (requestBody.commandSalaryId != undefined && requestBody.commandSalaryId != null) {
|
|
const commandSalary = await this.commandSalaryRepository.findOne({
|
|
where: { id: requestBody.commandSalaryId },
|
|
});
|
|
if (!commandSalary) {
|
|
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();
|
|
}
|
|
|
|
@Get("tab2/swap/{direction}/{commandReciveId}")
|
|
public async swapSalary(
|
|
@Path() direction: string,
|
|
commandReciveId: string,
|
|
@Request() req: RequestWithUser,
|
|
) {
|
|
const source_item = await this.commandReciveRepository.findOne({
|
|
where: { id: commandReciveId },
|
|
});
|
|
if (source_item == null) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
const sourceOrder = source_item.order;
|
|
if (direction.trim().toUpperCase() == "UP") {
|
|
const dest_item = await this.commandReciveRepository.findOne({
|
|
where: { commandId: source_item.commandId, order: LessThan(sourceOrder) },
|
|
order: { order: "DESC" },
|
|
});
|
|
if (dest_item == null) return new HttpSuccess();
|
|
var destOrder = dest_item.order;
|
|
dest_item.order = sourceOrder;
|
|
source_item.order = destOrder;
|
|
await Promise.all([
|
|
this.commandReciveRepository.save(source_item),
|
|
this.commandReciveRepository.save(dest_item),
|
|
]);
|
|
} else {
|
|
const dest_item = await this.commandReciveRepository.findOne({
|
|
where: { commandId: source_item.commandId, order: MoreThan(sourceOrder) },
|
|
order: { order: "ASC" },
|
|
});
|
|
if (dest_item == null) return new HttpSuccess();
|
|
var destOrder = dest_item.order;
|
|
dest_item.order = sourceOrder;
|
|
source_item.order = destOrder;
|
|
await Promise.all([
|
|
this.commandReciveRepository.save(source_item),
|
|
this.commandReciveRepository.save(dest_item),
|
|
]);
|
|
}
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body คำสั่ง Tab2
|
|
*
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab2
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("tab2/recive/{commandReciveId}")
|
|
async PutTab2Recive(
|
|
@Path() commandReciveId: string,
|
|
@Body()
|
|
requestBody: {
|
|
remarkVertical: string | null;
|
|
remarkHorizontal: string | null;
|
|
amount: Double | null;
|
|
positionSalaryAmount: Double | null;
|
|
mouthSalaryAmount: Double | null;
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const commandRecive = await this.commandReciveRepository.findOne({
|
|
where: { id: commandReciveId },
|
|
});
|
|
if (!commandRecive) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับคำสั่ง");
|
|
}
|
|
const data = new CommandRecive();
|
|
Object.assign(data, { ...commandRecive, ...requestBody });
|
|
data.lastUpdateUserId = request.user.sub;
|
|
data.lastUpdateFullName = request.user.name;
|
|
data.lastUpdatedAt = new Date();
|
|
await this.commandReciveRepository.save(data);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API ลบรายการผู้ได้รับคำสั่ง
|
|
*
|
|
* @summary API ลบรายการผู้ได้รับคำสั่ง
|
|
*
|
|
* @param {string} id Id ผู้ได้รับคำสั่ง
|
|
*/
|
|
@Delete("tab2/{commandReciveId}")
|
|
async DeleteTab2(@Path() commandReciveId: string) {
|
|
const commandRecive = await this.commandReciveRepository.findOne({
|
|
where: { id: commandReciveId },
|
|
});
|
|
if (!commandRecive) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับคำสั่ง");
|
|
}
|
|
const commandId = commandRecive.commandId;
|
|
await this.commandReciveRepository.delete(commandRecive.id);
|
|
|
|
const commandReciveList = await this.commandReciveRepository.find({
|
|
where: {
|
|
commandId: commandId,
|
|
},
|
|
order: { order: "ASC" },
|
|
});
|
|
commandReciveList.map(async (p, i) => {
|
|
p.order = i + 1;
|
|
await this.commandReciveRepository.save(p);
|
|
});
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการคำสั่ง tab3
|
|
*
|
|
* @summary API รายละเอียดรายการคำสั่ง tab3
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Get("tab3/{id}")
|
|
async GetByIdTab3(@Path() id: string) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id },
|
|
relations: ["commandSends", "commandSends.commandSendCCs"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
const _command = command.commandSends.map((item) => ({
|
|
id: item.id,
|
|
citizenId: item.citizenId,
|
|
prefix: item.prefix,
|
|
firstName: item.firstName,
|
|
lastName: item.lastName,
|
|
position: item.position,
|
|
org: item.org,
|
|
sendCC: item.commandSendCCs.map((x) => x.name),
|
|
profileId: item.profileId,
|
|
}));
|
|
return new HttpSuccess(_command);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("tab3-add/{id}")
|
|
async PutTab3Add(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: {
|
|
profileId: string[];
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
let _null: any = null;
|
|
|
|
await Promise.all(
|
|
requestBody.profileId.map(async (item) => {
|
|
const commandSendCheck = await this.commandSendRepository.findOne({
|
|
where: { profileId: item, commandId: command.id },
|
|
});
|
|
if (commandSendCheck) return;
|
|
|
|
let profile = await this.profileRepository.findOne({
|
|
where: { id: item },
|
|
relations: ["current_holders", "current_holders.orgRoot"],
|
|
});
|
|
if (!profile) return;
|
|
|
|
const findRevision = await this.orgRevisionRepo.findOne({
|
|
where: { orgRevisionIsCurrent: true },
|
|
});
|
|
|
|
const commandSend = new CommandSend();
|
|
commandSend.citizenId = profile.citizenId;
|
|
commandSend.prefix =
|
|
profile.rank != null && profile.rank != "" ? profile.rank : profile.prefix;
|
|
commandSend.firstName = profile.firstName;
|
|
commandSend.lastName = profile.lastName;
|
|
commandSend.position = profile.position;
|
|
commandSend.org =
|
|
profile.current_holders?.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot
|
|
?.orgRootName || _null;
|
|
commandSend.profileId = profile.id;
|
|
commandSend.commandId = command.id;
|
|
commandSend.createdUserId = request.user.sub;
|
|
commandSend.createdFullName = request.user.name;
|
|
commandSend.createdAt = new Date();
|
|
commandSend.lastUpdateUserId = request.user.sub;
|
|
commandSend.lastUpdateFullName = request.user.name;
|
|
commandSend.lastUpdatedAt = new Date();
|
|
await this.commandSendRepository.save(commandSend);
|
|
}),
|
|
);
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("tab3/{id}")
|
|
async PutTab3Update(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: {
|
|
commandSend: {
|
|
id: string;
|
|
sendCC: string[];
|
|
}[];
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
await Promise.all(
|
|
requestBody.commandSend.map(async (item) => {
|
|
const commandSendCC = await this.commandSendCCRepository.find({
|
|
where: { commandSendId: item.id },
|
|
});
|
|
await this.commandSendCCRepository.remove(commandSendCC);
|
|
|
|
await Promise.all(
|
|
item.sendCC.map(async (item1) => {
|
|
const _commandSendCC = new CommandSendCC();
|
|
_commandSendCC.name = item1;
|
|
_commandSendCC.commandSendId = item.id;
|
|
_commandSendCC.createdUserId = request.user.sub;
|
|
_commandSendCC.createdFullName = request.user.name;
|
|
_commandSendCC.createdAt = new Date();
|
|
_commandSendCC.lastUpdateUserId = request.user.sub;
|
|
_commandSendCC.lastUpdateFullName = request.user.name;
|
|
_commandSendCC.lastUpdatedAt = new Date();
|
|
await this.commandSendCCRepository.save(_commandSendCC);
|
|
}),
|
|
);
|
|
}),
|
|
);
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @summary API แก้ไขรายการ body คำสั่ง Tab3
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Delete("tab3/{commandSendId}")
|
|
async DeleteTab3Update(
|
|
@Path() commandSendId: string,
|
|
@Body()
|
|
requestBody: { reason?: string | null },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandSendRepository.findOne({ where: { id: commandSendId } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับสำเนาคำสั่ง");
|
|
}
|
|
|
|
await this.commandSendCCRepository.delete({ commandSendId: commandSendId });
|
|
await this.commandSendRepository.delete(commandSendId);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API คัดลอก
|
|
*
|
|
* @summary API คัดลอก
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("copy/{id}")
|
|
async PutCopy(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { commandNo?: string | null; commandYear?: number | null },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
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 });
|
|
const _null: any = null;
|
|
copy.status = "NEW";
|
|
copy.commandAffectDate = _null;
|
|
copy.commandExcecuteDate = _null;
|
|
copy.isSignature = _null;
|
|
copy.isDraft = _null;
|
|
copy.isSign = _null;
|
|
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: RequestWithUser,
|
|
) {
|
|
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 คำสั่ง
|
|
*/
|
|
@Put("resume/{id}")
|
|
async PutDraft(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { reason?: string | null },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
command.status = "DRAFT";
|
|
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, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
const commandSend = await this.commandSendRepository.find({
|
|
where: { commandId: id },
|
|
});
|
|
await this.commandSendCCRepository.delete({ commandSendId: In(commandSend.map((x) => x.id)) });
|
|
await this.commandReciveRepository.delete({ commandId: command.id });
|
|
await this.commandSendRepository.delete({ commandId: command.id });
|
|
await this.commandRepository.delete(command.id);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการคำสั่ง tab0
|
|
*
|
|
* @summary API รายละเอียดรายการคำสั่ง tab0
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Get("tab0/{id}")
|
|
async GetByIdTab0(@Path() id: string) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id },
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
const _command = {
|
|
id: command.id,
|
|
isSignature: command.isSignature,
|
|
status: command.status,
|
|
isDraft: command.isDraft,
|
|
isSign: command.isSign,
|
|
isAttachment: command.isAttachment,
|
|
};
|
|
return new HttpSuccess(_command);
|
|
}
|
|
|
|
/**
|
|
* API ทำคำสั่งใหม่
|
|
*
|
|
* @summary API ทำคำสั่งใหม่
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Put("sign/{id}")
|
|
async PutSelectSign(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { sign: boolean },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
command.isSignature = requestBody.sign;
|
|
command.status = "DRAFT";
|
|
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 คำสั่ง
|
|
*/
|
|
@Put("draft/{id}")
|
|
async PutSelectDraft(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { sign: boolean },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
command.isDraft = requestBody.sign;
|
|
command.status = "PENDING";
|
|
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 คำสั่ง
|
|
*/
|
|
@Put("pending-check/{id}")
|
|
async PutSelectPending_Check(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { sign?: boolean },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({ where: { id: id } });
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
command.isSign = true;
|
|
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 คำสั่ง
|
|
*/
|
|
@Put("pending/{id}")
|
|
async PutSelectPending(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: { sign?: boolean },
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id: id },
|
|
relations: ["commandType", "commandRecives"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
command.isSign = true;
|
|
if (command.commandExcecuteDate == null)
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบวันที่คำสั่งมีผล");
|
|
if (
|
|
new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()) <
|
|
new Date(
|
|
command.commandExcecuteDate.getFullYear(),
|
|
command.commandExcecuteDate.getMonth(),
|
|
command.commandExcecuteDate.getDate(),
|
|
)
|
|
) {
|
|
command.status = "WAITING";
|
|
} else {
|
|
const path = this.commandTypePath(command.commandType.code);
|
|
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
|
|
|
|
new CallAPI()
|
|
.PostData(request, path + "excecute", {
|
|
refIds: command.commandRecives
|
|
.filter((x) => x.refId != null)
|
|
.map((x) => ({
|
|
refId: x.refId,
|
|
commandAffectDate: command.commandAffectDate,
|
|
commandNo: command.commandNo,
|
|
commandYear: command.commandYear,
|
|
templateDoc: command.positionDetail,
|
|
amount: x.amount,
|
|
positionSalaryAmount: x.positionSalaryAmount,
|
|
mouthSalaryAmount: x.mouthSalaryAmount,
|
|
})),
|
|
})
|
|
.then(async (res) => {})
|
|
.catch((e) => {
|
|
command.status = "REPORTED";
|
|
});
|
|
}
|
|
command.lastUpdateUserId = request.user.sub;
|
|
command.lastUpdateFullName = request.user.name;
|
|
command.lastUpdatedAt = new Date();
|
|
await this.commandRepository.save(command);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการคำสั่ง tab4 คำสั่ง
|
|
*
|
|
* @summary API รายละเอียดรายการคำสั่ง tab4 คำสั่ง
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Get("tab4/cover/{id}")
|
|
async GetByIdTab4Cover(@Path() id: string, @Request() request: RequestWithUser) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id },
|
|
relations: ["commandType"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
const _command = {
|
|
issue: "...................................",
|
|
commandNo: command.commandNo,
|
|
commandYear: command.commandYear,
|
|
commandTitle: command.issue,
|
|
detailHeader: command.detailHeader,
|
|
detailBody: command.detailBody,
|
|
detailFooter: command.detailFooter,
|
|
commandDate:
|
|
command.commandAffectDate == null
|
|
? ""
|
|
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)),
|
|
commandExcecuteDate:
|
|
command.commandExcecuteDate == null
|
|
? ""
|
|
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandExcecuteDate)),
|
|
name: "...................................",
|
|
position: "...................................",
|
|
};
|
|
return new HttpSuccess({
|
|
template: command.commandType.fileCover,
|
|
reportName: "docx-report",
|
|
data: _command,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* API รายละเอียดรายการคำสั่ง tab4 แนบท้าย
|
|
*
|
|
* @summary API รายละเอียดรายการคำสั่ง tab4 แนบท้าย
|
|
*
|
|
* @param {string} id Id คำสั่ง
|
|
*/
|
|
@Get("tab4/attachment/{id}")
|
|
async GetByIdTab4Attachment(@Path() id: string, @Request() request: RequestWithUser) {
|
|
const command = await this.commandRepository.findOne({
|
|
where: { id },
|
|
relations: ["commandType", "commandRecives"],
|
|
});
|
|
if (!command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
|
|
}
|
|
|
|
let _command: any = [];
|
|
const path = this.commandTypePath(command.commandType.code);
|
|
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
|
|
|
|
new CallAPI()
|
|
.PostData(request, path + "attachment", {
|
|
refIds: command.commandRecives.map((x) => x.refId),
|
|
})
|
|
.then(async (res) => {
|
|
console.log(res);
|
|
_command = res;
|
|
})
|
|
.catch(() => {});
|
|
|
|
return new HttpSuccess({
|
|
template: command.commandType.fileAttachment,
|
|
reportName: "xlsx-report",
|
|
data: _command,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* API สร้างรายการ body คำสั่ง
|
|
*
|
|
* @summary API สร้างรายการ body คำสั่ง
|
|
*
|
|
*/
|
|
@Post("person")
|
|
async PostPerson(
|
|
@Body()
|
|
requestBody: {
|
|
commandTypeId?: string | null;
|
|
commandNo?: string | null;
|
|
commandYear?: number | null;
|
|
commandId?: string | null;
|
|
persons: {
|
|
refId: string;
|
|
citizenId: string;
|
|
prefix: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
}[];
|
|
},
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
let command = new Command();
|
|
let commandCode = null;
|
|
if (
|
|
requestBody.commandId != undefined &&
|
|
requestBody.commandId != null &&
|
|
requestBody.commandId != ""
|
|
) {
|
|
const _command = await this.commandRepository.findOne({
|
|
where: { id: requestBody.commandId },
|
|
relations: ["commandRecives", "commandType"],
|
|
order: {
|
|
commandRecives: {
|
|
order: "DESC",
|
|
},
|
|
},
|
|
});
|
|
if (!_command) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบคำสั่งนี้ในระบบ");
|
|
}
|
|
commandCode = _command.commandType.code;
|
|
command = _command;
|
|
} else {
|
|
command = Object.assign(new Command(), requestBody);
|
|
if (!requestBody.commandTypeId) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่ง");
|
|
}
|
|
const commandType = await this.commandTypeRepository.findOne({
|
|
where: { id: requestBody.commandTypeId },
|
|
});
|
|
|
|
if (!commandType) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
|
|
}
|
|
commandCode = commandType.code;
|
|
command.detailHeader = commandType.detailHeader;
|
|
command.detailBody = commandType.detailBody;
|
|
command.detailFooter = commandType.detailFooter;
|
|
command.isAttachment = commandType.isAttachment;
|
|
command.status = "NEW";
|
|
command.issue = commandType.name;
|
|
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);
|
|
}
|
|
|
|
const path = this.commandTypePath(commandCode);
|
|
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
|
|
new CallAPI()
|
|
.PostData(request, path, {
|
|
refIds: requestBody.persons.map((x) => x.refId),
|
|
})
|
|
.then(async (res) => {
|
|
let order =
|
|
command.commandRecives == null || command.commandRecives.length <= 0
|
|
? 0
|
|
: command.commandRecives[0].order;
|
|
await Promise.all(
|
|
requestBody.persons.map(async (item) => {
|
|
const _commandRecive = await this.commandReciveRepository.findOne({
|
|
where: {
|
|
commandId: command.id,
|
|
refId: item.refId,
|
|
},
|
|
});
|
|
if (_commandRecive) return;
|
|
order = order + 1;
|
|
let commandRecive = new CommandRecive();
|
|
commandRecive = Object.assign(new CommandRecive(), item);
|
|
commandRecive.order = order;
|
|
commandRecive.commandId = command.id;
|
|
commandRecive.createdUserId = request.user.sub;
|
|
commandRecive.createdFullName = request.user.name;
|
|
commandRecive.createdAt = new Date();
|
|
commandRecive.lastUpdateUserId = request.user.sub;
|
|
commandRecive.lastUpdateFullName = request.user.name;
|
|
commandRecive.lastUpdatedAt = new Date();
|
|
await this.commandReciveRepository.save(commandRecive);
|
|
}),
|
|
);
|
|
})
|
|
.catch(() => {});
|
|
return new HttpSuccess(command.id);
|
|
}
|
|
|
|
commandTypePath(commandCode: string) {
|
|
switch (commandCode) {
|
|
case "C-PM-01":
|
|
return "/placement/recruit/report/";
|
|
case "C-PM-02":
|
|
return "/placemant/recruit/report/";
|
|
case "C-PM-03":
|
|
return "/placemant/appoint/report/";
|
|
case "C-PM-04":
|
|
return "/placemant/move/report/";
|
|
case "C-PM-05":
|
|
return "/placemant/appointment/appoint/report/";
|
|
case "C-PM-06":
|
|
return "/placemant/appointment/slip/report/";
|
|
case "C-PM-07":
|
|
return "/placemant/appointment/move/report/";
|
|
case "C-PM-08":
|
|
return "/retirement/other/appoint/report/";
|
|
case "C-PM-09":
|
|
return "/retirement/other/out/report/";
|
|
case "C-PM-10":
|
|
return "/xxxxxx/";
|
|
case "C-PM-11":
|
|
return "/order/command11/report/";
|
|
case "C-PM-12":
|
|
return "/order/command12/report/";
|
|
case "C-PM-13":
|
|
return "/xxxxxx/";
|
|
case "C-PM-14":
|
|
return "/xxxxxx/";
|
|
case "C-PM-15":
|
|
return "/xxxxxx/";
|
|
case "C-PM-16":
|
|
return "/xxxxxx/";
|
|
case "C-PM-17":
|
|
return "/xxxxxx/";
|
|
case "C-PM-18":
|
|
return "/xxxxxx/";
|
|
case "C-PM-19":
|
|
return "/xxxxxx/";
|
|
case "C-PM-20":
|
|
return "/xxxxxx/";
|
|
case "C-PM-21":
|
|
return "/xxxxxx/";
|
|
case "C-PM-22":
|
|
return "/xxxxxx/";
|
|
case "C-PM-23":
|
|
return "/xxxxxx/";
|
|
case "C-PM-24":
|
|
return "/xxxxxx/";
|
|
case "C-PM-25":
|
|
return "/xxxxxx/";
|
|
case "C-PM-26":
|
|
return "/xxxxxx/";
|
|
case "C-PM-27":
|
|
return "/xxxxxx/";
|
|
case "C-PM-28":
|
|
return "/xxxxxx/";
|
|
case "C-PM-29":
|
|
return "/xxxxxx/";
|
|
case "C-PM-30":
|
|
return "/xxxxxx/";
|
|
case "C-PM-31":
|
|
return "/xxxxxx/";
|
|
case "C-PM-32":
|
|
return "/xxxxxx/";
|
|
case "C-PM-33":
|
|
return "/xxxxxx/";
|
|
case "C-PM-34":
|
|
return "/xxxxxx/";
|
|
case "C-PM-35":
|
|
return "/xxxxxx/";
|
|
case "C-PM-36":
|
|
return "/xxxxxx/";
|
|
case "C-PM-37":
|
|
return "/xxxxxx/";
|
|
case "C-PM-38":
|
|
return "/xxxxxx/";
|
|
case "C-PM-39":
|
|
return "/placemant/slip/report/";
|
|
case "C-PM-40":
|
|
return "/xxxxxx/";
|
|
case "C-PM-41":
|
|
return "/xxxxxx/";
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|