diff --git a/backend/src/features/maintenance/domain/maintenance.service.ts b/backend/src/features/maintenance/domain/maintenance.service.ts index 7990ac4..da04b7e 100644 --- a/backend/src/features/maintenance/domain/maintenance.service.ts +++ b/backend/src/features/maintenance/domain/maintenance.service.ts @@ -9,7 +9,8 @@ import type { MaintenanceRecordResponse, MaintenanceScheduleResponse, MaintenanceCategory, - ScheduleType + ScheduleType, + MaintenanceCostStats } from './maintenance.types'; import { validateSubtypes } from './maintenance.types'; import { MaintenanceRepository } from '../data/maintenance.repository'; @@ -63,6 +64,19 @@ export class MaintenanceService { return records.map(r => this.toRecordResponse(r)); } + async getVehicleMaintenanceCosts(vehicleId: string, userId: string): Promise { + const records = await this.repo.findRecordsByVehicleId(vehicleId, userId); + const totalCost = records.reduce((sum, r) => { + if (r.cost === null || r.cost === undefined) return sum; + const cost = Number(r.cost); + if (isNaN(cost)) { + throw new Error(`Invalid cost value for maintenance record ${r.id}`); + } + return sum + cost; + }, 0); + return { totalCost, recordCount: records.length }; + } + async updateRecord(userId: string, id: string, patch: UpdateMaintenanceRecordRequest): Promise { const existing = await this.repo.findRecordById(id, userId); if (!existing) return null; diff --git a/backend/src/features/maintenance/domain/maintenance.types.ts b/backend/src/features/maintenance/domain/maintenance.types.ts index caeeeb5..ce85ac1 100644 --- a/backend/src/features/maintenance/domain/maintenance.types.ts +++ b/backend/src/features/maintenance/domain/maintenance.types.ts @@ -162,6 +162,12 @@ export interface MaintenanceRecordResponse extends MaintenanceRecord { subtypeCount: number; } +// TCO aggregation stats +export interface MaintenanceCostStats { + totalCost: number; + recordCount: number; +} + export interface MaintenanceScheduleResponse extends MaintenanceSchedule { subtypeCount: number; isDueSoon?: boolean;