mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 21:48:18 +00:00
feat(empty-return-requests): customer-requested empty return, priced and paid
A booking sold WITHOUT the return service had no way to send its empties back: the containers were the customer's problem and nothing in the system priced, billed or planned the movement. Adds the request flow end to end. The customer opens the booking, says how many containers are coming back and types each number; the request lands in a new backoffice queue (Empty Return Requests). Approval prices it off the same live WITH_RETURN route rate the rule engine bills when the service IS bought up front — per container, converted to birr, and overridable by the reviewer — and issues the invoice there and then. Payment settles through the normal invoice path, whose `empty_return_request.invoice.paid` event moves the request to PAID; the customer then books the return date and the truck. Scheduled requests surface on Container Returns as Planned Empty Returns, where confirming the arrival records the containers through the existing empty-container-return flow — which in turn closes the request once its last container is in. Container freight only, never a booking that already ships WITH_RETURN, and only from IN_TRANSIT onward: the empty comes back after delivery, so the option has to outlive ARRIVED. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
/**
|
||||
* Customer-initiated empty container return, for a booking that did NOT buy
|
||||
* the return service up front. The customer names the containers coming back,
|
||||
* operations approves and prices it off the contract's WITH_RETURN rate, the
|
||||
* customer pays that invoice and then books the date and truck. The empty
|
||||
* itself is still recorded through `empty_container_returns` when the truck
|
||||
* actually arrives — this table only carries the request up to that point.
|
||||
*/
|
||||
export class EmptyReturnRequests3840000000000 implements MigrationInterface {
|
||||
name = 'EmptyReturnRequests3840000000000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE IF NOT EXISTS freight.empty_return_requests (
|
||||
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
booking_id uuid NOT NULL,
|
||||
company_id uuid,
|
||||
status varchar(30) NOT NULL DEFAULT 'SUBMITTED',
|
||||
container_numbers text[] NOT NULL DEFAULT '{}',
|
||||
container_count smallint NOT NULL DEFAULT 0,
|
||||
quoted_unit_amount numeric(14,2),
|
||||
quoted_total_amount numeric(14,2),
|
||||
currency varchar(8),
|
||||
invoice_id uuid,
|
||||
paid_at timestamptz,
|
||||
requested_return_date date,
|
||||
truck_plate_number varchar(32),
|
||||
truck_driver_name varchar(120),
|
||||
truck_type varchar(60),
|
||||
scheduled_at timestamptz,
|
||||
submitted_by_user_id uuid,
|
||||
submitted_at timestamptz NOT NULL DEFAULT now(),
|
||||
reviewed_by_staff_id uuid,
|
||||
reviewed_at timestamptz,
|
||||
rejection_reason text,
|
||||
completed_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
deleted_at timestamptz
|
||||
)
|
||||
`);
|
||||
|
||||
await queryRunner.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_empty_return_requests_booking
|
||||
ON freight.empty_return_requests (booking_id)
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_empty_return_requests_status
|
||||
ON freight.empty_return_requests (status)
|
||||
`);
|
||||
|
||||
// A container number may only be owed back once at a time. That guard is
|
||||
// per array element, so it lives in the service (see assertContainersFree)
|
||||
// rather than in a unique index — this GIN index is what makes the check
|
||||
// cheap.
|
||||
await queryRunner.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_empty_return_requests_containers
|
||||
ON freight.empty_return_requests USING gin (container_numbers)
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE IF EXISTS freight.empty_return_requests`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user