import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { DataSource, Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { EntityMaster } from '../entity/entity-master.entity'; import { EntityMasterRepository } from '../repository/entity-master.repository'; import { ListMasterService } from 'src/module/listmaster/service/list-master.service'; import { SavedFilterRepositoryService } from 'src/module/filter/repository/saved-filter.repository'; import { AttributeMasterRepository } from '../repository/attribute-master.repository'; @Injectable() export class EntityMasterService { constructor( @InjectRepository(EntityMaster) private entityMasterRepository: Repository, private attributeMasterRepo: AttributeMasterRepository, private entityMasterRepo: EntityMasterRepository, private readonly dataSource: DataSource, @Inject('ListMasterService') private readonly listMasterService: ListMasterService, private readonly savedFilterRepoService: SavedFilterRepositoryService, ) {} async getEntityById(id: number): Promise { return this.entityMasterRepo.getEntityById(id); } async getAllEntityDropDown(entity_type:string,loggedInUser): Promise<{ label: string; value: any }[]>{ return this.entityMasterRepo.getAllEntityDropdownData(entity_type,loggedInUser); } async getEntityData( mappedEntityType: string | null, loggedInUser, mapped_entity_id?: number, ): Promise { if (mappedEntityType) { let filterCondition = { mapped_entity_type: mappedEntityType, enterprise_id: loggedInUser.enterprise_id, }; if (mapped_entity_id) { filterCondition['id'] = mapped_entity_id; } const entityMaster = await this.entityMasterRepository.findOne({ where: filterCondition, }); if (!entityMaster) { throw new Error( `Entity with mappedEntityType "${mappedEntityType}" not found.`, ); } return entityMaster; } else { throw new Error(); } } async getEntityByTableName(tableName: string) { const entityMaster = await this.entityMasterRepository.findOne({ where: { db_table_name: tableName }, }); return entityMaster; } async save(entityMaster: EntityMaster) { return await this.entityMasterRepository.save(entityMaster); } async update(id: number, entityMaster: EntityMaster) { return await this.entityMasterRepository.update(id, entityMaster); } async findByMappedEntityType(mappedEntityType: string) { return await this.entityMasterRepository.findOne({ where: { mapped_entity_type: mappedEntityType }, }); } async getFilterMetaData(entityType: string, loggedInUser: any) { // Step 1: Get entity_master record by mapped_entity_type const entityMasterData = await this.entityMasterRepo.getEntityByMappedEntityType( entityType, loggedInUser.enterprise_id, ); if (!entityMasterData) { throw new Error( `Entity master with mapped_entity_type "${entityType}" not found.`, ); } // Step 2: Get attribute_master (instead of entity_table_column) const attributes = await this.attributeMasterRepo.findAttributesByMappedEntityType(entityMasterData.mapped_entity_type, loggedInUser); // Step 3: Attach column_list from attribute_master entityMasterData['attribute_list'] = attributes; // Step 4: Keep existing operation_list logic entityMasterData['operation_list'] = { text: await this.listMasterService.getDropdownOptions( 'OPT', {}, [], loggedInUser, ), number: await this.listMasterService.getDropdownOptions( 'OPN', {}, [], loggedInUser, ), date: await this.listMasterService.getDropdownOptions( 'OPD', {}, [], loggedInUser, ), select: await this.listMasterService.getDropdownOptions( 'OPS', {}, [], loggedInUser, ), multiselect: await this.listMasterService.getDropdownOptions( 'OPM', {}, [], loggedInUser, ), year: await this.listMasterService.getDropdownOptions( 'OPY', {}, [], loggedInUser, ), }; // Step 5: Keep filters as is entityMasterData['default_filter'] = await this.savedFilterRepoService.getDefaultFilterByEntityType( entityType, ); if (loggedInUser.id) { entityMasterData['saved_filter'] = await this.savedFilterRepoService.getSavedFiltersByUserIdAndEntityType( loggedInUser, entityType, ); } return entityMasterData; } async getOperationList(loggedInUser: any) { return await this.entityMasterRepo.getOperationList(loggedInUser); } async getAttributesOfTypeDate(entityType: string, loggedInUser: any) { return await this.entityMasterRepo.getAttributesOfTypeDate( entityType, loggedInUser, ); } }