diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index a0f03194..5b9a6882 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -1,6 +1,6 @@ import { Controller, Post, Route, Security, Tags, Request, UploadedFile } from "tsoa"; import { AppDataSource } from "../database/data-source"; -import { In, Not } from "typeorm"; +import { In, IsNull, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import { UseInterceptors } from "@nestjs/common"; import { Profile } from "../entities/Profile"; @@ -37,6 +37,13 @@ import { HR_PERSONAL_OFFICER_ADDRESS } from "../entities/HR_PERSONAL_OFFICER_ADD import { HR_EDUCATION_EMP } from "../entities/HR_EDUCATION_EMP"; import { HR_PERSONAL_EMP_ADDRESS } from "../entities/HR_PERSONAL_EMP_ADDRESS"; import { HR_PERSONAL_EMP_FAMILY } from "../entities/HR_PERSONAL_EMP_FAMILY"; +import { OrgRoot } from "../entities/OrgRoot"; +import { OrgChild1 } from "../entities/OrgChild1"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { IMPORT_ORG } from "../entities/IMPORT_ORG"; +import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/upload") @Tags("UPLOAD") @@ -68,6 +75,14 @@ export class ImportDataController extends Controller { private provinceIdRepo = AppDataSource.getRepository(Province); private districtIdRepo = AppDataSource.getRepository(District); private subDistrictIdRepo = AppDataSource.getRepository(SubDistrict); + + private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); + private orgRootRepo = AppDataSource.getRepository(OrgRoot); + private orgChild1Repo = AppDataSource.getRepository(OrgChild1); + private orgChild2Repo = AppDataSource.getRepository(OrgChild2); + private orgChild3Repo = AppDataSource.getRepository(OrgChild3); + private orgChild4Repo = AppDataSource.getRepository(OrgChild4); + private IMPORT_ORGRepo = AppDataSource.getRepository(IMPORT_ORG); /** * @summary ทะเบียนประวัติ ข้าราชการ */ @@ -1447,4 +1462,105 @@ export class ImportDataController extends Controller { } return new HttpSuccess(); } + + /** + * @summary Import Org + */ + @Post("ImportOrg") + async ImportOrg(@Request() request: { user: Record }) { + const IMPORT_ORG = await this.IMPORT_ORGRepo.find({ + where: { orgChild1: Not(IsNull()) }, + }); + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + if (orgRevision == null) return new HttpSuccess(); + await Promise.all( + IMPORT_ORG.map(async (item) => { + const orgRoot = new OrgRoot(); + orgRoot.orgRootName = item.orgRoot; + orgRoot.misId = item.MISCODE; + orgRoot.orgRevisionId = orgRevision.id; + orgRoot.createdUserId = request.user.sub; + orgRoot.createdFullName = request.user.name; + orgRoot.lastUpdateUserId = request.user.sub; + orgRoot.lastUpdateFullName = request.user.name; + orgRoot.createdAt = new Date(); + orgRoot.lastUpdatedAt = new Date(); + await this.orgRootRepo.save(orgRoot); + + const IMPORT_CHILD1 = await this.IMPORT_ORGRepo.find({ + where: { orgChild2: Not(IsNull()), orgRoot: item.orgRoot }, + }); + await Promise.all( + IMPORT_CHILD1.map(async (item1) => { + const orgChild1 = new OrgChild1(); + orgChild1.orgRootId = orgRoot.id; + orgChild1.orgChild1Name = item1.orgChild1; + orgChild1.misId = item1.MISCODE; + orgChild1.orgRevisionId = orgRevision.id; + orgChild1.createdUserId = request.user.sub; + orgChild1.createdFullName = request.user.name; + orgChild1.lastUpdateUserId = request.user.sub; + orgChild1.lastUpdateFullName = request.user.name; + orgChild1.createdAt = new Date(); + orgChild1.lastUpdatedAt = new Date(); + await this.orgChild1Repo.save(orgChild1); + const IMPORT_CHILD2 = await this.IMPORT_ORGRepo.find({ + where: { + orgChild3: Not(IsNull()), + orgRoot: item.orgRoot, + orgChild1: item1.orgChild1, + }, + }); + + await Promise.all( + IMPORT_CHILD2.map(async (item2) => { + const orgChild2 = new OrgChild2(); + orgChild2.orgRootId = orgRoot.id; + orgChild2.orgChild1Id = orgChild1.id; + orgChild2.orgChild2Name = item2.orgChild2; + orgChild2.misId = item2.MISCODE; + orgChild2.orgRevisionId = orgRevision.id; + orgChild2.createdUserId = request.user.sub; + orgChild2.createdFullName = request.user.name; + orgChild2.lastUpdateUserId = request.user.sub; + orgChild2.lastUpdateFullName = request.user.name; + orgChild2.createdAt = new Date(); + orgChild2.lastUpdatedAt = new Date(); + await this.orgChild2Repo.save(orgChild2); + const IMPORT_CHILD3 = await this.IMPORT_ORGRepo.find({ + where: { + orgRoot: item.orgRoot, + orgChild1: item1.orgChild1, + orgChild2: item2.orgChild2, + }, + }); + + await Promise.all( + IMPORT_CHILD3.map(async (item) => { + const orgChild3 = new OrgChild3(); + orgChild3.orgRootId = orgRoot.id; + orgChild3.orgChild1Id = orgChild1.id; + orgChild3.orgChild2Id = orgChild2.id; + orgChild3.orgChild3Name = item.orgChild3; + orgChild3.misId = item.MISCODE; + orgChild3.orgRevisionId = orgRevision.id; + orgChild3.createdUserId = request.user.sub; + orgChild3.createdFullName = request.user.name; + orgChild3.lastUpdateUserId = request.user.sub; + orgChild3.lastUpdateFullName = request.user.name; + orgChild3.createdAt = new Date(); + orgChild3.lastUpdatedAt = new Date(); + await this.orgChild3Repo.save(orgChild3); + }), + ); + }), + ); + }), + ); + }), + ); + return new HttpSuccess(); + } } diff --git a/src/entities/IMPORT_ORG.ts b/src/entities/IMPORT_ORG.ts new file mode 100644 index 00000000..7c5a2e1e --- /dev/null +++ b/src/entities/IMPORT_ORG.ts @@ -0,0 +1,70 @@ +import { Entity, Column, PrimaryGeneratedColumn } from "typeorm"; + +@Entity("IMPORT_ORG") +export class IMPORT_ORG { + @PrimaryGeneratedColumn() + id!: number; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + DEPARTMENT_CODE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + DIVISION_CODE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + SECTION_CODE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + JOB_CODE: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + orgRoot: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + orgChild1: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + orgChild2: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + orgChild3: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + MISCODE: string; +}