diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 8c2604b9..9544aa3f 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -30,6 +30,8 @@ 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") @@ -47,6 +49,7 @@ export class CommandController extends Controller { 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); /** @@ -276,7 +279,7 @@ export class CommandController extends Controller { prefix: x.prefix, firstName: x.firstName, lastName: x.lastName, - profileId: x.profileId, + // profileId: x.profileId, order: x.order, remarkVertical: x.remarkVertical, remarkHorizontal: x.remarkHorizontal, @@ -858,6 +861,15 @@ export class CommandController extends Controller { ) { command.status = "WAITING"; } else { + let path = this.commandTypePath(command.commandType.name); + if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); + + new CallAPI() + .PostData(request, path + "excecute", { + refIds: command.commandRecives.map((x) => x.refId), + }) + .then(async (res) => {}) + .catch(() => {}); command.status = "REPORTED"; } command.lastUpdateUserId = request.user.sub; @@ -875,7 +887,7 @@ export class CommandController extends Controller { * @param {string} id Id คำสั่ง */ @Get("tab4/cover/{id}") - async GetByIdTab4Cover(@Path() id: string) { + async GetByIdTab4Cover(@Path() id: string, @Request() request: RequestWithUser) { const command = await this.commandRepository.findOne({ where: { id }, relations: ["commandType"], @@ -918,38 +930,230 @@ export class CommandController extends Controller { * @param {string} id Id คำสั่ง */ @Get("tab4/attachment/{id}") - async GetByIdTab4Attachment(@Path() id: string) { + async GetByIdTab4Attachment(@Path() id: string, @Request() request: RequestWithUser) { const command = await this.commandRepository.findOne({ where: { id }, - relations: ["commandType"], + relations: ["commandType", "commandRecives"], }); 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: "...................................", - }; + let _command: any = []; + let path = this.commandTypePath(command.commandType.name); + 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; + 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(); + if ( + requestBody.commandId != undefined && + requestBody.commandId != null && + requestBody.commandId != "" + ) { + const _command = await this.commandRepository.findOne({ + where: { id: requestBody.commandId }, + relations: ["commandRecives"], + order: { + commandRecives: { + order: "DESC", + }, + }, + }); + if (!_command) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบคำสั่งนี้ในระบบ"); + } + command = _command; + } else { + 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); + } + + let path = this.commandTypePath(command.commandType.name); + 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(commandType: string) { + let path = null; + switch (commandType) { + case "C-PM-01": + path = "/placemant/main/report/"; + case "C-PM-02": + path = "/placemant/recive/report/"; + case "C-PM-03": + path = "/discipline/recive/report/"; + case "C-PM-04": + path = "/xxxxxx/"; + case "C-PM-05": + path = "/xxxxxx/"; + case "C-PM-06": + path = "/xxxxxx/"; + case "C-PM-07": + path = "/xxxxxx/"; + case "C-PM-08": + path = "/xxxxxx/"; + case "C-PM-09": + path = "/xxxxxx/"; + case "C-PM-10": + path = "/xxxxxx/"; + case "C-PM-11": + path = "/xxxxxx/"; + case "C-PM-12": + path = "/xxxxxx/"; + case "C-PM-13": + path = "/xxxxxx/"; + case "C-PM-14": + path = "/xxxxxx/"; + case "C-PM-15": + path = "/xxxxxx/"; + case "C-PM-16": + path = "/xxxxxx/"; + case "C-PM-17": + path = "/xxxxxx/"; + case "C-PM-18": + path = "/xxxxxx/"; + case "C-PM-19": + path = "/xxxxxx/"; + case "C-PM-20": + path = "/xxxxxx/"; + case "C-PM-21": + path = "/xxxxxx/"; + case "C-PM-22": + path = "/xxxxxx/"; + case "C-PM-23": + path = "/xxxxxx/"; + case "C-PM-24": + path = "/xxxxxx/"; + case "C-PM-25": + path = "/xxxxxx/"; + case "C-PM-26": + path = "/xxxxxx/"; + case "C-PM-27": + path = "/xxxxxx/"; + case "C-PM-28": + path = "/xxxxxx/"; + case "C-PM-29": + path = "/xxxxxx/"; + case "C-PM-30": + path = "/xxxxxx/"; + case "C-PM-31": + path = "/xxxxxx/"; + case "C-PM-32": + path = "/xxxxxx/"; + case "C-PM-33": + path = "/xxxxxx/"; + case "C-PM-34": + path = "/xxxxxx/"; + case "C-PM-35": + path = "/xxxxxx/"; + case "C-PM-36": + path = "/xxxxxx/"; + case "C-PM-37": + path = "/xxxxxx/"; + case "C-PM-38": + path = "/xxxxxx/"; + case "C-PM-39": + path = "/xxxxxx/"; + case "C-PM-40": + path = "/xxxxxx/"; + case "C-PM-41": + path = "/xxxxxx/"; + default: + path = null; + } + return path; + } } diff --git a/src/entities/CommandRecive.ts b/src/entities/CommandRecive.ts index b017f82e..3b25ad3d 100644 --- a/src/entities/CommandRecive.ts +++ b/src/entities/CommandRecive.ts @@ -1,7 +1,6 @@ import { Entity, Column, JoinColumn, ManyToOne, OneToMany, Double } from "typeorm"; import { EntityBase } from "./base/Base"; import { Command } from "./Command"; -import { Profile } from "./Profile"; @Entity("commandRecive") export class CommandRecive extends EntityBase { @@ -37,22 +36,6 @@ export class CommandRecive extends EntityBase { }) lastName: string; - @Column({ - nullable: true, - comment: "ตำแหน่ง", - length: 255, - default: null, - }) - position: string; - - @Column({ - nullable: true, - comment: "หน่วยงาน", - length: 255, - default: null, - }) - org: string; - @Column({ nullable: true, comment: "ลำดับแสดงผล", @@ -100,6 +83,14 @@ export class CommandRecive extends EntityBase { }) mouthSalaryAmount: Double; + @Column({ + nullable: true, + length: 40, + comment: "refId", + default: null, + }) + refId: string; + @Column({ length: 40, comment: "คีย์นอก(FK)ของตาราง command", @@ -110,15 +101,25 @@ export class CommandRecive extends EntityBase { @JoinColumn({ name: "commandId" }) command: Command; - @Column({ - length: 40, - comment: "คีย์นอก(FK)ของตาราง profile", - }) - profileId: string; + // @Column({ + // length: 40, + // comment: "คีย์นอก(FK)ของตาราง profile", + // }) + // profileId: string; - @ManyToOne(() => Profile, (profile) => profile.commandRecives) - @JoinColumn({ name: "profileId" }) - profile: Profile; + // @ManyToOne(() => Profile, (profile) => profile.commandRecives) + // @JoinColumn({ name: "profileId" }) + // profile: Profile; + + // @Column({ + // length: 40, + // comment: "คีย์นอก(FK)ของตาราง profileEmployee", + // }) + // profileEmployeeId: string; + + // @ManyToOne(() => ProfileEmployee, (profileEmployee) => profileEmployee.commandRecives) + // @JoinColumn({ name: "profileEmployeeId" }) + // profileEmployee: ProfileEmployee; } export class CreateCommandRecive { diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 968d2046..0978f528 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -29,10 +29,8 @@ import { ProfileDiscipline } from "./ProfileDiscipline"; import { ProfileEmployee } from "./ProfileEmployee"; import { ProfileEdit } from "./ProfileEdit"; import { ProfileDevelopment } from "./ProfileDevelopment"; -import { OrgRoot } from "./OrgRoot"; import { PermissionOrg } from "./PermissionOrg"; import { CommandSend } from "./CommandSend"; -import { CommandRecive } from "./CommandRecive"; import { DevelopmentRequest } from "./DevelopmentRequest"; @Entity("profile") @@ -380,9 +378,6 @@ export class Profile extends EntityBase { @OneToMany(() => CommandSend, (v) => v.profile) commandSends: CommandSend[]; - @OneToMany(() => CommandRecive, (v) => v.profile) - commandRecives: CommandRecive[]; - @ManyToOne(() => PosLevel, (posLevel) => posLevel.profiles) @JoinColumn({ name: "posLevelId" }) posLevel: PosLevel;