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

485 lines
15 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";
import { Brackets } 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";
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);
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 != ""
? "command.status LIKE :status"
: "1=1",
{
status:
status == null || status == undefined || status == ""
? null
: `${status.trim().toLocaleUpperCase()}`,
},
)
.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}`,
},
)
.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: 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.status = "DRAFT";
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,
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;
},
@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();
}
/**
* 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"],
});
if (!command) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้");
}
const _command = command.commandSends.map((item) => ({
id: item.id,
citizenId: item.citizenId,
prefix: item.prefix,
fristName: item.fristName,
lastName: item.lastName,
position: item.position,
org: item.org,
sendCC: item.sendCC,
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.fristName = 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 commandSend = await this.commandSendRepository.findOne({
where: { id: item.id },
});
if (!commandSend) return;
commandSend.sendCC = item.sendCC;
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
*/
@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.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
) {
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: 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, "ไม่พบข้อมูลคำสั่งนี้");
}
await this.commandRepository.delete(command.id);
return new HttpSuccess();
}
}