Files
edr-platform/apps/edr-freight-api/src/modules/contracts/clearance-milestone.risk.spec.ts
Marshal cd8fb2b321 enhance gate pass and freight payment handling in train scheduling
- Updated the logic in  to ensure that a booking only earns its gate pass once the freight charges are settled.
- Added logging for bookings that have not settled freight payment when securing gate passes.
- Modified seeders to ensure that bookings have associated company profiles to prevent data inconsistencies.
- Updated freight permissions to include new clearance actions for bookings.
- Enhanced the UI to reflect changes in the clearance process, including new shipment request pages and improved status handling in the clearance action panel.
- Adjusted the contract clearance list to accommodate both customs contracts and shipment bookings.
- Improved the handling of GENERAL contracts in various components to ensure proper booking flow and visibility.
2026-07-09 07:19:36 +00:00

69 lines
2.5 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
import type { DataSource } from 'typeorm';
import { ClearanceMilestoneService } from './clearance-milestone.service';
import type { ClearanceMilestone } from './entities/clearance-milestone.entity';
type Status = 'PENDING' | 'COMPLETED' | 'SKIPPED';
/**
* Risk assignment is gated on the T1 being closed (catalog order
* T1_CLOSED → RISK_ASSIGNED): customs cannot rate cargo still under transit.
*/
function makeService(t1Status: Status | 'MISSING') {
const rows = new Map<string, ClearanceMilestone>();
if (t1Status !== 'MISSING') {
rows.set('T1_CLOSED', { milestoneCode: 'T1_CLOSED', status: t1Status } as ClearanceMilestone);
}
const risk = { milestoneCode: 'RISK_ASSIGNED', status: 'PENDING' } as ClearanceMilestone;
rows.set('RISK_ASSIGNED', risk);
const repo = {
findOne: jest.fn(({ where }: { where: { milestoneCode: string } }) =>
Promise.resolve(rows.get(where.milestoneCode) ?? null),
),
save: jest.fn((m: ClearanceMilestone) => Promise.resolve(m)),
};
const dataSource = { getRepository: () => repo } as unknown as DataSource;
return { service: new ClearanceMilestoneService(dataSource), repo, risk };
}
describe('ClearanceMilestoneService.assignRisk', () => {
it('rejects the assignment while the T1 is still open', async () => {
const { service, repo } = makeService('PENDING');
await expect(service.assignRisk('b-1', 'GREEN')).rejects.toBeInstanceOf(
BadRequestException,
);
expect(repo.save).not.toHaveBeenCalled();
});
it('rejects the assignment when the booking has no T1_CLOSED milestone', async () => {
const { service, repo } = makeService('MISSING');
await expect(service.assignRisk('b-1', 'GREEN')).rejects.toBeInstanceOf(
BadRequestException,
);
expect(repo.save).not.toHaveBeenCalled();
});
it('assigns the risk level once the T1 is closed', async () => {
const { service, risk } = makeService('COMPLETED');
const saved = await service.assignRisk('b-1', 'RED', 'user-1');
expect(saved.status).toBe('COMPLETED');
expect(saved.metadata?.riskLevel).toBe('RED');
expect(risk.triggeredByUserId).toBe('user-1');
});
it('assigns the risk level when the T1 step was skipped', async () => {
const { service } = makeService('SKIPPED');
const saved = await service.assignRisk('b-1', 'YELLOW');
expect(saved.status).toBe('COMPLETED');
expect(saved.metadata?.riskLevel).toBe('YELLOW');
});
});