diff --git a/water-api/src/services/meter.service.ts b/water-api/src/services/meter.service.ts index 6e66ccc..6507bf7 100644 --- a/water-api/src/services/meter.service.ts +++ b/water-api/src/services/meter.service.ts @@ -81,7 +81,6 @@ export interface Meter { export interface MeterWithDetails extends Meter { concentrator_name?: string; concentrator_serial?: string; - project_id?: string; project_name?: string; } @@ -124,7 +123,7 @@ export interface CreateMeterInput { serial_number: string; meter_id?: string | null; name: string; - project_id: string; + project_id?: string; // Optional - will be obtained from concentrator if not provided concentrator_id: string; location?: string; type?: string; @@ -345,6 +344,21 @@ export async function getById(id: string): Promise { * Create a new meter */ export async function create(data: CreateMeterInput): Promise { + // If project_id is not provided, get it from the concentrator + let projectId = data.project_id; + if (!projectId) { + const concentratorQuery = await query<{ project_id: string }>( + 'SELECT project_id FROM concentrators WHERE id = $1', + [data.concentrator_id] + ); + + if (concentratorQuery.rows.length === 0) { + throw new Error('Concentrator not found'); + } + + projectId = concentratorQuery.rows[0].project_id; + } + const result = await query( `INSERT INTO meters (serial_number, meter_id, name, project_id, concentrator_id, location, type, status, installation_date) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) @@ -353,7 +367,7 @@ export async function create(data: CreateMeterInput): Promise { data.serial_number, data.meter_id || null, data.name, - data.project_id, + projectId, data.concentrator_id, data.location || null, data.type || 'LORA', @@ -369,6 +383,19 @@ export async function create(data: CreateMeterInput): Promise { * Update an existing meter */ export async function update(id: string, data: UpdateMeterInput): Promise { + // If concentrator_id is being updated and project_id is not provided, + // automatically get project_id from the new concentrator + if (data.concentrator_id !== undefined && data.project_id === undefined) { + const concentratorQuery = await query<{ project_id: string }>( + 'SELECT project_id FROM concentrators WHERE id = $1', + [data.concentrator_id] + ); + + if (concentratorQuery.rows.length > 0) { + data.project_id = concentratorQuery.rows[0].project_id; + } + } + const updates: string[] = []; const params: unknown[] = []; let paramIndex = 1; diff --git a/water-api/src/validators/meter.validator.ts b/water-api/src/validators/meter.validator.ts index 8ccfd75..5169fdf 100644 --- a/water-api/src/validators/meter.validator.ts +++ b/water-api/src/validators/meter.validator.ts @@ -52,8 +52,9 @@ export const createMeterSchema = z.object({ .min(1, 'Name cannot be empty') .max(255, 'Name must be at most 255 characters'), project_id: z - .string({ required_error: 'Project ID is required' }) - .regex(uuidRegex, 'Project ID must be a valid UUID'), + .string() + .regex(uuidRegex, 'Project ID must be a valid UUID') + .optional(), // Optional - will be obtained from concentrator if not provided concentrator_id: z .string({ required_error: 'Concentrator ID is required' }) .regex(uuidRegex, 'Concentrator ID must be a valid UUID'),