hrms-api-org/src/controllers/CommandController.ts

1003 lines
33 KiB
TypeScript
Raw Normal View History

2024-09-11 17:29:33 +07:00
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";
2024-09-26 19:46:09 +07:00
import { Brackets, LessThan, MoreThan, Double, In } from "typeorm";
2024-09-11 17:29:33 +07:00
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";
2024-09-13 15:34:01 +07:00
import { CommandSendCC } from "../entities/CommandSendCC";
2024-09-25 11:17:38 +07:00
import { CommandSalary } from "../entities/CommandSalary";
import { CommandRecive } from "../entities/CommandRecive";
import HttpStatus from "../interfaces/http-status";
2024-09-26 16:05:59 +07:00
import Extension from "../interfaces/extension";
2024-09-11 17:29:33 +07:00
@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);
2024-09-13 15:34:01 +07:00
private commandSendCCRepository = AppDataSource.getRepository(CommandSendCC);
2024-09-25 11:17:38 +07:00
private commandSalaryRepository = AppDataSource.getRepository(CommandSalary);
private commandReciveRepository = AppDataSource.getRepository(CommandRecive);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
2024-09-11 17:29:33 +07:00
/**
* 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 != ""
2024-09-24 11:31:08 +07:00
? "command.status IN (:...status)"
2024-09-11 17:29:33 +07:00
: "1=1",
{
status:
status == null || status == undefined || status == ""
? null
2024-09-24 09:45:43 +07:00
: status.trim().toLocaleUpperCase() == "NEW" ||
status.trim().toLocaleUpperCase() == "DRAFT"
? ["NEW", "DRAFT"]
: [status.trim().toLocaleUpperCase()],
2024-09-11 17:29:33 +07:00
},
)
.andWhere(
year != null && year != undefined && year != 0
? "command.commandYear = :commandYear"
: "1=1",
{
commandYear: year == null || year == undefined || year == 0 ? null : `${year}`,
},
)
2024-09-11 17:29:33 +07:00
.andWhere(
commandTypeId != null && commandTypeId != undefined && commandTypeId != ""
? "command.commandTypeId = :commandTypeId"
: "1=1",
{
commandTypeId:
commandTypeId == null || commandTypeId == undefined || commandTypeId == ""
? null
: `${commandTypeId}`,
},
)
2024-09-26 19:46:09 +07:00
.orderBy("command.createdAt", "DESC")
2024-09-11 17:29:33 +07:00
.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,
2024-09-25 16:17:06 +07:00
issue: _data.issue,
2024-09-11 17:29:33 +07:00
}));
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,
2024-09-11 17:29:33 +07:00
) {
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;
2024-09-24 09:45:43 +07:00
command.status = "NEW";
command.issue = commandType.name;
2024-09-11 17:29:33 +07:00
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"],
2024-09-11 17:29:33 +07:00
});
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,
2024-09-25 21:05:16 +07:00
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);
2024-09-11 17:29:33 +07:00
}
/**
* 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;
2024-09-25 21:05:16 +07:00
isBangkok: boolean | null;
2024-09-26 17:25:33 +07:00
isAttachment: boolean | null;
2024-09-11 17:29:33 +07:00
},
@Request() request: RequestWithUser,
2024-09-11 17:29:33 +07:00
) {
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();
}
2024-09-24 16:42:24 +07:00
/**
* 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,
2024-09-25 16:17:06 +07:00
commandRecives: command.commandRecives
2024-09-24 16:42:24 +07:00
.sort((a, b) => a.order - b.order)
.map((x) => ({
2024-09-25 16:17:06 +07:00
id: x.id,
2024-09-24 16:42:24 +07:00
citizenId: x.citizenId,
prefix: x.prefix,
2024-09-25 21:05:16 +07:00
firstName: x.firstName,
2024-09-24 16:42:24 +07:00
lastName: x.lastName,
profileId: x.profileId,
order: x.order,
2024-09-25 16:17:06 +07:00
remarkVertical: x.remarkVertical,
remarkHorizontal: x.remarkHorizontal,
amount: x.amount,
positionSalaryAmount: x.positionSalaryAmount,
mouthSalaryAmount: x.mouthSalaryAmount,
2024-09-24 16:42:24 +07:00
})),
};
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: {
2024-09-25 11:17:38 +07:00
positionDetail: string | null;
commandSalaryId: string | null;
2024-09-24 16:42:24 +07:00
},
@Request() request: RequestWithUser,
) {
const command = await this.commandRepository.findOne({ where: { id: id } });
if (!command) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
}
2024-09-25 11:17:38 +07:00
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, "ไม่พบข้อมูลต้นแบบ");
}
}
2024-09-24 16:42:24 +07:00
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();
}
2024-09-25 21:05:16 +07:00
@Get("tab2/swap/{direction}/{commandReciveId}")
2024-09-25 11:17:38 +07:00
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();
}
2024-09-25 16:17:06 +07:00
/**
* 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();
}
2024-09-25 11:17:38 +07:00
/**
* API
*
* @summary API
*
* @param {string} id Id
*/
2024-09-25 16:17:06 +07:00
@Delete("tab2/{commandReciveId}")
async DeleteTab2(@Path() commandReciveId: string) {
2024-09-25 11:17:38 +07:00
const commandRecive = await this.commandReciveRepository.findOne({
2024-09-25 16:17:06 +07:00
where: { id: commandReciveId },
2024-09-25 11:17:38 +07:00
});
if (!commandRecive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับคำสั่ง");
}
2024-09-25 16:17:06 +07:00
const commandId = commandRecive.commandId;
2024-09-26 11:18:28 +07:00
await this.commandReciveRepository.delete(commandRecive.id);
2024-09-25 16:17:06 +07:00
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);
});
2024-09-25 11:17:38 +07:00
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 },
2024-09-13 15:34:01 +07:00
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,
2024-09-25 21:05:16 +07:00
firstName: item.firstName,
lastName: item.lastName,
position: item.position,
org: item.org,
2024-09-13 15:34:01 +07:00
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;
2024-09-25 21:05:16 +07:00
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;
2024-09-13 15:34:01 +07:00
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) => {
2024-09-13 15:34:01 +07:00
const commandSendCC = await this.commandSendCCRepository.find({
where: { commandSendId: item.id },
});
2024-09-13 15:34:01 +07:00
await this.commandSendCCRepository.remove(commandSendCC);
2024-09-13 15:34:01 +07:00
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, "ไม่พบข้อมูลผู้ได้รับสำเนาคำสั่ง");
}
2024-09-27 11:10:17 +07:00
await this.commandSendCCRepository.delete({ commandSendId: commandSendId });
await this.commandSendRepository.delete(commandSendId);
return new HttpSuccess();
}
2024-09-11 17:29:33 +07:00
/**
* 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,
2024-09-11 17:29:33 +07:00
) {
2024-09-27 10:12:54 +07:00
const command = await this.commandRepository.findOne({
where: { id: id },
});
2024-09-11 17:29:33 +07:00
if (!command) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
}
const copy = new Command();
Object.assign(copy, { ...command, ...requestBody, id: undefined });
2024-09-27 10:12:54 +07:00
copy.status = "NEW";
2024-09-11 17:29:33 +07:00
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);
2024-09-27 10:12:54 +07:00
const commandRecives = await this.commandReciveRepository.find({
where: { commandId: id },
});
const commandSends = await this.commandSendRepository.find({
where: { commandId: id },
2024-09-27 11:10:17 +07:00
relations: ["commandSendCCs"],
2024-09-27 10:12:54 +07:00
});
commandRecives.map((x: any) => {
delete x.id;
return {
...x,
2024-09-27 11:10:17 +07:00
commandId: copy.id,
2024-09-27 10:12:54 +07:00
createdUserId: request.user.sub,
createdFullName: request.user.name,
createdAt: new Date(),
lastUpdateUserId: request.user.sub,
lastUpdateFullName: request.user.name,
lastUpdatedAt: new Date(),
};
});
2024-09-27 11:10:17 +07:00
await this.commandReciveRepository.save(commandRecives);
2024-09-27 10:12:54 +07:00
2024-09-27 11:10:17 +07:00
commandSends.map(async (x: any) => {
2024-09-27 10:12:54 +07:00
delete x.id;
2024-09-27 11:10:17 +07:00
let commandSend = {
2024-09-27 10:12:54 +07:00
...x,
2024-09-27 11:10:17 +07:00
commandSendCCs: [],
commandId: copy.id,
2024-09-27 10:12:54 +07:00
createdUserId: request.user.sub,
createdFullName: request.user.name,
createdAt: new Date(),
lastUpdateUserId: request.user.sub,
lastUpdateFullName: request.user.name,
lastUpdatedAt: new Date(),
};
2024-09-27 11:10:17 +07:00
await this.commandSendRepository.save(commandSend);
let commandSendCC = await x.commandSendCCs.map((y: any) => {
delete y.id;
return {
...y,
commandSendId: commandSend.id,
createdUserId: request.user.sub,
createdFullName: request.user.name,
createdAt: new Date(),
lastUpdateUserId: request.user.sub,
lastUpdateFullName: request.user.name,
lastUpdatedAt: new Date(),
};
});
await this.commandSendCCRepository.save(commandSendCC);
2024-09-27 10:12:54 +07:00
});
2024-09-11 17:29:33 +07:00
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,
2024-09-11 17:29:33 +07:00
) {
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();
}
2024-09-11 17:29:33 +07:00
/**
* 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, "ไม่พบข้อมูลคำสั่งนี้");
}
2024-09-26 19:46:09 +07:00
const commandSend = await this.commandSendRepository.find({
where: { commandId: id },
});
await this.commandSendCCRepository.delete({ commandSendId: In(commandSend.map((x) => x.id)) });
2024-09-26 16:05:59 +07:00
await this.commandReciveRepository.delete({ commandId: command.id });
await this.commandSendRepository.delete({ commandId: command.id });
2024-09-11 17:29:33 +07:00
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,
2024-09-24 16:42:24 +07:00
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()
2024-09-24 16:42:24 +07:00
requestBody: { sign: boolean },
@Request() request: RequestWithUser,
) {
const command = await this.commandRepository.findOne({ where: { id: id } });
if (!command) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
}
2024-09-24 16:42:24 +07:00
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 } });
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 {
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();
}
2024-09-26 16:05:59 +07:00
/**
* API tab4
*
* @summary API tab4
*
* @param {string} id Id
*/
@Get("tab4/cover/{id}")
async GetByIdTab4Cover(@Path() id: string) {
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,
2024-09-26 17:37:20 +07:00
commandDate:
command.commandAffectDate == null
? ""
2024-09-26 17:37:20 +07:00
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)),
commandExcecuteDate:
command.commandExcecuteDate == null
? ""
2024-09-26 17:37:20 +07:00
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandExcecuteDate)),
2024-09-26 16:05:59 +07:00
name: "...................................",
position: "...................................",
};
return new HttpSuccess({
template: command.commandType.fileCover,
reportName: "docx-report",
2024-09-26 17:25:33 +07:00
data: _command,
2024-09-26 16:05:59 +07:00
});
}
/**
* API tab4
*
* @summary API tab4
*
* @param {string} id Id
*/
@Get("tab4/attachment/{id}")
async GetByIdTab4Attachment(@Path() id: string) {
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,
2024-09-26 17:37:20 +07:00
commandDate:
command.commandAffectDate == null
? ""
2024-09-26 17:37:20 +07:00
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)),
commandExcecuteDate:
command.commandExcecuteDate == null
? ""
2024-09-26 17:37:20 +07:00
: Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandExcecuteDate)),
2024-09-26 16:05:59 +07:00
name: "...................................",
position: "...................................",
};
return new HttpSuccess({
template: command.commandType.fileAttachment,
reportName: "xlsx-report",
2024-09-26 17:25:33 +07:00
data: _command,
2024-09-26 16:05:59 +07:00
});
}
2024-09-11 17:29:33 +07:00
}