mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 21:15:41 +00:00
fix issue
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
|
||||
import { BookingWagonCancellationService } from './booking-wagon-cancellation.service';
|
||||
import {
|
||||
bulkTonWagonsRequired,
|
||||
bulkTonsPerWagonFor,
|
||||
} from '../train-scheduling/train-capacity.util';
|
||||
|
||||
/**
|
||||
* Sizing of a bulk quantity cut (no DB touched on this branch): a whole-booking
|
||||
@@ -107,3 +111,48 @@ describe('BookingWagonCancellationService.rebook (odd-20ft consolidation)', () =
|
||||
).rejects.toThrow(/already shares a wagon/i);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* A NUMBER_OF_WAGONS booking pins its count in `bulkRequestedWagons`, and
|
||||
* bulkTonWagonsRequired honours that verbatim. Partial cancel must shrink it
|
||||
* alongside wagonsRequired/cargoTotalWeightVgm — left stale, the booking
|
||||
* re-inflates to its pre-cancel count on the next allocation and each wagon
|
||||
* carries tons / stale-count instead of the real even share.
|
||||
*/
|
||||
describe('partial cancel of a NUMBER_OF_WAGONS bulk booking', () => {
|
||||
// 980T over 14 wagons (70T each), 2 wagons cancelled.
|
||||
const before = { freightType: 'BULK', cargoTotalWeightVgm: 980, bulkRequestedWagons: 14 };
|
||||
const droppedWeight = 140;
|
||||
const wagonsCancelled = 2;
|
||||
|
||||
// The decrement applied in applyPaidCut's booking update.
|
||||
const after = {
|
||||
...before,
|
||||
cargoTotalWeightVgm: before.cargoTotalWeightVgm - droppedWeight,
|
||||
bulkRequestedWagons: Math.max(
|
||||
0,
|
||||
Math.floor(before.bulkRequestedWagons - wagonsCancelled),
|
||||
),
|
||||
};
|
||||
|
||||
it('reallocates at the reduced count, not the pre-cancel one', () => {
|
||||
expect(bulkTonWagonsRequired(before, undefined, 'nw5', 70)).toBe(14);
|
||||
expect(bulkTonWagonsRequired(after, undefined, 'nw5', 70)).toBe(12);
|
||||
});
|
||||
|
||||
it('keeps tons-per-wagon at the real even share', () => {
|
||||
// Stale count would spread 840T over 14 wagons → 60T each.
|
||||
expect(bulkTonsPerWagonFor(after, undefined, 'nw5', 70)).toBe(70);
|
||||
});
|
||||
|
||||
it('cancelling every wagon leaves no requested count behind', () => {
|
||||
const all = Math.max(0, Math.floor(before.bulkRequestedWagons - 14));
|
||||
expect(all).toBe(0);
|
||||
expect(bulkTonWagonsRequired(
|
||||
{ ...before, cargoTotalWeightVgm: 0, bulkRequestedWagons: all },
|
||||
undefined,
|
||||
'nw5',
|
||||
70,
|
||||
)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -704,8 +704,21 @@ export class BookingWagonCancellationService {
|
||||
Number(booking.wagonsRequired ?? 0) - Number(row.wagonsCancelled),
|
||||
);
|
||||
const isFull = wagonsLeft <= 0;
|
||||
// NUMBER_OF_WAGONS bookings pin their count in bulkRequestedWagons, which
|
||||
// bulkTonWagonsRequired honours verbatim. Left stale it re-inflates the
|
||||
// booking to its pre-cancel count on the next allocation (and shrinks
|
||||
// tons-per-wagon to tons / stale-count), so shrink it with the cut.
|
||||
const requestedWagonsLeft = booking.bulkRequestedWagons
|
||||
? Math.max(
|
||||
0,
|
||||
Math.floor(Number(booking.bulkRequestedWagons) - Number(row.wagonsCancelled)),
|
||||
)
|
||||
: null;
|
||||
await manager.getRepository(Booking).update(booking.id, {
|
||||
wagonsRequired: Math.max(0, wagonsLeft),
|
||||
...(requestedWagonsLeft !== null
|
||||
? { bulkRequestedWagons: requestedWagonsLeft }
|
||||
: {}),
|
||||
cargoTotalWeightVgm: Math.max(
|
||||
0,
|
||||
round3(Number(booking.cargoTotalWeightVgm) - droppedWeight),
|
||||
|
||||
Reference in New Issue
Block a user