# Parcel example

## A complete example: Parcel

**Packages** watches deliveries and keeps them; **Delivery times** reads them for the period showing. Save them as `packages.json` and `delivery-times.json` in the Pages folder, with a Parcel API key saved as the `parcel` secret.

These two are cut down to show how History works. The complete pages, with a page per carrier and only deliveries in your area counted, are the downloads on [Parcel](https://legatobar.com/services/parcel#download-the-pages).

```json
{
  "$schema": "./.schemas/page.schema.json",
  "title": "Packages",
  "icon": "shippingbox",
  "sources": {
    "parcel": {
      "url": "https://api.parcel.app",
      "secret": "parcel",
      "auth": { "type": "header", "name": "api-key" },
      "every": "30m",
      "requests": {
        "active": "/external/deliveries/?filter_mode=active",
        "recent": "/external/deliveries/?filter_mode=recent"
      },
      "collect": {
        "dataset": "parcel-deliveries",
        "each": ["active.deliveries", "recent.deliveries"],
        "key": "{carrier_code}:{tracking_number}",
        "fields": ["carrier_code", "tracking_number", "description", "status_code",
                   "events[].date", "events[].event", "events[].location"],
        "remove_after": {
          "events[].event": { "markers": ["Received By:", "Delivered:"],
                              "keep_exact": ["Signature Not Req", "Signature Required"] }
        },
        "date": { "date": { "first": "date", "of": "events",
                            "where": { "path": "event", "contains": "Delivered" },
                            "sort": { "by": "date", "order": "desc", "as": "date" } } },
        "keep": { "for": "400d" },
        "readers": ["delivery-times"],
        "accepts_rows": true
      }
    }
  },
  "cards": [
    { "id": "on-the-way", "type": "list", "label": "On the way", "items": [],
      "source": { "ref": "parcel",
        "map": { "items": { "each": "active.deliveries", "limit": 20,
                            "fields": { "text": "description", "detail": "carrier_code" } } },
        "empty": { "when": { "path": "active.deliveries", "equals": [] }, "text": "Nothing on the way" } } }
  ]
}
```

- Every 30 minutes, both lists are fetched once; the card shows what's on the way, and each delivery in either list is kept under its carrier and tracking number.
- A delivery's date is its newest Delivered event. Parcel's dates are wall times, so a package delivered at 9:15 PM on the 4th is in the 4th wherever the Mac is. A package not delivered yet is undated: it's kept, and gets its date the first time an answer shows it delivered.
- Names after "Received By:" and "Delivered:" are removed before anything is stored.
- `accepts_rows` lets you add deliveries from before the page existed (below).

```json
{
  "$schema": "./.schemas/page.schema.json",
  "title": "Delivery times",
  "icon": "clock",
  "sidebar": false,
  "period": {
    "choices": [
      { "id": "30d", "label": "30 days", "last_days": 30 },
      { "id": "90d", "label": "90 days", "last_days": 90 },
      { "id": "365d", "label": "365 days", "last_days": 365 }
    ]
  },
  "sources": {
    "kept": {
      "dataset": "parcel-deliveries",
      "derive": [
        { "name": "times", "value": { "each": "kept.rows", "fields": {
            "carrier": "row.carrier_code",
            "item": "row.description",
            "delivered": "date",
            "days": { "days_between": [
              { "date": { "first": "date", "of": "row.events",
                          "where": { "path": "event", "contains": "Picked up" },
                          "sort": { "by": "date", "as": "date" } } },
              "date" ] } } } },
        { "name": "average", "value": { "avg": "days", "of": "times", "where": { "path": "days", "gte": 0 }, "round": 1 } }
      ]
    }
  },
  "cards": [
    { "id": "summary", "type": "facts", "label": "Delivered", "items": [],
      "source": { "ref": "kept", "map": {
        "items[0].key": { "template": "Delivered" },
        "items[0].value": { "template": "{kept.selected_count}" },
        "items[1].key": { "template": "Pickup to delivery" },
        "items[1].value": { "template": "{average} days" },
        "items[2].key": { "template": "Recording since" },
        "items[2].value": { "template": "{kept.coverage.recording_since|day}" } } } },
    { "id": "by-carrier", "type": "bars", "label": "Days, by carrier", "unit": "days", "items": [],
      "source": { "ref": "kept", "map": { "items": { "each": "times", "where": { "path": "days", "gte": 0 },
        "group": "carrier", "fields": { "label": "key", "value": { "avg": "days", "of": "rows", "round": 1 } } } } } },
    { "id": "recent", "type": "list", "label": "Recent", "items": [],
      "source": { "ref": "kept",
        "map": { "items": { "each": "times", "sort": { "by": "delivered", "order": "desc", "as": "date" }, "limit": 4,
                            "fields": { "text": "item", "detail": { "template": "{days} days" } } } },
        "empty": { "when": { "path": "kept.selected_count", "equals": 0 }, "text": "Nothing delivered in these days" } } }
  ]
}
```

- With a period, the reader gets only dated rows in it: here, the packages delivered in the last 30, 90 or 365 days.
- `times` gives each delivered package its carrier, its name and the days from its first "Picked up" event to its delivery. A package with no "Picked up" event has no `days`, and `"gte": 0` leaves it out of the average and the bars.
- `"sidebar": false` makes it a [detail page](https://legatobar.com/docs/detail-pages.md#detail-pages): link to it from a card on Packages with `legato://page/delivery-times`.
- "Recording since" says how far back the history goes. Until then, the page shows what Parcel's recent list held when it started.

**Deliveries from before.** Write the ones you have as Parcel-shaped items, with the carriers' dates exactly as they wrote them:

```json
[
  { "carrier_code": "ups", "tracking_number": "EX10000001", "description": "Desk lamp", "status_code": 0,
    "events": [
      { "date": "October 30, 2026 2:12 PM", "event": "Delivered", "location": "Fernhollow" },
      { "date": "October 27, 2026 6:40 PM", "event": "Picked up", "location": "Kestrel Bay" } ] },
  { "carrier_code": "usps", "tracking_number": "EX20000002", "description": "Book stand", "status_code": 0,
    "events": [
      { "date": "October 21, 2026 11:03 AM", "event": "Delivered, Front Door/Porch", "location": "Fernhollow" },
      { "date": "October 18, 2026 9:20 AM", "event": "Picked up", "location": "Millbrook Junction" } ] }
]
```

```bash
legato rows add parcel-deliveries old-deliveries.json
```

They join the current history with the same key, fields, removal and dates as fetched ones. A delivery Parcel still lists replaces its imported row the next time Packages fetches. Imported rows count from when they're added for retention; with `"clock": "date"` in `keep`, they'd age by their delivery dates instead.

