import { Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; import { EntityServiceImpl } from './entity-service-impl.service'; import { ReflectionHelper } from 'src/utils/service/reflection-helper.service'; @Injectable() export class PopulateMetaService { constructor( private readonly dataSource: DataSource, private readonly entityServiceImpl: EntityServiceImpl, private readonly reflectionHelper: ReflectionHelper, ) {} async populateMetaData(organization_id: number) { const metadataTables = [ 'frm_entity_master', 'frm_entity_attribute', 'frm_entity_view', 'frm_entity_relation', ]; const metaData = [ 'EntityMaster', 'AttributeMaster', 'ViewMaster', 'EntityRelationData', ]; const listMasterItemsRepo = this.reflectionHelper.getRepoService('ListMasterItems'); const viewMasterRepo = this.reflectionHelper.getRepoService('ViewMaster'); const relationDataRepo = this.reflectionHelper.getRepoService('EntityRelationData'); const relationRepo = this.reflectionHelper.getRepoService('EntityRelation'); const attributeMasterRepo = this.reflectionHelper.getRepoService('AttributeMaster'); const entityMasterRepo = this.reflectionHelper.getRepoService('EntityMaster'); if (!listMasterItemsRepo) return; const getStatus = (await listMasterItemsRepo.find({ where: { organization_id: organization_id, code: 'STATUS_ACTIVE' }, })) as any[]; let status: any; if (getStatus && getStatus.length > 0) { status = getStatus[0].id; } else { status = 'ACTIVE'; } let oneToManyId = await listMasterItemsRepo?.find({ where: { value: 'ONE_TO_MANY', organization_id: organization_id , enterprise_id: organization_id }, }); let oneToOneId = await listMasterItemsRepo?.find({ where: { value: 'ONE_TO_ONE', organization_id: organization_id, enterprise_id: organization_id }, }); // Step 1: Insert basic metadata tables for (const table of metadataTables) { let transformedData; let transformRepo; if (table === 'frm_entity_master') { transformRepo = entityMasterRepo; const factoryData = await transformRepo?.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, }, }); transformedData = factoryData.map( ({ id, created_date, modified_date, ...rest }) => ({ ...rest, organization_id, enterprise_id: organization_id, level_type: 'ORG', level_id: organization_id, is_workflow: rest.is_workflow == 1 ? true : false, status: status, }), ); } else if (table === 'frm_entity_attribute') { transformRepo = attributeMasterRepo; const factoryData = await transformRepo?.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, }, }); transformedData = factoryData.map( ({ id, created_date, modified_date, ...rest }) => ({ ...rest, organization_id, enterprise_id: organization_id, level_type: 'ORG', level_id: organization_id, // is_unique: rest.is_unique == 1 ? true : false, // required: rest.required == 1 ? true : false, // is_hidden: rest.is_hidden == 1 ? true : false, status: status, }), ); } else if (table === 'frm_entity_relation') { if (!oneToManyId || !oneToOneId) continue; transformRepo = relationRepo; const factoryData = await transformRepo?.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, }, }); transformedData = factoryData.map( ({ id, created_date, modified_date, ...rest }) => ({ ...rest, organization_id, enterprise_id: organization_id, level_type: 'ORG', level_id: organization_id, status: status, relation_id: rest.relation_type == 'ONE_TO_ONE' ? oneToOneId[0].id : oneToManyId[0].id, }), ); } else if (table === 'frm_entity_view') { transformRepo = viewMasterRepo; const factoryData = await transformRepo?.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, }, }); transformedData = factoryData.map( ({ id, created_date, modified_date, ...rest }) => ({ ...rest, organization_id, enterprise_id: organization_id, level_type: 'ORG', level_id: organization_id, status: status, }), ); } await transformRepo?.save(transformedData); } // table population for entity tables and entity table columns const entityTableRepo = this.reflectionHelper.getRepoService('EntityTable'); const entityTableColumnRepo = this.reflectionHelper.getRepoService('EntityTableColumn'); if (!entityTableRepo || !entityTableColumnRepo) return; const entityTables = await entityTableRepo.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, }, }); for (let entityTable of entityTables) { const { id, mapped_entity_type, display_type, ...rest } = entityTable; entityTable = { ...rest, organization_id, enterprise_id: organization_id, mapped_entity_type: mapped_entity_type, level_type: 'ORG', level_id: organization_id, display_type: display_type, }; const insertResult = await entityTableRepo.save(entityTable); const newId = insertResult.id; const entityTableColumns = await entityTableColumnRepo.find({ where: { organization_id: -1, level_type: 'ORG', level_id: -1, mapped_entity_type: mapped_entity_type, display_type: display_type, }, }); let tableCloumnArray = [] as any[]; for (const column of entityTableColumns) { const { id, created_date, modified_date, ...columnRest } = column; const newColumn = { ...columnRest, organization_id, enterprise_id: organization_id, parent_id: newId, level_type: 'ORG', level_id: organization_id, mapped_entity_type: mapped_entity_type, display_type: display_type, }; tableCloumnArray.push(newColumn); } await entityTableColumnRepo.save(tableCloumnArray); } return { message: 'Metadata populated successfully', status: 'success', }; } }