feat(bookings): add agent-driven clearance flow with forwarder/transit-agent panels and BookingClearedByAgent migration

This commit is contained in:
marshal
2026-09-08 08:49:33 +00:00
parent 850e0753d2
commit 7af3ded7d7
14 changed files with 1716 additions and 57 deletions

View File

@@ -0,0 +1,45 @@
import { MigrationInterface, QueryRunner } from "typeorm";
/**
* A without-customs booking the customer handed to a registered clearing agent
* (a freight forwarder on the platform) now runs the phased clearance workflow,
* with the forwarder doing the GL Ethiopia steps and the Djibouti agent it
* names doing the Djibouti steps. The flag marks such a booking so every
* "phased customs" gate admits it alongside GL-cleared customs bookings.
*/
export class BookingClearedByAgent3970000000000 implements MigrationInterface {
name = "BookingClearedByAgent3970000000000";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.bookings
ADD COLUMN IF NOT EXISTS cleared_by_agent boolean NOT NULL DEFAULT false
`);
// Bookings already handed to a registered Ethiopian agent before the flag
// existed join the workflow too. Their milestones are seeded lazily the
// first time the clearance view is read (BookingClearanceService), since
// the catalog lives in code.
await queryRunner.query(`
UPDATE freight.bookings b
SET cleared_by_agent = true
WHERE b.customs_clearing_enabled = false
AND b.contract_id IS NOT NULL
AND b.deleted_at IS NULL
AND EXISTS (
SELECT 1
FROM freight.transit_assignments ta
JOIN freight.transit_agents a ON a.id = ta.transit_agent_id
WHERE ta.booking_id = b.id
AND ta.deleted_at IS NULL
AND a.deleted_at IS NULL
AND a.country = 'ET'
)
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.bookings DROP COLUMN IF EXISTS cleared_by_agent
`);
}
}