{
  "ursId": "URS-071",
  "title": "Distributor returns expiring, expired, or recalled stock to the manufacturer",
  "timestamp": "2026-08-03T23:05:12.565Z",
  "durationMs": 28164,
  "config": {
    "inboxUrl": "http://localhost:35551",
    "dbHost": "localhost",
    "dbPort": 46175,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Login and open the expiry return flow",
      "status": "pass",
      "description": "Step 1: Login and open the expiry return flow",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-01-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-01-reason-manufacturer-step.png"
      ],
      "explanation": "A Corveta sales rep (Blair Bennett) logs in and opens the dedicated expiry/recall returns flow at /returns/new?type=expiry. The first step asks why the stock is coming back (Expiring or Recall) and which manufacturer it belongs to. This verifies the lean flow is reachable and gated by the rep's returns permissions and the `returns` feature flag.",
      "startedAt": "2026-08-03T23:05:17.704Z",
      "finishedAt": "2026-08-03T23:05:20.484Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/videos/step-01-open-expiry-flow.webm"
    },
    {
      "name": "Step 2: Select manufacturer and expiring lots",
      "status": "pass",
      "description": "Step 2: Select manufacturer and expiring lots",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-02-lot-picker.png",
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-02-lots-selected.png"
      ],
      "explanation": "The rep selects Vantis as the manufacturer (auto-selected when Corveta stocks a single manufacturer) and advances to the lot picker. The picker lists the distributor's own returnable inventory with expiration status badges; the seeded URS-071 lot renders as \"expiring soon\". The rep uses \"Select all expiring\" to choose it. This verifies the flow surfaces the distributor's own expiring stock and lets the rep pick the lots to return.",
      "startedAt": "2026-08-03T23:05:27.198Z",
      "finishedAt": "2026-08-03T23:05:28.336Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/videos/step-02-select-lots.webm"
    },
    {
      "name": "Step 3: Review and submit the expiry return",
      "status": "pass",
      "description": "Step 3: Review and submit the expiry return",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-03-review.png",
        "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/screenshots/step-03-submission-success.png"
      ],
      "explanation": "The rep reviews the return — manufacturer, reason, and selected lots — adds notes, and submits. The submit path creates a return with return_type=expiry, derives the return location from the selected lots, attaches no sales account, and creates no replacement order. Because the replacement-order step is skipped, the service writes a decision audit row (create_replacement_order_on_return, performed=false, reason=return_type_not_incident). This is the primary URS-071 evidence that the lean return persists correctly and the skip is audited.",
      "expectedAuditActions": [
        "decision:create_replacement_order_on_return"
      ],
      "startedAt": "2026-08-03T23:05:37.084Z",
      "finishedAt": "2026-08-03T23:05:39.074Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z/videos/step-03-submit.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Expiry return created for Corveta → Vantis",
      "query": "SELECT id, return_number, status, return_type, sales_account_id,\n      return_location_id, replacement_order_id, notes, created_at\n    FROM returns\n    WHERE sales_organization_id = $1\n      AND manufacturer_organization_id = $2\n      AND return_type = 'expiry'\n      AND notes LIKE '%URS-071%'\n      AND created_at > NOW() - INTERVAL '2 hours'\n    ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9e0-2997-7e03-b5c1-23a27310fb16",
          "return_number": "RET-1",
          "status": "submitted",
          "return_type": "expiry",
          "sales_account_id": null,
          "return_location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "replacement_order_id": null,
          "notes": "URS-071 validation run — submitted at 2026-08-03T23:05:36.507Z",
          "created_at": "2026-08-03T23:05:38.943Z"
        }
      ],
      "assertion": "A URS-071 return with return_type=expiry and status=submitted should exist (marker: \"URS-071 validation run\")."
    },
    {
      "name": "No replacement order created for the expiry return",
      "query": "-- replacement_order_id from the return row above",
      "status": "pass",
      "rows": [
        {
          "replacement_order_id": null
        }
      ],
      "assertion": "Expiry/recall returns never create a replacement order, so replacement_order_id must be NULL."
    },
    {
      "name": "Return location derived from lots and no sales account required",
      "query": "-- return_location_id and sales_account_id from the return row above",
      "status": "pass",
      "rows": [
        {
          "return_location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "sales_account_id": null
        }
      ],
      "assertion": "The return location should be derived from the selected lots (return_location_id NOT NULL) and no account should be attached (sales_account_id NULL)."
    },
    {
      "name": "Return item carries the seeded lot, manufacturer, and location",
      "query": "SELECT ri.id, ri.product_id, ri.lot_number, ri.manufacturer_organization_id,\n      ri.location_id, ri.return_item_number\n    FROM return_items ri\n    WHERE ri.return_id = $1\n    ORDER BY ri.lot_number",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9e0-299b-75dc-982e-664efc11eecb",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "URS071-SHV-001",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-8"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665294c9f11d",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "URS071-SHV-001",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-12"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6651658e7302",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "URS071-SHV-001",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-11"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6650b75279a8",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "URS071-SHV-001",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-10"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-664f6e00d9c5",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "URS071-SHV-001",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-9"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665c5a717eb5",
          "product_id": "01989c9e-9ccf-7a73-8ff7-b60007f2de85",
          "lot_number": "VM-PMX-2418",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200616-4"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6659ec7ce25b",
          "product_id": "01989c9e-9ccf-7a73-8ff7-b60007f2de85",
          "lot_number": "VM-PMX-2418",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200616-1"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665a01f54553",
          "product_id": "01989c9e-9ccf-7a73-8ff7-b60007f2de85",
          "lot_number": "VM-PMX-2418",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200616-2"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665b9cdbac77",
          "product_id": "01989c9e-9ccf-7a73-8ff7-b60007f2de85",
          "lot_number": "VM-PMX-2418",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200616-3"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-664bbaa6eade",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2390",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200873-5"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-664cc63a9eed",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2390",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200873-6"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-664d867d9f44",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2390",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "b7f8a9b0-c1d2-3456-1234-567890123456",
          "return_item_number": "RET-1-72200873-7"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-664a849d2cd2",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2401",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-4"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-66498ea9104e",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2401",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-3"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-66481e62b906",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2401",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-2"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-66478d45c65e",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2401",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-1"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6657cc237b06",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-17"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6658dec5eb7b",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-18"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665399c70506",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-13"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6655b399b619",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-15"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-6654deba9be4",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-14"
        },
        {
          "id": "019fc9e0-299b-75dc-982e-665628b2bfb0",
          "product_id": "01989c9e-fb9b-7f7e-a345-ee490ddfc1cc",
          "lot_number": "VM-SHV-2415",
          "manufacturer_organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "return_item_number": "RET-1-72200873-16"
        }
      ],
      "assertion": "At least one return_items row should carry lot_number=URS071-SHV-001, manufacturer_organization_id=Vantis, and location_id=TRUNK-25 (the lot the picker selected)."
    },
    {
      "name": "Replacement-order skip recorded as an audit decision",
      "query": "SELECT id, event_type, action, object_id, organization_id, payload, created_at\n    FROM audit_events\n    WHERE created_at >= $1\n      AND event_type = 'decision'\n      AND action = 'create_replacement_order_on_return'\n      AND payload->>'performed' = 'false'\n      AND payload->>'reason' = 'return_type_not_incident'\n    ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9e0-29a1-7639-ada5-3d164c6cb348",
          "event_type": "decision",
          "action": "create_replacement_order_on_return",
          "object_id": "019fc9e0-2997-7e03-b5c1-23a27310fb16",
          "organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "payload": {
            "reason": "return_type_not_incident",
            "performed": false,
            "entityType": "return"
          },
          "created_at": "2026-08-03T23:05:38.943Z"
        }
      ],
      "assertion": "Submitting an expiry return should write a decision audit_events row with action='create_replacement_order_on_return', performed=false, reason='return_type_not_incident'."
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/home/runner/_work/code/code/validation_test_results/urs-071-expiry-returns/2026-08-03T23-05-12-564Z",
  "auditStartTime": "2026-08-03T23:05:10.689Z",
  "auditEventEvidence": [
    {
      "createdAt": "2026-08-03T23:05:10.877Z",
      "eventType": "checklist",
      "action": "checklists.create",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9df-bbde-795b-b31d-9d52feabc8bb",
      "secondaryObjectId": null,
      "payload": {
        "title": "Lot expiration review (1 location need attention)",
        "source": {
          "type": "lot_expiration",
          "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901"
        }
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:05:10.977Z",
      "eventType": "decision",
      "action": "ensure_lot_expiration_checklist",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "secondaryObjectId": null,
      "payload": {
        "reason": "created_1_reopened_0_already_open_1",
        "performed": true,
        "entityType": "organization"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:05:16.090Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:05:22.638Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:05:30.109Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:05:38.943Z",
      "eventType": "decision",
      "action": "create_replacement_order_on_return",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9e0-2997-7e03-b5c1-23a27310fb16",
      "secondaryObjectId": null,
      "payload": {
        "reason": "return_type_not_incident",
        "performed": false,
        "entityType": "return"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:05:38.943Z",
      "eventType": "return_request",
      "action": "created",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9e0-2997-7e03-b5c1-23a27310fb16",
      "secondaryObjectId": null,
      "payload": {
        "itemCount": 22,
        "returnNumber": "RET-1",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    }
  ],
  "auditQuery": "SELECT\n    ae.created_at,\n    ae.event_type,\n    ae.action,\n    ae.user_id,\n    u.email AS user_email,\n    ae.organization_id,\n    o.name AS organization_name,\n    ae.object_id,\n    ae.secondary_object_id,\n    ae.payload,\n    ae.route,\n    ae.trace_id\n  FROM audit_events ae\n  LEFT JOIN users u ON u.id = ae.user_id\n  LEFT JOIN organizations o ON o.id = ae.organization_id\n  WHERE ae.created_at >= $1\n    AND ae.organization_id = ANY($2::uuid[])\n  ORDER BY ae.created_at ASC",
  "auditAssertions": [
    {
      "stepName": "Step 3: Review and submit the expiry return",
      "expectedAction": "decision:create_replacement_order_on_return",
      "found": true
    }
  ],
  "emailAssertions": []
}