import { BadRequestException, Controller, Get, Param, Query, } from '@nestjs/common'; import { WorkflowAutomationEngineService } from 'src/module/workflow-automation/service/workflow-automation-engine.service'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; import { EntityMasterService } from '../service/entity-master.service'; import { EntityServiceImpl } from '../service/entity-service-impl.service'; @Controller('entity/public') export class EntityPublicController { constructor( private entityService: EntityServiceImpl, private reflectionHelper: ReflectionHelper, private entityMasterService: EntityMasterService, private readonly workflowAutomationEngineService: WorkflowAutomationEngineService, ) {} @Get('getById/:id') async getPublicById( @Param('id') id: number, @Query() queryParams: Record, ) { const { entity_type: entityType, loggedInUser: loggedInUserParam, ...params } = queryParams; if (!entityType) { throw new BadRequestException( 'Query parameter "entity_type" is required', ); } // Default user object (if organization_id or appcode is needed in service) let loggedInUser: any = {}; if (typeof loggedInUserParam === 'string') { try { loggedInUser = JSON.parse(loggedInUserParam); } catch (e) { loggedInUser = {}; } } else if ( typeof loggedInUserParam === 'object' && loggedInUserParam !== null ) { loggedInUser = loggedInUserParam; } else { loggedInUser = {}; } if (!entityType) { throw new BadRequestException( 'Query parameter "entity_type" is required', ); } const entityMaster = await this.entityMasterService.getEntityData( entityType, loggedInUser, ); const entityService = await this.reflectionHelper.getBean( entityMaster.entity_service, ); if (entityService) { return await entityService.getEntityData(entityType, id, loggedInUser); } } }