import { Controller, Get, Post, Query, Logger } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiQuery, ApiBadRequestResponse, ApiInternalServerErrorResponse, } from '@nestjs/swagger'; import { IcdService } from './icd.service'; import { SearchIcdDto } from './dto/search-icd.dto'; import { IcdSearchResponseDto, IcdImportResponseDto, IcdStatisticsResponseDto, ErrorResponseDto, } from './dto/icd-response.dto'; @ApiTags('ICD') @Controller('icd') export class IcdController { private readonly logger = new Logger(IcdController.name); constructor(private readonly icdService: IcdService) {} @Post('import') @ApiOperation({ summary: 'Import ICD data from Excel files', description: 'Import ICD-9 and ICD-10 codes from Excel files located in the test directory. This operation will process both ICD files and insert/update the database with the latest codes.', }) @ApiResponse({ status: 200, description: 'ICD data imported successfully', type: IcdImportResponseDto, }) @ApiBadRequestResponse({ description: 'Bad request - Invalid file format or missing files', type: ErrorResponseDto, }) @ApiInternalServerErrorResponse({ description: 'Internal server error during import process', type: ErrorResponseDto, }) async importData(): Promise { try { this.logger.log('Starting ICD data import...'); const result = await this.icdService.importIcdData(); return { success: true, message: 'ICD data imported successfully', data: result, }; } catch (error) { this.logger.error('Error importing ICD data:', error); throw error; } } @Get('search') @ApiOperation({ summary: 'Search ICD codes with filters and pagination', description: 'Search for ICD codes using various filters like category, search term, with pagination support. Returns a paginated list of matching ICD codes.', }) @ApiQuery({ name: 'category', required: false, description: 'Filter by ICD category', enum: ['ICD9', 'ICD10'], example: 'ICD10', }) @ApiQuery({ name: 'search', required: false, description: 'Search term for ICD code or description', example: 'diabetes', }) @ApiQuery({ name: 'page', required: false, description: 'Page number for pagination', example: 1, type: 'number', }) @ApiQuery({ name: 'limit', required: false, description: 'Number of items per page (max 100)', example: 10, type: 'number', }) @ApiResponse({ status: 200, description: 'ICD codes retrieved successfully', type: IcdSearchResponseDto, }) @ApiBadRequestResponse({ description: 'Bad request - Invalid query parameters', type: ErrorResponseDto, }) @ApiInternalServerErrorResponse({ description: 'Internal server error during search', type: ErrorResponseDto, }) async searchIcdCodes( @Query('category') category?: string, @Query('search') search?: string, @Query('page') page?: string, @Query('limit') limit?: string, ): Promise { try { const pageNum = page ? parseInt(page, 10) : 1; const limitNum = limit ? parseInt(limit, 10) : 10; const result = await this.icdService.findIcdCodes( category, search, pageNum, limitNum, ); return { success: true, data: result.data, pagination: { currentPage: result.page, totalPages: result.totalPages, totalItems: result.total, itemsPerPage: result.limit, hasNextPage: result.page < result.totalPages, hasPreviousPage: result.page > 1, }, }; } catch (error) { this.logger.error('Error searching ICD codes:', error); throw error; } } @Get('statistics') @ApiOperation({ summary: 'Get ICD database statistics', description: 'Retrieve statistics about the ICD database including total counts for ICD-9 and ICD-10 codes, and last import information.', }) @ApiResponse({ status: 200, description: 'Statistics retrieved successfully', type: IcdStatisticsResponseDto, }) @ApiInternalServerErrorResponse({ description: 'Internal server error while fetching statistics', type: ErrorResponseDto, }) async getStatistics(): Promise { try { const stats = await this.icdService.getStatistics(); return { success: true, data: stats, }; } catch (error) { this.logger.error('Error getting statistics:', error); throw error; } } }