From 140bb6dbbc2a9916a9ad780417fb145c7493331c Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Wed, 12 Jun 2024 14:44:18 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=20Upload=20sql=20=E0=B8=82=E0=B8=AD=E0=B8=87=20posType=20?= =?UTF-8?q?=E0=B9=80=E0=B9=80=E0=B8=A5=E0=B8=B0=20posLevel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ImportDataController.ts | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index fcebcf7b..f9230ec8 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -28,6 +28,8 @@ import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; import { ProfileFamilyMother } from "../entities/ProfileFamilyMother"; import { ProfileFamilyFather } from "../entities/ProfileFamilyFather"; import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { PosLevel } from "../entities/PosLevel"; +import { PosType } from "../entities/PosType"; @Route("api/v1/org/upload") @Tags("UPLOAD") @@ -40,6 +42,8 @@ export class ImportDataController extends Controller { private salaryRepository = AppDataSource.getRepository(ProfileSalary); private profileRepo = AppDataSource.getRepository(Profile); private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); + private posLevelRepo = AppDataSource.getRepository(PosLevel); + private posTypeRepo = AppDataSource.getRepository(PosType); /** * API upload EDU * @@ -594,4 +598,58 @@ export class ImportDataController extends Controller { await this.salaryRepository.save(profiles); return new HttpSuccess(allData); } + + @Post("uploadposSQL") + @UseInterceptors(FileInterceptor("file")) + async UploadPOSFileSQL(@UploadedFile() file: Express.Multer.File) { + const workbook = xlsx.read(file.buffer, { type: "buffer" }); + const sheetName = workbook.SheetNames[0]; + const sheet = workbook.Sheets[sheetName]; + const getProFile = xlsx.utils.sheet_to_json(sheet); + let profiles: any = []; + await Promise.all( + getProFile.map(async (item: any) => { + // Find profile by ID + let profile = await this.profileRepo.findOne({ + where: { citizenId: item["ID"] }, + }); + if (!profile) { + return; // Skip processing this item + } + + if (item["MP_CEE_TYPE"] === "NULL") { + profile.posTypeId = null; + } else { + // Find posType by posTypeName + const posType = await this.posTypeRepo.findOne({ + where: { posTypeName: item["MP_CEE_TYPE"] }, + }); + if (!posType) { + return; // Skip processing this item + } + profile.posTypeId = posType.id; + } + // Check if posLevelName is "NULL" + if (item["MP_CEE_POSITION"] === "NULL") { + profile.posLevelId = null; + } else { + // Find posLevel by posLevelName + const posLevel = await this.posLevelRepo.findOne({ + where: { posLevelName: item["MP_CEE_POSITION"] }, + }); + if (!posLevel) { + console.error( + `posLevel with name ${item["MP_CEE_POSITION"]} not found for profile ID ${profile.id}`, + ); + return; // Skip processing this item + } + profile.posLevelId = posLevel.id; + } + + profiles.push(profile); + }), + ); + await this.profileRepo.save(profiles); + return new HttpSuccess(getProFile); + } }