Files
edr-platform/apps/edr-freight-web/portal/src/components/customer-actions/deriveContractCustomerAction.ts
Marshal 2429f6b629 implement contract cancellation feature and update contract statuses
- Added functionality to cancel contracts, allowing users to provide a reason for cancellation.
- Updated contract statuses to include SUSPENDED and changed CLOSED to COMPLETED.
- Enhanced the UI to reflect the new cancellation option and updated messaging for contract statuses.
- Refactored contract booking actions to accommodate changes in booking logic for ONE_TIME and GENERAL contracts.
- Removed clearance document management from the contract detail page, as it is now handled per booking.
- Introduced a SQL script to reset bookings and train schedules for development purposes.
2026-07-28 05:02:58 +00:00

172 lines
3.8 KiB
TypeScript

import type { Freight } from "@edr/types";
import type { LucideIcon } from "lucide-react";
import {
CreditCard,
Eye,
FileSignature,
PackagePlus,
PencilLine,
RotateCcw,
} from "lucide-react";
import { getContractBookingAction } from "@/pages/contracts/contract-booking-action";
export type ContractCustomerAction =
| {
type: "sign";
label: string;
to: string;
primary: boolean;
icon: LucideIcon;
}
| {
type: "navigate";
label: string;
to: string;
primary: boolean;
icon: LucideIcon;
}
| {
type: "pay";
booking: Freight.IBooking;
label: string;
primary: boolean;
icon: LucideIcon;
}
| {
/** One-click bare booking instance (GENERAL non-customs) — mutation, not navigation. */
type: "initiate";
contract: Freight.IContract;
label: string;
primary: boolean;
icon: LucideIcon;
};
function findPayableBookingForContract(
contractId: string,
bookings: Freight.IBooking[],
): Freight.IBooking | null {
return (
bookings.find((b) => {
if (b.contractId !== contractId) return false;
if (b.paymentStatus === "PAID") return false;
const isGeneral = b.bookingType === "GENERAL_CONTRACT";
return isGeneral
? b.status === "FULLY_EXECUTED"
: b.status === "SELECTED_FOR_BATCH";
}) ?? null
);
}
/** Single best customer action for a contract row (list / home). */
export function deriveContractCustomerAction(
contract: Freight.IContract,
bookings: Freight.IBooking[],
): ContractCustomerAction {
const id = contract.id;
if (contract.status === "CONTRACT_READY") {
return {
type: "sign",
label: "View & sign",
to: `/contracts/${id}/view`,
primary: true,
icon: FileSignature,
};
}
if (contract.status === "CHANGES_REQUESTED") {
return {
type: "navigate",
label: "Edit & resubmit",
to: `/contracts/${id}`,
primary: true,
icon: PencilLine,
};
}
// Saved-but-not-submitted contract — send the customer back into the wizard to
// finish editing and submit it for review.
if (contract.status === "DRAFT" || contract.status === "RENEWAL_DRAFT") {
return {
type: "navigate",
label: "Continue draft",
to: `/contracts/${id}/edit`,
primary: true,
icon: PencilLine,
};
}
const payable = findPayableBookingForContract(id, bookings);
if (payable) {
return {
type: "pay",
booking: payable,
label: "Pay now",
primary: true,
icon: CreditCard,
};
}
if (
contract.customsClearingEnabled &&
["CLEARANCE_READY_FOR_BOOKING", "ACTIVE_SHIPMENT_IN_PROGRESS"].includes(
contract.status,
)
) {
return {
type: "navigate",
label: "View",
to: `/contracts/${id}`,
primary: false,
icon: Eye,
};
}
const bookingAction = getContractBookingAction(contract, bookings);
if (bookingAction.kind === "initiate") {
return {
type: "initiate",
contract,
label: "Initiate booking",
primary: true,
icon: PackagePlus,
};
}
if (bookingAction.kind === "book") {
return {
type: "navigate",
label: "Book shipment",
to: bookingAction.to,
primary: true,
icon: PackagePlus,
};
}
if (bookingAction.kind === "rebook") {
return {
type: "navigate",
label: "Re-book shipment",
to: bookingAction.to,
primary: true,
icon: RotateCcw,
};
}
if (bookingAction.kind === "request") {
return {
type: "navigate",
label: "Request shipment",
to: bookingAction.to,
primary: true,
icon: PackagePlus,
};
}
return {
type: "navigate",
label: "View",
to: `/contracts/${id}`,
primary: false,
icon: Eye,
};
}