import { Request, Response } from 'express'; import * as readingService from '../services/reading.service'; /** * GET /readings * List all readings with pagination and filtering */ export async function getAll(req: Request, res: Response): Promise { try { const { page = '1', pageSize = '50', meter_id, concentrator_id, project_id, start_date, end_date, reading_type, } = req.query; const filters: readingService.ReadingFilters = {}; if (meter_id) filters.meter_id = meter_id as string; if (concentrator_id) filters.concentrator_id = concentrator_id as string; if (project_id) filters.project_id = project_id as string; if (start_date) filters.start_date = start_date as string; if (end_date) filters.end_date = end_date as string; if (reading_type) filters.reading_type = reading_type as string; const pagination = { page: parseInt(page as string, 10), pageSize: Math.min(parseInt(pageSize as string, 10), 100), // Max 100 per page }; const result = await readingService.getAll(filters, pagination); res.status(200).json({ success: true, data: result.data, pagination: result.pagination, }); } catch (error) { console.error('Error fetching readings:', error); res.status(500).json({ success: false, error: 'Internal server error', }); } } /** * GET /readings/:id * Get a single reading by ID */ export async function getById(req: Request, res: Response): Promise { try { const { id } = req.params; const reading = await readingService.getById(id); if (!reading) { res.status(404).json({ success: false, error: 'Reading not found', }); return; } res.status(200).json({ success: true, data: reading, }); } catch (error) { console.error('Error fetching reading:', error); res.status(500).json({ success: false, error: 'Internal server error', }); } } /** * POST /readings * Create a new reading */ export async function create(req: Request, res: Response): Promise { try { const data = req.body as readingService.CreateReadingInput; const reading = await readingService.create(data); res.status(201).json({ success: true, data: reading, }); } catch (error) { console.error('Error creating reading:', error); res.status(500).json({ success: false, error: 'Internal server error', }); } } /** * DELETE /readings/:id * Delete a reading */ export async function deleteReading(req: Request, res: Response): Promise { try { const { id } = req.params; const deleted = await readingService.deleteReading(id); if (!deleted) { res.status(404).json({ success: false, error: 'Reading not found', }); return; } res.status(200).json({ success: true, data: { message: 'Reading deleted successfully' }, }); } catch (error) { console.error('Error deleting reading:', error); res.status(500).json({ success: false, error: 'Internal server error', }); } } /** * GET /readings/summary * Get consumption summary statistics */ export async function getSummary(req: Request, res: Response): Promise { try { const { project_id } = req.query; const summary = await readingService.getConsumptionSummary( project_id as string | undefined ); res.status(200).json({ success: true, data: summary, }); } catch (error) { console.error('Error fetching summary:', error); res.status(500).json({ success: false, error: 'Internal server error', }); } }