Project id fix for meter edit
This commit is contained in:
@@ -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<MeterWithDetails | null> {
|
||||
* Create a new meter
|
||||
*/
|
||||
export async function create(data: CreateMeterInput): Promise<Meter> {
|
||||
// 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<Meter>(
|
||||
`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<Meter> {
|
||||
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<Meter> {
|
||||
* Update an existing meter
|
||||
*/
|
||||
export async function update(id: string, data: UpdateMeterInput): Promise<Meter | null> {
|
||||
// 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;
|
||||
|
||||
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user