import { Body, Controller, Get, Param, Post, Query, Req, BadRequestException, NotFoundException, HttpCode, HttpStatus, UseGuards, } from '@nestjs/common'; import { Request } from 'express'; import { EntityDynamicService } from '../service/entity-dynamic.service'; import { JwtAuthGuard } from '../../auth/guards/jwt.guard'; @UseGuards(JwtAuthGuard) @Controller('dynamic-entity') export class EntityDynamicController { constructor(private readonly entityDynamicService: EntityDynamicService) {} @Post('create') @HttpCode(HttpStatus.OK) async createEntity( @Body() data: Record, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; if (!entityType) { throw new BadRequestException(`Query param "entity_type" is required`); } return await this.entityDynamicService.createEntityWithRelation( entityType, data, loggedInUser, ); } @Post('update/:id') @HttpCode(HttpStatus.OK) async updateEntity( @Param('id') id: number, @Body() data: Record, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; if (!entityType) { throw new BadRequestException(`Query param "entity_type" is required`); } const existingEntity = await this.entityDynamicService.getEntity( entityType, id, loggedInUser, ); if (!existingEntity) { throw new NotFoundException(`No entity found for id "${id}"`); } return await this.entityDynamicService.updateEntityWithRelations( entityType, id, data, loggedInUser, ); } @Get('get/:id') @HttpCode(HttpStatus.OK) async getEntity( @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 param "entity_type" is required`); } const entity = await this.entityDynamicService.getEntityWithRelation( entityType, id, loggedInUser, ); if (!entity) { throw new NotFoundException(`No entity found for id "${id}"`); } return entity; } @Get('getDropdownList') async getAttributesDropdownList( @Req() req: Request & { user: any }, @Query('appcode') appcode?: string, ) { const loggedInUser = req.user.userData; return await this.entityDynamicService.getEntitiesDropdownList( loggedInUser, appcode, ); } @Get('getResolvedEntity/:id') async getResolvedEntity( @Param('id') id: number, @Query('entity_type') entityType: string, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; return await this.entityDynamicService.getResolvedEntity( id, entityType, loggedInUser, ); } }