add pg vector and embed

This commit is contained in:
2025-08-22 19:34:54 +07:00
parent 21567a0a7c
commit b77beb2d85
27 changed files with 5273 additions and 216 deletions

View File

@@ -1,7 +1,22 @@
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);
@@ -9,7 +24,25 @@ export class IcdController {
constructor(private readonly icdService: IcdService) {}
@Post('import')
async importData() {
@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<IcdImportResponseDto> {
try {
this.logger.log('Starting ICD data import...');
const result = await this.icdService.importIcdData();
@@ -20,21 +53,62 @@ export class IcdController {
};
} catch (error) {
this.logger.error('Error importing ICD data:', error);
return {
success: false,
message: 'Failed to import ICD data',
error: error.message,
};
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<IcdSearchResponseDto> {
try {
const pageNum = page ? parseInt(page, 10) : 1;
const limitNum = limit ? parseInt(limit, 10) : 10;
@@ -48,20 +122,38 @@ export class IcdController {
return {
success: true,
...result,
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);
return {
success: false,
message: 'Failed to search ICD codes',
error: error.message,
};
throw error;
}
}
@Get('statistics')
async getStatistics() {
@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<IcdStatisticsResponseDto> {
try {
const stats = await this.icdService.getStatistics();
return {
@@ -70,11 +162,7 @@ export class IcdController {
};
} catch (error) {
this.logger.error('Error getting statistics:', error);
return {
success: false,
message: 'Failed to get statistics',
error: error.message,
};
throw error;
}
}
}