shankar 8630a3e2c5 Fixed React backend startup issue.
Updated SQL script to correct procedure and column sizes.
Moved SQL scripts to the project root folder (outside React frontend).
Resolved backend container dependency issue to ensure MySQL is up before starting React backend.
Moved common values to .env file in the project root.
Updated React backend and MySQL ports to use default values.
Added code to get last study received, containers status and updated into DB.
2024-11-22 13:44:03 +05:30

211 lines
6.4 KiB
TypeScript

// src/controllers/DicomStudyController.ts
import { Request, Response, NextFunction } from 'express';
import { DicomStudyService } from '../services/DicomStudyService';
import logger from '../utils/logger';
import { Pool } from 'mysql2/promise';
interface ApiError {
message: string;
code?: string;
stack?: string;
}
export class DicomStudyController {
private service: DicomStudyService;
constructor(pool:Pool) {
this.service = new DicomStudyService(pool);
}
private handleError(error: unknown, message: string): ApiError {
const apiError: ApiError = {
message: message
};
if (error instanceof Error) {
logger.error(`${message}: ${error.message}`);
if (process.env.NODE_ENV === 'development') {
apiError.message = error.message;
apiError.stack = error.stack;
}
} else {
logger.error(`${message}: Unknown error type`, error);
}
return apiError;
}
getAllStudies = async (req: Request, res: Response, next: NextFunction) => {
try {
const studies = await this.service.getAllStudies();
res.json(studies);
} catch (error) {
const apiError = this.handleError(error, 'Failed to fetch studies');
res.status(500).json({ error: apiError });
}
};
getStudyById = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const study = await this.service.getStudyById(id);
if (!study) {
return res.status(404).json({ error: 'Study not found' });
}
res.json(study);
} catch (error) {
const apiError = this.handleError(error, `Failed to fetch study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
getStudiesByRouterId = async (req: Request, res: Response, next: NextFunction) => {
try {
const routerId = req.params.routerId;
if (!routerId || typeof routerId !== 'string') {
return res.status(400).json({ error: 'Invalid router ID' });
}
const studies = await this.service.getStudiesByRouterId(routerId);
res.json(studies);
} catch (error) {
const apiError = this.handleError(error, `Failed to fetch studies for router ${req.params.routerId}`);
// If router not found, return 404
if (error instanceof Error && error.message.includes('Invalid router_id')) {
return res.status(404).json({ error: 'Router not found' });
}
res.status(500).json({ error: apiError });
}
};
createStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const requiredFields = [
'router_id',
'study_instance_uid',
'patient_id',
'patient_name',
'accession_number',
'study_date',
'modality',
'series_instance_uid',
'study_status_code',
'association_id'
];
const missingFields = requiredFields.filter(field => !req.body[field]);
if (missingFields.length > 0) {
return res.status(400).json({
error: 'Missing required fields',
missingFields
});
}
// Validate router_id format if needed
if (typeof req.body.router_id !== 'string') {
return res.status(400).json({
error: 'Invalid router_id format',
details: 'router_id must be a string'
});
}
const study = await this.service.createStudy(req.body);
res.status(201).json(study);
} catch (error) {
const apiError = this.handleError(error, 'Failed to create study');
// Handle specific error cases
if (error instanceof Error) {
if (error.message.includes('Missing required field')) {
return res.status(400).json({ error: apiError });
}
if (error.message.includes('Invalid router_id')) {
return res.status(404).json({ error: 'Router not found' });
}
if (error.message.includes('Invalid study status code')) {
return res.status(400).json({ error: apiError });
}
if (error.message.includes('Invalid study date format')) {
return res.status(400).json({ error: apiError });
}
}
res.status(500).json({ error: apiError });
}
};
updateStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const study = await this.service.updateStudy(id, req.body);
if (!study) {
return res.status(404).json({ error: 'Study not found' });
}
res.json(study);
} catch (error) {
const apiError = this.handleError(error, `Failed to update study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
deleteStudy = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid study ID' });
}
const success = await this.service.deleteStudy(id);
if (!success) {
return res.status(404).json({ error: 'Study not found' });
}
res.status(204).send();
} catch (error) {
const apiError = this.handleError(error, `Failed to delete study ${req.params.id}`);
res.status(500).json({ error: apiError });
}
};
searchStudies = async (req: Request, res: Response, next: NextFunction) => {
try {
const { startDate, endDate, modality, patientName } = req.query;
if (startDate && typeof startDate !== 'string') {
return res.status(400).json({
error: 'Invalid startDate format',
expected: 'YYYY-MM-DD'
});
}
if (endDate && typeof endDate !== 'string') {
return res.status(400).json({
error: 'Invalid endDate format',
expected: 'YYYY-MM-DD'
});
}
const studies = await this.service.searchStudies({
startDate: startDate as string,
endDate: endDate as string,
modality: modality as string,
patientName: patientName as string
});
res.json(studies);
} catch (error) {
const apiError = this.handleError(error, 'Failed to search studies');
res.status(500).json({ error: apiError });
}
};
}