import { Injectable } from '@nestjs/common'; import { AttributeMasterRepository } from '../repository/attribute-master.repository'; import { UserData } from 'src/module/user/entity/user.entity'; import { AttributeMaster } from '../entity/attribute-master.entity'; @Injectable() export class AttributeMasterService { constructor( private readonly attributeMasterRepository: AttributeMasterRepository, ) { } async findAttributesByMappedEntityType( entity_type: string, loggedInUser: UserData, ) { return this.attributeMasterRepository.findAttributesByMappedEntityType( entity_type, loggedInUser, ); } async createAttribute( attributeData: AttributeMaster, loggedInUser: UserData, ): Promise { attributeData.organization_id = loggedInUser.organization_id; attributeData.enterprise_id = loggedInUser.enterprise_id; attributeData.level_type = loggedInUser.level_type; attributeData.level_id = loggedInUser.level_id; // 1️⃣ Save attribute in master table const createdAttr = await this.attributeMasterRepository.createAttribute(attributeData); // 2️⃣ Get mapped table name const tableName = await this.attributeMasterRepository.getTableNameByEntityType( attributeData.mapped_entity_type, loggedInUser, ); if (!tableName) { throw new Error( `No table found for entity type ${attributeData.mapped_entity_type}`, ); } // 3️⃣ Decide SQL type based on data type let columnType: string; switch (attributeData.element_type) { case 'text': columnType = 'VARCHAR(255)'; break; case 'number': columnType = 'INT'; break; case 'date': columnType = 'DATE'; break; case 'boolean': columnType = 'int(1)'; break; default: columnType = 'VARCHAR(255)'; } // 4️⃣ Add column if not exists const columnExists = await this.attributeMasterRepository.checkIfColumnExists( tableName, attributeData.attribute_key, ); if (!columnExists) { await this.attributeMasterRepository.addColumnToTable( tableName, attributeData.attribute_key, columnType, ); } return createdAttr; } async updateAttribute( attributeData: AttributeMaster, id: number, loggedInUser: UserData, ): Promise { attributeData.organization_id = loggedInUser.organization_id; attributeData.enterprise_id = loggedInUser.enterprise_id; attributeData.level_type = loggedInUser.level_type; attributeData.level_id = loggedInUser.level_id; return this.attributeMasterRepository.updateAttribute(attributeData, id); } async getAttributeById(id: number): Promise { return this.attributeMasterRepository.getAttributeById(id); } async getAttributesByEntityType(entity_type,loggedInUser){ return this.attributeMasterRepository.getAllAttributeByEntity(entity_type,loggedInUser) } async getAttributesByMappedEntityType( entityType: string, loggedInUser: UserData, ): Promise { return this.attributeMasterRepository.findAttributesByMappedEntityType( entityType, loggedInUser, ); } async getAttributesDropdownList( entityType: string, loggedInUser: UserData, ): Promise<{ label: string; value: number }[]> { return this.attributeMasterRepository.getAttributesDropdownList( entityType, loggedInUser, ); } async findByMappedEntityTypeAndAttributeKey(mapped_entity_type: string, attribute_key: string, loggedInUser: UserData) { return await this.attributeMasterRepository.findByMappedEntityTypeAndAttributeKeyAndOrganizationId( mapped_entity_type, attribute_key, loggedInUser.enterprise_id ); } }