Files
edr-platform/apps/edr-freight-api/src/modules/maintenance/maintenance.repository.ts
natib21 e59a77b859 feat: add maintenance tracking module foundation
Entities:
- MaintenanceSchedule: track preventive/corrective maintenance
- MaintenanceCost: record actual maintenance expenses

DTOs:
- CreateMaintenanceScheduleDto: schedule maintenance
- CreateMaintenanceCostDto: log costs
- UpdateMaintenanceScheduleDto: mark complete/adjust cost

Repository:
- getUpcomingMaintenance(): find due maintenance
- getMaintenanceCosts(): historical costs by date
- getTotalMaintenanceCost(): aggregate spending

Also fixed fuel-consumption.entity.ts: totalDistanceKm default 0

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-06-30 12:28:00 +00:00

51 lines
1.8 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseRepository } from '@edr/api-common';
import { Repository, Between } from 'typeorm';
import { MaintenanceSchedule, MaintenanceStatus } from './entities/maintenance-schedule.entity';
import { MaintenanceCost } from './entities/maintenance-cost.entity';
@Injectable()
export class MaintenanceRepository extends BaseRepository<MaintenanceSchedule> {
constructor(
@InjectRepository(MaintenanceSchedule)
private readonly scheduleRepository: Repository<MaintenanceSchedule>,
@InjectRepository(MaintenanceCost)
private readonly costRepository: Repository<MaintenanceCost>,
) {
super(scheduleRepository);
}
async getUpcomingMaintenance(vehicleId: string, daysAhead: number = 30) {
const futureDate = new Date(Date.now() + daysAhead * 24 * 60 * 60 * 1000);
return this.scheduleRepository.find({
where: {
vehicleId,
scheduledDate: Between(new Date(), futureDate),
status: MaintenanceStatus.SCHEDULED,
},
order: { scheduledDate: 'ASC' },
});
}
async getMaintenanceCosts(vehicleId: string, startDate: Date, endDate: Date) {
return this.costRepository.find({
where: {
vehicleId,
incurredDate: Between(startDate, endDate),
},
order: { incurredDate: 'DESC' },
});
}
async getTotalMaintenanceCost(vehicleId: string, startDate: Date, endDate: Date) {
const result = await this.costRepository
.createQueryBuilder()
.select('SUM(cost_amount)', 'total')
.where('vehicle_id = :vehicleId', { vehicleId })
.andWhere('incurred_date BETWEEN :startDate AND :endDate', { startDate, endDate })
.getRawOne();
return result?.total || 0;
}
}