import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { AttributeMaster } from '../entity/attribute-master.entity'; import { EntityManager, Repository } from 'typeorm'; import { UserData } from 'src/module/user/entity/user.entity'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; import { ConfigService } from '@nestjs/config'; @Injectable() export class AttributeMasterRepository { constructor( @InjectRepository(AttributeMaster) private readonly attributeMasterRepository: Repository, private readonly entityManger: EntityManager, private readonly reflectionHelper: ReflectionHelper, private readonly configService: ConfigService, ) { } schema = this.configService.get('DB_SCHEMA'); // ---------- Existing Methods ---------- async findByMappedEntityIdAndMappedEntityTypeAndAttributeKeyAndOrganizationId( mappedEntityId: number, mappedEntityType: string, attributeKey: string, organizationId: number, ): Promise { return await this.attributeMasterRepository.findOne({ where: { mapped_entity_id: mappedEntityId, mapped_entity_type: mappedEntityType, attribute_key: attributeKey, organization_id: organizationId, }, }); } async findAttributesByMappedEntityType( entityType: string, loggedInUser: UserData, ): Promise { return await this.attributeMasterRepository.find({ where: { mapped_entity_type: entityType, enterprise_id: loggedInUser.enterprise_id, }, }); } async createAttribute( attributeData: AttributeMaster, ): Promise { return await this.attributeMasterRepository.save(attributeData); } async updateAttribute( attributeData: AttributeMaster, id: number, ): Promise { await this.attributeMasterRepository.update({ id }, attributeData); return this.attributeMasterRepository.findOne({ where: { id } }); } async getAttributeById(id: number): Promise { return await this.attributeMasterRepository.findOne({ where: { id } }); } async getAllAttributeByEntity(entity_type: string, loggedInUser): Promise<{ label: string; value: any }[]> { let data = await this.attributeMasterRepository.find({ where: { enterprise_id: loggedInUser.enterprise_id, mapped_entity_type: entity_type, }, }); if (data) { const dropdownData = data.map((attribute) => { return { 'label': attribute.name, 'value': attribute.attribute_key, 'data_type': attribute.data_type, 'data_source_list': attribute.datasource_list, }; }); return dropdownData; } return []; } async getAttributesDropdownList( entityType: string, loggedInUser: UserData, ): Promise<{ label: string; value: number }[]> { const data = await this.attributeMasterRepository.find({ where: { mapped_entity_type: entityType, enterprise_id: loggedInUser.enterprise_id, }, }); return data.map((item) => ({ label: item.name, value: item.id, })); } // ---------- New Raw SQL Methods ---------- async getTableNameByEntityType( entityType: string, loggedInUser: UserData, ): Promise { const entityMasterRepo = this.reflectionHelper.getRepoService('EntityMaster'); const result = await entityMasterRepo.findOne({ where: { mapped_entity_type: entityType, enterprise_id: loggedInUser.enterprise_id, }, }); if (result) { return result.db_table_name; } return null; } async checkIfColumnExists( tableName: string, columnName: string, ): Promise { const result = await this.entityManger.query( ` SELECT column_name FROM information_schema.columns WHERE table_name = $1 AND column_name = $2 `, [tableName, columnName], ); return result.length > 0; } async addColumnToTable( tableName: string, columnName: string, columnType: string, ): Promise { const alterQuery = ` ALTER TABLE ${this.schema}.${tableName} ADD COLUMN IF NOT EXISTS ${columnName} ${columnType} `; await this.entityManger.query(alterQuery); } async findByMappedEntityTypeAndAttributeKeyAndOrganizationId(mapped_entity_type: string, attribute_key: string, enterprise_id: number) { return await this.attributeMasterRepository.findOne({ where: { mapped_entity_type, attribute_key, enterprise_id, }, }); } }