import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Inject, Param, Post, Query, Req, Request, UseGuards, } from '@nestjs/common'; import { ListMasterService } from '../service/list-master.service'; import { JwtAuthGuard } from 'src/module/auth/guards/jwt.guard'; import { ListMasterItemService } from '../service/list-master-item.service'; import { MessagePattern, Payload } from '@nestjs/microservices'; @Controller('list-master') export class ListMasterController { constructor( @Inject('ListMasterService') private readonly service: ListMasterService, @Inject('ListMasterItemService') private readonly listMasterItemService: ListMasterItemService, ) {} @Post('/getDropdownData/:type') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async getDropdownDataO( @Param('type') type: string, @Query() queryParams: Record, @Request() req, ) { const { inactiveIds, ...params } = queryParams; const inactiveIdsArray = inactiveIds ? inactiveIds.split(',').map((id) => parseInt(id, 10)) : []; const loggedInUser = req.user.userData; return await this.service.getDropdownOptions( type, params, inactiveIdsArray, loggedInUser, ); } // Public/internal endpoint (no JWT required) @Get('/getDropdownDataPublic/:type') @HttpCode(HttpStatus.OK) async getDropdownDataPublic( @Param('type') type: string, @Query() queryParams: Record, ) { const { inactiveIds, loggedInUser: loggedInUserParam, ...params } = queryParams; const inactiveIdsArray = inactiveIds ? inactiveIds.split(',').map((id) => parseInt(id, 10)) : []; // 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 = {}; } return await this.service.getDropdownOptions( type, params, inactiveIdsArray, loggedInUser, true, // publicCall flag set to true ); } @Get('/getDataSourceList/:source') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async getListSourceType( @Req() req: Request & { user: any }, @Param('source') source: string, ) { const loggedInUser = req.user.userData; return await this.listMasterItemService.getListSourceType( loggedInUser, source, ); } // @Post('/getResolvedListMasterItems/:type') // @UseGuards(JwtAuthGuard) // @HttpCode(HttpStatus.OK) // async getResolvedListMasterItems( // @Body() entityData: any, // @Param('type') type: string, // @Req() req: Request & { user: any }, // ) { // const loggedInUser = req.user.userData; // const entityType = type; // return await this.listMasterItemService.getResolvedListMasterItems( // loggedInUser, // entityData, // entityType, // ); // } @Get('/getListMasterItems/:type') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async getListMasterItems( @Param('type') type: string, @Req() req: Request & { user: any }, @Query('search') search?: string, ) { const loggedInUser = req.user.userData; return await this.listMasterItemService.getListMasterItemsByType( type, loggedInUser.enterprise_id, search, ); } @Get('') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async getAllListMasters( @Req() req: Request & { user: any }, @Query('search') search?: string, ) { const loggedInUser = req.user.userData; return await this.service.getAllListMasterByOrganization( loggedInUser.enterprise_id, search, ); } @Post('/upsertListMasterItem/:type') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async upsertListMasterItem( @Param('type') type: string, @Body() item: any, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; return await this.listMasterItemService.upsertListMasterItem( type, item.items, loggedInUser, ); } @Delete('/deleteListMasterItem/:type/:code') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async deleteListMasterItem( @Param('type') type: string, @Param('code') code: string, ): Promise { return await this.listMasterItemService.deleteListMasterItem(type, code); } @Post('getDropdownData') @UseGuards(JwtAuthGuard) @HttpCode(HttpStatus.OK) async getDropdownData( @Query('entity_type') entity_type: string, @Query('attribute_key') attribute_key: string, @Body() body: Record, @Request() req, ) { const loggedInUser = req.user.userData; const authHeader = req.headers.authorization || ''; const token = authHeader.replace('Bearer ', '').trim(); return await this.service.getDropDownData( entity_type, attribute_key, loggedInUser, body ); } @MessagePattern('getDropdownData') async getDropdownDataMS( @Payload() data: { entity_type: string; attribute_key: string; loggedInUser: any; body: Record; }, ) { const { entity_type, attribute_key, loggedInUser, body } = data; return this.service.getDropDownData( entity_type, attribute_key, loggedInUser, body, ); } }