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"; @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 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 }); copy.status = "NEW"; 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); const commandRecives = await this.commandReciveRepository.find({ where: { commandId: id }, }); const commandSends = await this.commandSendRepository.find({ where: { commandId: id }, relations: ["commandSendCCs"], }); commandRecives.map((x: any) => { delete x.id; return { ...x, commandId: copy.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.commandReciveRepository.save(commandRecives); commandSends.map(async (x: any) => { delete x.id; let commandSend = { ...x, commandSendCCs: [], commandId: copy.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.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); }); 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 } }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } command.isSign = true; if (new Date().getDate() > 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(); } /** * 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, commandDate: command.commandAffectDate == null ? null : Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)), commandExcecuteDate: command.commandExcecuteDate == null ? 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) { 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 ? null : Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)), commandExcecuteDate: command.commandExcecuteDate == null ? null : Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandExcecuteDate)), name: "...................................", position: "...................................", }; return new HttpSuccess({ template: command.commandType.fileAttachment, reportName: "xlsx-report", data: _command, }); } }