Relation meter with project
This commit is contained in:
@@ -2,13 +2,14 @@ import { query } from '../config/database';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Meter interface matching database schema
|
* Meter interface matching database schema
|
||||||
* Meters are linked to concentrators (not directly to projects)
|
* Meters are linked to both projects and concentrators
|
||||||
*/
|
*/
|
||||||
export interface Meter {
|
export interface Meter {
|
||||||
id: string;
|
id: string;
|
||||||
serial_number: string;
|
serial_number: string;
|
||||||
meter_id: string | null;
|
meter_id: string | null;
|
||||||
name: string;
|
name: string;
|
||||||
|
project_id: string;
|
||||||
concentrator_id: string;
|
concentrator_id: string;
|
||||||
location: string | null;
|
location: string | null;
|
||||||
type: string;
|
type: string;
|
||||||
@@ -123,6 +124,7 @@ export interface CreateMeterInput {
|
|||||||
serial_number: string;
|
serial_number: string;
|
||||||
meter_id?: string | null;
|
meter_id?: string | null;
|
||||||
name: string;
|
name: string;
|
||||||
|
project_id: string;
|
||||||
concentrator_id: string;
|
concentrator_id: string;
|
||||||
location?: string;
|
location?: string;
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -173,6 +175,7 @@ export interface UpdateMeterInput {
|
|||||||
serial_number?: string;
|
serial_number?: string;
|
||||||
meter_id?: string | null;
|
meter_id?: string | null;
|
||||||
name?: string;
|
name?: string;
|
||||||
|
project_id?: string;
|
||||||
concentrator_id?: string;
|
concentrator_id?: string;
|
||||||
location?: string;
|
location?: string;
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -343,13 +346,14 @@ export async function getById(id: string): Promise<MeterWithDetails | null> {
|
|||||||
*/
|
*/
|
||||||
export async function create(data: CreateMeterInput): Promise<Meter> {
|
export async function create(data: CreateMeterInput): Promise<Meter> {
|
||||||
const result = await query<Meter>(
|
const result = await query<Meter>(
|
||||||
`INSERT INTO meters (serial_number, meter_id, name, concentrator_id, location, type, status, installation_date)
|
`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)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
RETURNING *`,
|
RETURNING *`,
|
||||||
[
|
[
|
||||||
data.serial_number,
|
data.serial_number,
|
||||||
data.meter_id || null,
|
data.meter_id || null,
|
||||||
data.name,
|
data.name,
|
||||||
|
data.project_id,
|
||||||
data.concentrator_id,
|
data.concentrator_id,
|
||||||
data.location || null,
|
data.location || null,
|
||||||
data.type || 'LORA',
|
data.type || 'LORA',
|
||||||
@@ -387,6 +391,12 @@ export async function update(id: string, data: UpdateMeterInput): Promise<Meter
|
|||||||
paramIndex++;
|
paramIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.project_id !== undefined) {
|
||||||
|
updates.push(`project_id = $${paramIndex}`);
|
||||||
|
params.push(data.project_id);
|
||||||
|
paramIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
if (data.concentrator_id !== undefined) {
|
if (data.concentrator_id !== undefined) {
|
||||||
updates.push(`concentrator_id = $${paramIndex}`);
|
updates.push(`concentrator_id = $${paramIndex}`);
|
||||||
params.push(data.concentrator_id);
|
params.push(data.concentrator_id);
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ export const createMeterSchema = z.object({
|
|||||||
.string({ required_error: 'Name is required' })
|
.string({ required_error: 'Name is required' })
|
||||||
.min(1, 'Name cannot be empty')
|
.min(1, 'Name cannot be empty')
|
||||||
.max(255, 'Name must be at most 255 characters'),
|
.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'),
|
||||||
concentrator_id: z
|
concentrator_id: z
|
||||||
.string({ required_error: 'Concentrator ID is required' })
|
.string({ required_error: 'Concentrator ID is required' })
|
||||||
.regex(uuidRegex, 'Concentrator ID must be a valid UUID'),
|
.regex(uuidRegex, 'Concentrator ID must be a valid UUID'),
|
||||||
@@ -150,6 +153,10 @@ export const updateMeterSchema = z.object({
|
|||||||
.min(1, 'Name cannot be empty')
|
.min(1, 'Name cannot be empty')
|
||||||
.max(255, 'Name must be at most 255 characters')
|
.max(255, 'Name must be at most 255 characters')
|
||||||
.optional(),
|
.optional(),
|
||||||
|
project_id: z
|
||||||
|
.string()
|
||||||
|
.regex(uuidRegex, 'Project ID must be a valid UUID')
|
||||||
|
.optional(),
|
||||||
concentrator_id: z
|
concentrator_id: z
|
||||||
.string()
|
.string()
|
||||||
.regex(uuidRegex, 'Concentrator ID must be a valid UUID')
|
.regex(uuidRegex, 'Concentrator ID must be a valid UUID')
|
||||||
|
|||||||
Reference in New Issue
Block a user