เพิ่ม Upload sql ของ posType เเละ posLevel

This commit is contained in:
AnandaTon 2024-06-12 14:44:18 +07:00
parent bd9dee6da2
commit 140bb6dbbc

View file

@ -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);
}
}