import { Injectable, NotFoundException } from '@nestjs/common'; import { TenantService } from '../../database/tenant.service'; @Injectable() export class ReserveComponentsService { constructor(private tenant: TenantService) {} async findAll() { return this.tenant.query('SELECT * FROM reserve_components ORDER BY name'); } async findOne(id: string) { const rows = await this.tenant.query('SELECT * FROM reserve_components WHERE id = $1', [id]); if (!rows.length) throw new NotFoundException('Reserve component not found'); return rows[0]; } async create(dto: any) { const rows = await this.tenant.query( `INSERT INTO reserve_components (name, category, description, useful_life_years, remaining_life_years, replacement_cost, current_fund_balance, annual_contribution, condition_rating, last_replacement_date, next_replacement_date) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *`, [dto.name, dto.category, dto.description, dto.useful_life_years, dto.remaining_life_years || 0, dto.replacement_cost, dto.current_fund_balance || 0, dto.annual_contribution || 0, dto.condition_rating || 5, dto.last_replacement_date || null, dto.next_replacement_date || null], ); return rows[0]; } async update(id: string, dto: any) { await this.findOne(id); const rows = await this.tenant.query( `UPDATE reserve_components SET name = COALESCE($2, name), category = COALESCE($3, category), description = COALESCE($4, description), useful_life_years = COALESCE($5, useful_life_years), remaining_life_years = COALESCE($6, remaining_life_years), replacement_cost = COALESCE($7, replacement_cost), current_fund_balance = COALESCE($8, current_fund_balance), annual_contribution = COALESCE($9, annual_contribution), condition_rating = COALESCE($10, condition_rating), last_replacement_date = COALESCE($11, last_replacement_date), next_replacement_date = COALESCE($12, next_replacement_date), updated_at = NOW() WHERE id = $1 RETURNING *`, [id, dto.name, dto.category, dto.description, dto.useful_life_years, dto.remaining_life_years, dto.replacement_cost, dto.current_fund_balance, dto.annual_contribution, dto.condition_rating, dto.last_replacement_date, dto.next_replacement_date], ); return rows[0]; } }