{
  "ursId": "URS-016",
  "title": "Auto-decrement Inventory After Order Submission",
  "timestamp": "2026-04-23T03:36:41.370Z",
  "durationMs": 119507,
  "config": {
    "inboxUrl": "http://localhost:62754",
    "dbHost": "localhost",
    "dbPort": 62755,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Initial inventory",
      "status": "pass",
      "description": "Step 1: Initial inventory",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-01-logged-in.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-01-initial-inventory.png"
      ],
      "explanation": "Logs in as a sales rep (Bob Kauffman) and navigates to TRUNK-36 to record the starting quantities for SpeedPatch and FiberLocker. This establishes the known baseline so subsequent steps can prove the correct delta was applied after each order.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-04-23T03:36:46.460Z",
      "finishedAt": "2026-04-23T03:36:49.790Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/videos/step-01-initial-inventory.webm"
    },
    {
      "name": "Step 2: First order",
      "status": "pass",
      "description": "Step 2: First order",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-02-account-selected.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-02-devices-selected.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-02-documents.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-02-review.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-02-order-submitted.png"
      ],
      "explanation": "Creates and submits a bill-only order for 3 SpeedPatch and 2 FiberLocker devices from trunk inventory. This exercises the full order-submission flow that should trigger an automatic inventory decrement in the database.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "expectedAuditActions": [
        "decision:bill_only_order.enqueue_upload_classification"
      ],
      "startedAt": "2026-04-23T03:37:03.656Z",
      "finishedAt": "2026-04-23T03:37:22.747Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/videos/step-02-first-order.webm"
    },
    {
      "name": "Step 3: Verify first decrement",
      "status": "pass",
      "description": "Step 3: Verify first decrement",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-03-inventory-after-first-order.png"
      ],
      "explanation": "Navigates to TRUNK-36 immediately after the first order and confirms that SpeedPatch and FiberLocker quantities have each decreased by the ordered amount. This proves the inventory auto-decrement fires on order submission.",
      "startedAt": "2026-04-23T03:37:29.542Z",
      "finishedAt": "2026-04-23T03:37:29.542Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/videos/step-03-verify-decrement.webm"
    },
    {
      "name": "Step 4: Second order",
      "status": "pass",
      "description": "Step 4: Second order",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-04-second-order-submitted.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-04-inventory-after-second-order.png"
      ],
      "explanation": "Submits a second bill-only order and then re-checks the inventory to verify that quantities reflect both orders cumulatively. This proves the decrement is applied consistently across multiple successive submissions.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "expectedAuditActions": [
        "decision:bill_only_order.enqueue_upload_classification"
      ],
      "startedAt": "2026-04-23T03:37:59.356Z",
      "finishedAt": "2026-04-23T03:38:02.888Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/videos/step-04-second-order.webm"
    },
    {
      "name": "Step 5: Shortage edge case",
      "status": "pass",
      "description": "Step 5: Shortage edge case",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-05-shortage-warning.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-05-review-with-shortage.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-05-shortage-result.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/step-05-final-inventory.png"
      ],
      "explanation": "Orders more SpeedPatch units than are available in trunk stock. The system permits submission in best-effort mode and consumes all remaining units, bringing the count to zero. This proves the system handles inventory shortages without blocking the order while still tracking the discrepancy.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "startedAt": "2026-04-23T03:38:25.208Z",
      "finishedAt": "2026-04-23T03:38:35.299Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/videos/step-05-shortage-edge-case.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Inventory items exist at trunk",
      "query": "SELECT ii.id, ii.di, ii.lot, ii.quantity_ready, ii.quantity_backordered, ii.quantity_in_transit\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n      ORDER BY ii.lot",
      "status": "pass",
      "rows": [
        {
          "id": "37d34f0e-71d0-44e4-a8c2-6051d9f00083",
          "di": "07649988202247",
          "lot": "URS016-FL-001",
          "quantity_ready": 12,
          "quantity_backordered": 0,
          "quantity_in_transit": 0
        },
        {
          "id": "183cc218-e60c-4186-b10f-5ae0ef359f2e",
          "di": "07649988202230",
          "lot": "URS016-SP-001",
          "quantity_ready": 0,
          "quantity_backordered": 0,
          "quantity_in_transit": 0
        }
      ],
      "assertion": "Both test inventory items (SpeedPatch and FiberLocker) should exist at TRUNK-36"
    },
    {
      "name": "Inventory quantities decremented",
      "query": "SELECT ii.lot, ii.quantity_ready,\n        CASE\n          WHEN ii.lot = $2 THEN 20\n          WHEN ii.lot = $3 THEN 15\n        END as initial_quantity,\n        CASE\n          WHEN ii.lot = $2 THEN 20 - ii.quantity_ready\n          WHEN ii.lot = $3 THEN 15 - ii.quantity_ready\n        END as total_decremented\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n      ORDER BY ii.lot",
      "status": "pass",
      "rows": [
        {
          "lot": "URS016-FL-001",
          "quantity_ready": 12,
          "initial_quantity": 15,
          "total_decremented": 3
        },
        {
          "lot": "URS016-SP-001",
          "quantity_ready": 0,
          "initial_quantity": 20,
          "total_decremented": 20
        }
      ],
      "assertion": "Both inventory items should have quantity_ready less than their initial values"
    },
    {
      "name": "Recent billing orders created",
      "query": "SELECT bo.id, bo.order_number, bo.status, bo.procedure_date,\n        bo.created_at, bo.created_by_user_id\n      FROM billing_orders bo\n      WHERE bo.created_by_user_id = $1\n        AND bo.created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY bo.created_at DESC\n      LIMIT 5",
      "status": "pass",
      "rows": [
        {
          "id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "order_number": "BO-3",
          "status": "submitted",
          "procedure_date": "2026-04-22T05:00:00.000Z",
          "created_at": "2026-04-23T03:38:29.898Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        },
        {
          "id": "019db86a-720a-76e7-a399-383a55663c73",
          "order_number": "BO-2",
          "status": "submitted",
          "procedure_date": "2026-04-22T05:00:00.000Z",
          "created_at": "2026-04-23T03:37:56.964Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        },
        {
          "id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "order_number": "BO-1",
          "status": "submitted",
          "procedure_date": "2026-04-22T05:00:00.000Z",
          "created_at": "2026-04-23T03:37:20.366Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        }
      ],
      "assertion": "At least one billing order should have been created by Bob Kauffman in the last 30 minutes"
    },
    {
      "name": "Billing order items reference correct products",
      "query": "SELECT boi.id, boi.billing_order_id, op.title as product_name,\n        boi.lot_number, boi.serial_number, boi.location_id,\n        boi.inventory_discrepancy\n      FROM billing_order_items boi\n      JOIN org_products op ON boi.product_id = op.id\n      JOIN billing_orders bo ON boi.billing_order_id = bo.id\n      WHERE bo.created_by_user_id = $1\n        AND bo.created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY bo.created_at DESC, op.title",
      "status": "pass",
      "rows": [
        {
          "id": "019db86a-f2a2-7f49-ba3e-926aaef035e6",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd1fc3e7524",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd2365978df",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd3706f8a3b",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd43eb43ed7",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd5e45aad7a",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd66dc51b49",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd706fa1217",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd87c53e9a2",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-9266117baeef",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-9267642d2b79",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-9268212d8c68",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-9269db68297e",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-926b0ac3c3a5",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-926c195da876",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-926d8c01c2f6",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-926e51b11599",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-926f5ea087b2",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a2-7f49-ba3e-9270554ec455",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-f2a3-7305-aab3-9bd0621055bb",
          "billing_order_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-7202-7755-a209-ce9e86ed2b83",
          "billing_order_id": "019db86a-720a-76e7-a399-383a55663c73",
          "product_name": "FiberLocker® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-7204-7747-8006-8271cf47d7f9",
          "billing_order_id": "019db86a-720a-76e7-a399-383a55663c73",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db86a-7204-7747-8006-82705004712d",
          "billing_order_id": "019db86a-720a-76e7-a399-383a55663c73",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db869-e307-79cb-bac6-efc13347e404",
          "billing_order_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "product_name": "FiberLocker® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db869-e308-77a6-80c6-88c1913bdeb2",
          "billing_order_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "product_name": "FiberLocker® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db869-e309-7d5c-b749-9a338a7ae71b",
          "billing_order_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db869-e309-7d5c-b749-9a32cad23a1c",
          "billing_order_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019db869-e309-7d5c-b749-9a3492c78d6d",
          "billing_order_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "product_name": "SpeedPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        }
      ],
      "assertion": "Billing order items should reference SpeedPatch and/or FiberLocker products"
    },
    {
      "name": "Inventory history records bill_only_order removals",
      "query": "SELECT ih.id, ih.item_id, ih.quantity, ih.status, ih.info,\n        ih.created_at, ih.inventory_id\n      FROM inventory_history ih\n      WHERE ih.item_id IN (\n        SELECT id FROM inventory_items\n        WHERE real_world_location_id = $1\n          AND lot IN ($2, $3)\n      )\n      AND ih.created_at > NOW() - INTERVAL '30 minutes'\n      AND ih.info->>'action' = 'removed'\n      AND ih.info->>'reason' = 'bill_only_order'\n      ORDER BY ih.created_at DESC\n      LIMIT 20",
      "status": "pass",
      "rows": [
        {
          "id": "019db86a-f2ad-7075-b4b7-8748724e42b4",
          "item_id": "183cc218-e60c-4186-b10f-5ae0ef359f2e",
          "quantity": 15,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
            "shortageQuantity": 0,
            "requestedQuantity": 15
          },
          "created_at": "2026-04-23T03:38:29.898Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        },
        {
          "id": "019db86a-f2ad-7075-b4b7-8749a4191cf2",
          "item_id": "183cc218-e60c-4186-b10f-5ae0ef359f2e",
          "quantity": 5,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
            "shortageQuantity": 5,
            "requestedQuantity": 20
          },
          "created_at": "2026-04-23T03:38:29.898Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        },
        {
          "id": "019db86a-7219-76f1-9ca2-a5fb6ec37e5c",
          "item_id": "37d34f0e-71d0-44e4-a8c2-6051d9f00083",
          "quantity": 1,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db86a-720a-76e7-a399-383a55663c73",
            "shortageQuantity": 0,
            "requestedQuantity": 1
          },
          "created_at": "2026-04-23T03:37:56.964Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        },
        {
          "id": "019db86a-7219-76f1-9ca2-a5fca831f0b8",
          "item_id": "183cc218-e60c-4186-b10f-5ae0ef359f2e",
          "quantity": 2,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db86a-720a-76e7-a399-383a55663c73",
            "shortageQuantity": 0,
            "requestedQuantity": 2
          },
          "created_at": "2026-04-23T03:37:56.964Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        },
        {
          "id": "019db869-e316-7af4-8978-6fab781d7633",
          "item_id": "37d34f0e-71d0-44e4-a8c2-6051d9f00083",
          "quantity": 2,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db869-e30c-7d88-8414-53d7a9be656b",
            "shortageQuantity": 0,
            "requestedQuantity": 2
          },
          "created_at": "2026-04-23T03:37:20.366Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        },
        {
          "id": "019db869-e316-7af4-8978-6fac8f0b2dec",
          "item_id": "183cc218-e60c-4186-b10f-5ae0ef359f2e",
          "quantity": 3,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019db869-e30c-7d88-8414-53d7a9be656b",
            "shortageQuantity": 0,
            "requestedQuantity": 3
          },
          "created_at": "2026-04-23T03:37:20.366Z",
          "inventory_id": "019db869-3d3a-7a42-826f-ae2aff8e5d04"
        }
      ],
      "assertion": "Inventory history should contain records with action=removed and reason=bill_only_order"
    },
    {
      "name": "Inventory transactions created",
      "query": "SELECT it.id, it.source_type, it.source_id, it.created_at,\n        it.distributor_organization_id, it.manufacturer_organization_id\n      FROM inventory_transactions it\n      WHERE it.source_type = 'bill_only_order'\n        AND it.created_at > NOW() - INTERVAL '30 minutes'\n        AND (it.distributor_organization_id = $1 OR it.manufacturer_organization_id = $1)\n      ORDER BY it.created_at DESC\n      LIMIT 10",
      "status": "pass",
      "rows": [
        {
          "id": "019db86a-f2ab-7f73-9352-8512a51e4928",
          "source_type": "bill_only_order",
          "source_id": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
          "created_at": "2026-04-23T03:38:29.898Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        },
        {
          "id": "019db86a-7215-7390-bfa6-6e2f5f4c1083",
          "source_type": "bill_only_order",
          "source_id": "019db86a-720a-76e7-a399-383a55663c73",
          "created_at": "2026-04-23T03:37:56.964Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        },
        {
          "id": "019db869-e312-7587-8bc2-3583d0b33178",
          "source_type": "bill_only_order",
          "source_id": "019db869-e30c-7d88-8414-53d7a9be656b",
          "created_at": "2026-04-23T03:37:20.366Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        }
      ],
      "assertion": "Inventory transactions should exist with source_type = bill_only_order"
    },
    {
      "name": "No negative inventory quantities",
      "query": "SELECT ii.id, ii.lot, ii.quantity_ready\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n        AND ii.quantity_ready < 0",
      "status": "pass",
      "rows": [],
      "assertion": "Inventory quantities should never go below zero (CHECK constraint enforced)"
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z",
  "auditStartTime": "2026-04-23T03:36:39.451Z",
  "emailEvidence": [
    {
      "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-1",
      "template": "New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-1",
      "screenshotPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/emails/2026-04-23T03-37-21-740Z-New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-1.png"
    },
    {
      "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-2",
      "template": "New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-2",
      "screenshotPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/emails/2026-04-23T03-37-58-333Z-New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-2.png"
    },
    {
      "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-3",
      "template": "New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-3",
      "screenshotPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-016-inventory-decrement/2026-04-23T03-36-41-369Z/screenshots/emails/2026-04-23T03-38-30-934Z-New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-3.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-04-23T03:36:43.852Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "bob.kauffman@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:36:54.365Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "bob.kauffman@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:37:20.366Z",
      "eventType": "decision",
      "action": "bill_only_order.enqueue_upload_classification",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019db869-e30c-7d88-8414-53d7a9be656b",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:37:21.771Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019db869-e30c-7d88-8414-53d7a9be656b",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "bob.kauffman@stellartech.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db869-e852-7564-a11b-d6d6ae61e90a/",
        "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-1",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:37:26.237Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "bob.kauffman@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:37:34.046Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "bob.kauffman@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:37:56.964Z",
      "eventType": "decision",
      "action": "bill_only_order.enqueue_upload_classification",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019db86a-720a-76e7-a399-383a55663c73",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:37:58.398Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019db86a-720a-76e7-a399-383a55663c73",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "bob.kauffman@stellartech.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db86a-775a-7a84-b70e-771eb029b318/",
        "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-2",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:38:07.649Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "bob.kauffman@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:38:29.898Z",
      "eventType": "decision",
      "action": "bill_only_order.enqueue_upload_classification",
      "userEmail": "bob.kauffman@stellartech.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:38:30.953Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019db86a-f2a5-7c88-a8c2-356185cd1b6b",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "bob.kauffman@stellartech.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db86a-f69c-7a1c-9a22-1931309162a9/",
        "subject": "New Bill-Only Order - 4/22/2026 - ZuriMED BO-3",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "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 1: Initial inventory",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 2: First order",
      "expectedAction": "decision:bill_only_order.enqueue_upload_classification",
      "found": true
    },
    {
      "stepName": "Step 4: Second order",
      "expectedAction": "decision:bill_only_order.enqueue_upload_classification",
      "found": true
    }
  ],
  "emailAssertions": [
    {
      "stepName": "Step 2: First order",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    },
    {
      "stepName": "Step 4: Second order",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    },
    {
      "stepName": "Step 5: Shortage edge case",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    }
  ]
}