feat: implement KM-based maintenance scheduling

Add maintenance intervals configuration to track maintenance by kilometers
driven. When a maintenance is marked COMPLETED, automatically calculate and
schedule the next maintenance based on interval + current odometer reading.

Features:
- MaintenanceInterval entity: stores KM/day intervals per vehicle & type
- scheduleNextMaintenance(): creates next SCHEDULED item after completion
- nextDueKm field: tracks when next maintenance is due (in kilometers)
- getDueBoard() queries already support KM-based tracking

Maintenance now "marches forward" based on distance driven, not just dates.
Each vehicle type can have different intervals (e.g., oil every 10k km, tires 50k km).

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Hagernesh
2026-07-23 07:04:40 +00:00
parent b756ae2f1c
commit fa59ccfc95
5 changed files with 275 additions and 2 deletions

View File

@@ -0,0 +1,103 @@
import { MigrationInterface, QueryRunner, Table, TableIndex, TableForeignKey, TableUnique } from 'typeorm';
export class AddMaintenanceIntervals2800000000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'maintenance_intervals',
schema: 'freight',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'gen_random_uuid()',
},
{
name: 'vehicle_id',
type: 'uuid',
isNullable: false,
},
{
name: 'maintenance_type',
type: 'varchar',
isNullable: false,
},
{
name: 'interval_km',
type: 'numeric',
precision: 14,
scale: 2,
isNullable: true,
},
{
name: 'interval_days',
type: 'integer',
isNullable: true,
},
{
name: 'description',
type: 'text',
isNullable: true,
},
{
name: 'is_active',
type: 'boolean',
default: true,
},
{
name: 'created_at',
type: 'timestamptz',
default: 'now()',
},
{
name: 'updated_at',
type: 'timestamptz',
default: 'now()',
},
{
name: 'deleted_at',
type: 'timestamptz',
isNullable: true,
},
],
}),
);
// Add indexes
await queryRunner.createIndex(
new Table({ name: 'maintenance_intervals', schema: 'freight' }),
new TableIndex({
name: 'IDX_maintenance_intervals_vehicle_type',
columnNames: ['vehicle_id', 'maintenance_type'],
}),
);
// Add unique constraint
await queryRunner.createUniqueConstraint(
'maintenance_intervals',
new TableUnique({
name: 'UQ_maintenance_intervals_vehicle_type',
columnNames: ['vehicle_id', 'maintenance_type'],
}),
);
// Add foreign key
await queryRunner.createForeignKey(
'maintenance_intervals',
new TableForeignKey({
name: 'FK_maintenance_intervals_vehicle',
columnNames: ['vehicle_id'],
referencedSchema: 'freight',
referencedTableName: 'vehicles',
referencedColumnNames: ['id'],
onDelete: 'CASCADE',
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('maintenance_intervals', true, true, true);
}
}