-
Notifications
You must be signed in to change notification settings - Fork 6
Version every negotiated term: widen the header snapshot and diff the accessorial schedule #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
42b4521
96f407c
1aa7b63
028171f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import type { RateAgreementVersion } from "@trenova/shared/types/rate"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { describeVersion } from "../version-summary"; | ||
|
|
||
| const CHARGE_ID = "acc_01K2ZX8Y4M5N6P7Q8R9S0T1V2W"; | ||
|
|
||
| function version(overrides: Partial<RateAgreementVersion>): RateAgreementVersion { | ||
| return { | ||
| versionNumber: 2, | ||
| effectiveFrom: 1_700_000_000, | ||
| changeMessage: "", | ||
| ...overrides, | ||
| } as RateAgreementVersion; | ||
| } | ||
|
|
||
| describe("describeVersion", () => { | ||
| it("prefers the change message when one was written", () => { | ||
| const v = version({ | ||
| changeMessage: "Annual renegotiation", | ||
| changeSummary: { currency: { from: "USD", to: "CAD" } }, | ||
| }); | ||
| expect(describeVersion(v)).toBe("Annual renegotiation"); | ||
| }); | ||
|
|
||
| it("humanizes header field paths instead of echoing camelCase keys", () => { | ||
| const v = version({ | ||
| changeSummary: { | ||
| agreementEffectiveFrom: { from: 1, to: 2 }, | ||
| defaultMinCharge: { from: "100", to: "150" }, | ||
| }, | ||
| }); | ||
| const text = describeVersion(v); | ||
| expect(text).toContain("Effective from"); | ||
| expect(text).toContain("Default minimum charge"); | ||
| expect(text).not.toContain("agreementEffectiveFrom"); | ||
| }); | ||
|
|
||
| it("names an accessorial term change by the charge's code, never its id", () => { | ||
| const v = version({ | ||
| changeSummary: { | ||
| [`accessorialTerms.${CHARGE_ID}.amount`]: { from: "25", to: "40" }, | ||
| }, | ||
| accessorialNames: { [CHARGE_ID]: "DETENTION" }, | ||
| }); | ||
| const text = describeVersion(v); | ||
| expect(text).toContain("DETENTION"); | ||
| expect(text).not.toContain(CHARGE_ID); | ||
| }); | ||
|
|
||
| it("reads an added accessorial as an addition", () => { | ||
| const v = version({ | ||
| changeSummary: { | ||
| [`accessorialTerms.${CHARGE_ID}`]: { to: { amount: "75" } }, | ||
| }, | ||
| accessorialNames: { [CHARGE_ID]: "TONU" }, | ||
| }); | ||
| expect(describeVersion(v)).toContain("Added TONU accessorial"); | ||
| }); | ||
|
|
||
| it("falls back to a plain phrase when the charge cannot be named", () => { | ||
| const v = version({ | ||
| changeSummary: { | ||
| [`accessorialTerms.${CHARGE_ID}`]: { from: { amount: "75" } }, | ||
| }, | ||
| }); | ||
| const text = describeVersion(v); | ||
| expect(text).toContain("Removed accessorial"); | ||
| expect(text).not.toContain(CHARGE_ID); | ||
| }); | ||
|
|
||
| it("collapses fuel binding sub-fields into one phrase", () => { | ||
| const v = version({ | ||
| changeSummary: { | ||
| "fuelTerms.fuelSurchargeProgramId": { from: "fsp_1", to: "fsp_2" }, | ||
| "fuelTerms.capAmount": { from: null, to: "500" }, | ||
| }, | ||
| }); | ||
| expect(describeVersion(v)).toBe("Fuel terms"); | ||
| }); | ||
|
|
||
| it("shows a dash when nothing is recorded", () => { | ||
| expect(describeVersion(version({}))).toBe("—"); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import type { RateAgreementVersion } from "@trenova/shared/types/rate"; | ||
|
|
||
| /** | ||
| * Labels for the header terms a version can record, in the words the form uses. | ||
| * A path missing from this map falls back to its raw key, so a newly versioned | ||
| * field degrades to legible-but-plain rather than to nothing. | ||
| */ | ||
| const HEADER_FIELD_LABELS: Record<string, string> = { | ||
| partyType: "Party type", | ||
| customerId: "Customer", | ||
| carrierId: "Carrier", | ||
| code: "Code", | ||
| name: "Name", | ||
| description: "Description", | ||
| agreementType: "Agreement type", | ||
| contractRef: "Contract reference", | ||
| documentId: "Contract document", | ||
| priority: "Priority", | ||
| agreementEffectiveFrom: "Effective from", | ||
| agreementEffectiveTo: "Effective to", | ||
| autoRenew: "Auto-renew", | ||
| renewalNoticeDays: "Renewal notice days", | ||
| billToCustomerId: "Bill-to customer", | ||
| currency: "Currency", | ||
| defaultMinCharge: "Default minimum charge", | ||
| defaultMaxCharge: "Default maximum charge", | ||
| roundingMode: "Rounding", | ||
| roundingPrecision: "Rounding precision", | ||
| marginFloorPercent: "Margin floor", | ||
| maxPayPercentOfSell: "Max pay percent of sell", | ||
| }; | ||
|
|
||
| const ACCESSORIAL_FIELD_LABELS: Record<string, string> = { | ||
| method: "method", | ||
| rateUnit: "rate unit", | ||
| amount: "amount", | ||
| waived: "waiver", | ||
| autoApply: "auto-apply", | ||
| applyCondition: "condition", | ||
| freeUnits: "free units", | ||
| maxAmount: "cap", | ||
| formulaTemplateId: "rating method", | ||
| serviceTypeIds: "applicability", | ||
| shipmentTypeIds: "applicability", | ||
| appliesFrom: "window", | ||
| appliesTo: "window", | ||
| }; | ||
|
|
||
| const ACCESSORIAL_PREFIX = "accessorialTerms."; | ||
|
|
||
| type FieldChange = { from?: unknown; to?: unknown }; | ||
|
|
||
| function accessorialPhrase( | ||
| path: string, | ||
| change: FieldChange, | ||
| names: Record<string, string>, | ||
| ): string { | ||
| const rest = path.slice(ACCESSORIAL_PREFIX.length); | ||
| const dot = rest.indexOf("."); | ||
| const chargeId = dot === -1 ? rest : rest.slice(0, dot); | ||
| const field = dot === -1 ? "" : rest.slice(dot + 1); | ||
| const name = names[chargeId]; | ||
|
|
||
| if (!field) { | ||
| // A whole term appearing or vanishing is the schedule itself changing. | ||
| const added = change.to != null; | ||
| if (added) return name ? `Added ${name} accessorial` : "Added accessorial"; | ||
| return name ? `Removed ${name} accessorial` : "Removed accessorial"; | ||
| } | ||
|
|
||
| const fieldLabel = ACCESSORIAL_FIELD_LABELS[field] ?? field; | ||
| return name ? `${name} ${fieldLabel}` : `Accessorial ${fieldLabel}`; | ||
| } | ||
|
|
||
| /** | ||
| * One line saying what a version changed, written for the person reading the | ||
| * history — field names in the form's words, accessorials named by their | ||
| * charge's code, and never a record id. | ||
| */ | ||
| export function describeVersion(version: RateAgreementVersion): string { | ||
| if (version.changeMessage) return version.changeMessage; | ||
|
|
||
| const summary = version.changeSummary ?? {}; | ||
| const names = version.accessorialNames ?? {}; | ||
|
|
||
| const phrases: string[] = []; | ||
| for (const [path, change] of Object.entries(summary)) { | ||
| let phrase: string; | ||
| if (path.startsWith(ACCESSORIAL_PREFIX)) { | ||
| phrase = accessorialPhrase(path, change as FieldChange, names); | ||
| } else if (path === "fuelTerms" || path.startsWith("fuelTerms.")) { | ||
| phrase = "Fuel terms"; | ||
| } else { | ||
| phrase = HEADER_FIELD_LABELS[path] ?? path; | ||
| } | ||
| if (!phrases.includes(phrase)) { | ||
| phrases.push(phrase); | ||
| } | ||
| } | ||
|
|
||
| if (phrases.length === 0) return "—"; | ||
|
|
||
| return phrases.join(", "); | ||
| } | ||
|
Comment on lines
+80
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Move
As per coding guidelines, "App-only utilities belong in that app's src/lib/; do not define utilities inline in components, hooks, or routes." 📍 Affects 3 files
🤖 Prompt for AI AgentsSource: Coding guidelines |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use contract-based fixtures for nullable version summaries.
The fixture uses a partial object and
as RateAgreementVersion. It does not model the serialized version contract. Add cases that distinguish absent,null, and emptychangeSummary, plus multiple differing changes. The backend can serialize a nilChangeSummarymap asnull, anddescribeVersionhas separate nullish handling.As per coding guidelines, "Write test fixtures from the external contract" and "Include fixture cases for contract-relevant distinctions such as absent versus null versus empty values, multiple elements, and disagreeing field values."
🤖 Prompt for AI Agents
Source: Coding guidelines