DEV Community

Hasnain Qureshi
Hasnain Qureshi

Posted on Originally published at blinkshift.net

Model Consolidation as a History of Membership, Not a List of Current Contents

Picture a bag that left the hub this afternoon. This morning it held twelve customer packages. One was pulled for review before departure. Tonight a destination team wants to know what actually traveled, and a customer wants to know which handling fees apply to the package that stayed behind.

If the platform stores only the group's current contents, both answers may already be gone. The bag now looks as though the removed package was never there. Billing cannot tell a service performed on the package from a charge that belonged to the group. Customer status has nothing reliable to show.

This is the records problem behind consolidation at a multi-market hub. I have worked on a Caribbean cross-border forwarding platform for a Miami hub serving 9+ markets, where receiving, grouping, destination-specific charges, and handoffs have to remain explainable after the freight has moved. The design note below is a recommended model. It is not a dump of a client's production database.

GS1 identifies a logistics unit with an SSCC, and GS1 materials on shipping management describe relationships between units, shipments, and consolidated consignments. An SSCC should not be treated as interchangeable with a parcel carrier's tracking number. You do not need to adopt every identifier standard on day one. You do need identities that grouping cannot destroy.

Four identities, plus an event log

I would distinguish:

  1. The individual package the customer cares about.
  2. Any physical grouping (a bag, carton, pallet, or master unit).
  3. The onward dispatch or shipment that is supposed to leave the hub.
  4. External references: inbound tracking, an SSCC if you use one, a carrier tracking number for the onward movement.

The current membership of a group can be a view. It should not be the only table you have.

create table package (
  id                   bigserial primary key,
  customer_account_id  bigint not null,
  origin_reference     text,            -- inbound tracking or purchase ref
  destination_market   text not null,   -- a specific market, not a catch-all
  current_status       text not null    -- received, held, assigned,
                                        -- departed, arrived, exception
);

create table logistics_unit (
  id                      bigserial primary key,
  unit_type               text not null,  -- package, bag, carton, pallet, master
  sscc                    text,           -- optional; not a parcel tracking number
  carrier_tracking_number text,
  purpose                 text not null,  -- receiving, consolidation, onward_shipment
  created_at              timestamptz not null default now()
);

-- A package may also be represented as a logistics_unit of type 'package'.
create table package_unit (
  package_id  bigint primary key references package(id),
  unit_id     bigint not null unique references logistics_unit(id)
);

create table unit_membership_event (
  id              bigserial primary key,
  child_unit_id   bigint not null references logistics_unit(id),
  parent_unit_id  bigint not null references logistics_unit(id),
  event_type      text not null,   -- assigned, removed, held, handed_over, received
  occurred_at     timestamptz not null,
  actor_id        bigint,
  reason          text,
  location_id     bigint,          -- hub or destination office
  evidence        text
);

create table dispatch (
  id                   bigserial primary key,
  destination_market   text not null,
  master_unit_id       bigint references logistics_unit(id),
  planned_departure_at timestamptz,
  departed_at          timestamptz,
  arrived_at           timestamptz
);

-- A point-in-time list of what was expected or what actually moved.
create table dispatch_membership_snapshot (
  id               bigserial primary key,
  dispatch_id      bigint not null references dispatch(id),
  package_id       bigint not null references package(id),
  included         boolean not null,
  snapshot_at      timestamptz not null,
  snapshot_reason  text not null,  -- planned, departed, arrived
  unique (dispatch_id, package_id, snapshot_reason, snapshot_at)
);
Enter fullscreen mode Exit fullscreen mode

Current contents can then be derived, instead of being edited in place:

-- Latest membership event per child/parent pair.
create view unit_current_membership as
select distinct on (child_unit_id, parent_unit_id)
  child_unit_id,
  parent_unit_id,
  event_type,
  occurred_at
from unit_membership_event
order by child_unit_id, parent_unit_id, occurred_at desc, id desc;
Enter fullscreen mode Exit fullscreen mode

A package is in a group now only if its latest event for that parent is assigned or handed_over, not removed or held. That rule should be explicit in code. Do not infer it from a missing row.

Departed means "verified membership at the handoff," not "everyone who was ever assigned"

The bug I want to prevent is this: a package is assigned, then removed; the dispatch later departs; a report of "packages on dispatch X" is written as where dispatch_id = X against a current-contents table that no longer mentions the removal. The destination team cannot see the hole. Billing may still attach a group-level fee to a package that never left, or fail to bill a hold that did occur.

function removePackageFromDispatch(package, dispatch, actor, reason):
  parent = dispatch.master_unit
  child  = unit for package

  append unit_membership_event(
    child, parent,
    event_type = "removed",
    occurred_at = now(),
    actor, reason,
    location = current hub
  )

  set package.current_status = "held" or "exception",
      according to the reason

  mark any group-level checks as needing review
    (weight, piece count, documents, seal)

  do not edit earlier membership events
  do not assume destination charges or handling fees
    should move to the group or disappear

function closeDispatchForDeparture(dispatch, actor):
  members = packages whose latest event on dispatch.master_unit
            is an included type (assigned or handed_over)

  write dispatch_membership_snapshot(
    dispatch,
    every package ever assigned, with included = true/false,
    snapshot_reason = "departed",
    snapshot_at = now()
  )

  set dispatch.departed_at = now()
  set current_status = "departed" only for members actually included

  packages removed or held stay on their own status
  their history remains queryable against the same dispatch
Enter fullscreen mode Exit fullscreen mode

Two billing rules fall out of the same model:

  • A service performed on the package (repacking, storage, a hold, a destination charge for that market) should reference the package, the account, and the time it was recorded.
  • A service charged to the group (a master-bag handling fee, a shared transport cost) should reference the dispatch and the snapshot that was in effect, not a later rewrite of membership.

If you only store the second kind of charge, and membership is a mutable list, you will not be able to explain either one.

Test a difficult dispatch, not only a clean bag

Walk the model through four cases before you trust a "add to consolidation" button:

  1. A normal consolidation that departs as planned.
  2. A package removed before departure, with a reason and an owner.
  3. A held item that never joins a dispatch.
  4. A destination discrepancy: the arrived snapshot does not match the departed snapshot.

For each case, ask:

  • Which record explains what was expected?
  • Which record confirms what was handed over?
  • Can staff trace the package without opening the group?
  • Can billing distinguish package-level services from group-level services?

Those questions are the requirements. The schema above is only useful if it can answer them after the freight has left the building.

This is the kind of exception that custom cross-border forwarding software has to hold if a corridor app only tracks the group that exists right now. Buy the existing product if it already preserves membership history, departed snapshots, and package-level charges. Extend or build when that history still lives in a spreadsheet.

This article is a rewritten engineering treatment of a subject I first discussed for operators, originally published on Consolidation Should Not Erase Individual Package History.


Hasnain Qureshi is co-founder of BlinkShift, which builds custom software for forwarding, consolidation, and destination-charge workflows.

Top comments (0)