mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 19:58:11 +00:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user