import { Inject, Injectable } from '@nestjs/common'; import { EntityMaster } from '../entity/entity-master.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { ListMasterService } from 'src/module/listmaster/service/list-master.service'; import { EntityRelation } from '../entity/entity-relation.entity'; import { AttributeMaster } from '../entity/attribute-master.entity'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; @Injectable() export class EntityMasterRepository { constructor( @InjectRepository(EntityMaster) private readonly entityMasterRepository: Repository, @Inject('ListMasterService') private readonly listMasterService: ListMasterService, @InjectRepository(EntityRelation) private readonly entityRelationRepo: Repository, @InjectRepository(AttributeMaster) private readonly attributeMasterRepository: Repository, private readonly reflectionHelper: ReflectionHelper, ) { } async getEntityById(id: number): Promise { let data = await this.entityMasterRepository.findOne({ where: { id } }); if (data) { const listMasterItemsRepo = this.reflectionHelper.getRepoService('ListMasterItems'); let listMasterItemsata = await listMasterItemsRepo.findOne({ where: { id: data.status }, }); data.status = listMasterItemsata?.name; } return data; } async getAllEntityDropdownData(entity_type: string, loggedInUser): Promise<{ label: string; value: any }[]> { let relations = await this.entityRelationRepo .createQueryBuilder('rel') .select([ 'rel.name AS label', 'rel.target_entity_type AS value', ]) .where('rel.source_entity_type = :entityType', { entityType: entity_type }) .andWhere('rel.enterprise_id = :entId', { entId: loggedInUser.enterprise_id }) .getRawMany(); // console.log(relations,"-------------") let parentEntityLabelName = await this.getEntityByMappedEntityType(entity_type, loggedInUser.enterprise_id); let dropdownData: any[] = [...relations]; if (parentEntityLabelName) { dropdownData.push({ label: parentEntityLabelName.name, value: entity_type, }); } return dropdownData.length ? dropdownData : []; } async getEntityByMappedEntityType( mappedEntityType: string, enterprise_id: any, ): Promise { return await this.entityMasterRepository.findOne({ where: { mapped_entity_type: mappedEntityType, enterprise_id: enterprise_id, }, }); } async getOperationList(loggedInUser: any) { const [text, number, date, select, multiselect, year] = await Promise.all([ this.listMasterService.getDropdownOptions('OPT', {}, [], loggedInUser), this.listMasterService.getDropdownOptions('OPN', {}, [], loggedInUser), this.listMasterService.getDropdownOptions('OPD', {}, [], loggedInUser), this.listMasterService.getDropdownOptions('OPS', {}, [], loggedInUser), this.listMasterService.getDropdownOptions('OPM', {}, [], loggedInUser), this.listMasterService.getDropdownOptions('OPY', {}, [], loggedInUser), ]); return { text, number, date, select, multiselect, year }; } async getAttributesOfTypeDate(entity_type, loggedInUser) { const enterprise_id = loggedInUser.enterprise_id; const attributes = await this.attributeMasterRepository.find({ where: { mapped_entity_type: entity_type, data_type: 'date', enterprise_id, }, }); return attributes.map((attr) => ({ label: attr.name, value: attr.attribute_key, })); } async findByEnterpriseIdAndAppCode(enterprise_id, appcode?) { let where = { enterprise_id, }; if (appcode) { where['appcode'] = appcode; } return await this.entityMasterRepository.find({ where, select: ['name', 'mapped_entity_type'] }); } }