import { BadRequestException, Body, Controller, Get, HttpCode, HttpStatus, InternalServerErrorException, NotFoundException, Param, Post, Query, Req, Request, Res, UseGuards, } from '@nestjs/common'; import { EntityServiceImpl } from '../service/entity-service-impl.service'; import { Response } from 'express'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; import { EntityMasterService } from '../service/entity-master.service'; import { BaseEntity } from '../entity/base-entity.entity'; import { JwtAuthGuard } from '../../auth/guards/jwt.guard'; import * as fs from 'fs'; import * as path from 'path'; import { UserData } from '../../user/entity/user.entity'; import { WorkflowAutomationEngineService, } from 'src/module/workflow-automation/service/workflow-automation-engine.service'; @UseGuards(JwtAuthGuard) @Controller('entity') export class EntityController { constructor( private entityService: EntityServiceImpl, private reflectionHelper: ReflectionHelper, private entityMasterService: EntityMasterService, private readonly workflowAutomationEngineService: WorkflowAutomationEngineService, ) { } @Get('getById/:id') // @Roles('USR', 'VIEW') async getById( @Param('id') id: number, @Req() req: Request & { user: any }, @Query('entity_type') entityType?: string, ) { const loggedInUser = req.user.userData; 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); } } @Get('getByCode/:code') async getDataByCode( @Param('code') code: string, @Req() req: Request & { user: any }, @Query('entity_type') entityType?: string, ) { const loggedInUser = req.user.userData; 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.getEntityDataByCode( entityType, code, loggedInUser, ); } } @Get('getResolvedById/:id') // @Roles('USR', 'VIEW') async getResolveDataById( @Param('id') id: number, @Req() req: Request & { user: any }, @Query('entity_type') entityType?: string, ) { const loggedInUser = req.user.userData; 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.getResolvedEntityData( entityType, id, loggedInUser, ); } } @Post('create') @HttpCode(200) async create( @Body() entityData: BaseEntity, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const appcode = loggedInUser.appcode; 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) { throw new InternalServerErrorException( `No service found for entity_type "${entityType}"`, ); } // ✅ Create const savedData = await entityService.createEntity( entityData, loggedInUser as UserData, null, appcode, ); // ✅ Run workflow automation (no preUpdateStates for CREATE) await this.workflowAutomationEngineService.handleEntityEvent( entityType, 'CREATE', savedData, loggedInUser, null, ); return savedData; } @Post('update/:id') @HttpCode(200) async update( @Param('id') id: number, @Body() entityData: BaseEntity, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; 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) { throw new InternalServerErrorException( `No service found for entity_type "${entityType}"`, ); } // 1️⃣ Get old state const existingEntity = await entityService.getEntityData( entityType, id, loggedInUser, ); if (!existingEntity) { throw new NotFoundException(`No entity found for id "${id}"`); } // 2️⃣ Pre-evaluate criteria const workflows = await this.workflowAutomationEngineService[ 'wfService' ].getActiveRules(entityType, 'UPDATE', loggedInUser); const preUpdateStates: Record = {}; for (const wf of workflows) { preUpdateStates[wf.id] = await this.workflowAutomationEngineService[ 'filterEvaluator' ].evaluateCriteria( entityType, wf.condition_filter_code, existingEntity.id, loggedInUser, ); } // 3️⃣ Update const updatedData = await entityService.updateEntity( entityData, loggedInUser as UserData, ); // 4️⃣ Run workflow automation (pass preUpdateStates) await this.workflowAutomationEngineService.handleEntityEvent( entityType, 'UPDATE', updatedData, loggedInUser, preUpdateStates, ); return updatedData; } @Post('delete/:id') @HttpCode(200) async delete( @Param('id') id: number, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; 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) { throw new InternalServerErrorException( `No service found for entity_type "${entityType}"`, ); } const existingEntity = await entityService.getEntityData( entityType, id, loggedInUser, ); if (!existingEntity) { throw new NotFoundException(`No entity found for id "${id}"`); } return await entityService.deleteEntity(entityType, id, loggedInUser); } }