{
  "ursId": "URS-005",
  "title": "Prevent Uncertified Reps from Creating Orders",
  "timestamp": "2026-08-03T23:01:57.259Z",
  "durationMs": 89173,
  "config": {
    "inboxUrl": "http://localhost:44117",
    "dbHost": "localhost",
    "dbPort": 43903,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Uncertified rep blocked",
      "status": "pass",
      "description": "Step 1: Uncertified rep blocked",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-01-ryan-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-01-order-blocked.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-01-order-request-blocked.png"
      ],
      "explanation": "Proves the system correctly prevents an uncertified representative from creating orders. Rana Reyes has no active manufacturer approval, so the bill-only order creation form displays a \"Manufacturer Approval Required\" warning and presents no selectable manufacturers. The same block appears on both the /billing/new and /orders/requests/new routes, confirming the enforcement is applied consistently across order entry points.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:05.873Z",
      "finishedAt": "2026-08-03T23:02:17.336Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/videos/step-01-uncertified-blocked.webm"
    },
    {
      "name": "Step 2: Certified rep control",
      "status": "pass",
      "description": "Step 2: Certified rep control",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-02-bob-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-02-order-form-accessible.png"
      ],
      "explanation": "Control condition confirming that the blocking behavior is targeted at uncertified reps only. Blair Bennett, whose representation relationship is active, can access the order creation form without any approval warning. The form auto-advances to Step 2 with Vantis pre-selected. This eliminates the possibility that the block in Step 1 is a general UI defect.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:22.986Z",
      "finishedAt": "2026-08-03T23:02:29.441Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/videos/step-02-certified-control.webm"
    },
    {
      "name": "Step 3: Manufacturer approval",
      "status": "pass",
      "description": "Step 3: Manufacturer approval",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-03-mark-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-03-ryan-pending-status.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-03-approval-dialog.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-03-ryan-active-status.png"
      ],
      "explanation": "The Vantis manufacturer user (Mark) navigates to Rana's representative detail page, confirms the status shows \"Pending Approval\", and clicks the \"Approve Representative\" button. After submission the status badge updates to \"Active\", demonstrating that the approval workflow functions end-to-end and transitions the relationship to the certified state required for order creation.",
      "expectedEmailTemplates": [
        "Representative Account Approved"
      ],
      "expectedAuditActions": [
        "user_log:rep_onboarding_request_approved"
      ],
      "startedAt": "2026-08-03T23:02:34.763Z",
      "finishedAt": "2026-08-03T23:02:50.588Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/videos/step-03-manufacturer-approval.webm"
    },
    {
      "name": "Step 4: Certified order creation",
      "status": "pass",
      "description": "Step 4: Certified order creation",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-ryan-logged-in-certified.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-form-accessible.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-account-selected.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-devices-selected.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-documents.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-review.png",
        "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/step-04-order-submitted.png"
      ],
      "explanation": "Validates that the now-approved representative can create and submit a bill-only order without restriction. Rana logs in after certification, accesses /billing/new without any approval warning, selects an account, adds a product with lot number, and successfully submits the order. The system redirects to /billing, confirming the requirement gate is lifted after manufacturer approval.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "expectedAuditActions": [
        "decision:bill_only_order.direct_po_import_on_create"
      ],
      "startedAt": "2026-08-03T23:02:55.366Z",
      "finishedAt": "2026-08-03T23:03:24.833Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/videos/step-04-certified-order.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Rana relationship now active",
      "query": "SELECT id, status, active, responded_at, responded_by_user_id\n      FROM organization_representation_relationships\n      WHERE id = $1",
      "status": "pass",
      "rows": [
        {
          "id": "95d6e7f8-a9b0-1234-9012-345678901234",
          "status": "active",
          "active": true,
          "responded_at": "2026-08-03T23:02:44.032Z",
          "responded_by_user_id": "d4e5f6a7-b8c9-0123-def1-234567890123"
        }
      ],
      "assertion": "Rana's representation relationship should be active after manufacturer approval"
    },
    {
      "name": "Blair relationship still active (control)",
      "query": "SELECT id, status, active\n      FROM organization_representation_relationships\n      WHERE id = $1",
      "status": "pass",
      "rows": [
        {
          "id": "84c5d6e7-f8a9-0123-8901-234567890123",
          "status": "proposed_pending_onboarding",
          "active": true
        }
      ],
      "assertion": "Blair's active flag should remain true and unaffected by Rana's approval flow"
    },
    {
      "name": "Status change history recorded",
      "query": "SELECT id, to_status, from_status, changed_by_user_id, created_at\n      FROM organization_representation_request_status_changes\n      WHERE relationship_id = $1\n        AND created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY created_at DESC\n      LIMIT 5",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9dd-7e42-7d8d-974e-0f0bfb1bfc30",
          "to_status": "active",
          "from_status": "proposed",
          "changed_by_user_id": "d4e5f6a7-b8c9-0123-def1-234567890123",
          "created_at": "2026-08-03T23:02:43.954Z"
        }
      ],
      "assertion": "Status change to \"active\" should be recorded in history table"
    },
    {
      "name": "Billing order created by Rana after certification",
      "query": "SELECT bo.id, bo.order_number, bo.status, 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": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
          "order_number": "BO-1",
          "status": "submitted",
          "created_at": "2026-08-03T23:03:20.955Z",
          "created_by_user_id": "28c9d0e1-f2a3-4567-2345-678901234567"
        }
      ],
      "assertion": "At least one billing order should have been created by Rana after being certified"
    },
    {
      "name": "Audit trail for representative approval",
      "query": "SELECT ae.id, ae.event_type, ae.action, ae.created_at, ae.user_id, ae.object_id,\n        substring(ae.payload::text, 1, 500) as payload_preview\n      FROM audit_events ae\n      WHERE ae.created_at > NOW() - INTERVAL '30 minutes'\n        AND (ae.object_id = $1 OR ae.object_id = $2)\n      ORDER BY ae.created_at DESC\n      LIMIT 10",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9dd-8273-7392-8893-8c8de17aff6d",
          "event_type": "transactional_email",
          "action": "rep_created",
          "created_at": "2026-08-03T23:02:45.151Z",
          "user_id": null,
          "object_id": "95d6e7f8-a9b0-1234-9012-345678901234",
          "payload_preview": "{\"to\": [\"rana.reyes@corvetasurgical.com\"], \"s3Path\": \"email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9dd-8273-7392-8893-8c8de17aff6d/\", \"subject\": \"Representative Account Approved - Vantis\", \"messageId\": \"dev-console-log\", \"relatedEntityType\": \"organization_representation_relationship\"}"
        },
        {
          "id": "019fc9dd-7e43-7ada-8d66-bbf6dfc8d618",
          "event_type": "user_log",
          "action": "rep_onboarding_request_approved",
          "created_at": "2026-08-03T23:02:44.083Z",
          "user_id": "d4e5f6a7-b8c9-0123-def1-234567890123",
          "object_id": "95d6e7f8-a9b0-1234-9012-345678901234",
          "payload_preview": "{\"userId\": \"28c9d0e1-f2a3-4567-2345-678901234567\", \"userName\": \"Rana Reyes\", \"userEmail\": \"rana.reyes@corvetasurgical.com\", \"distributorOrganizationId\": \"b2c3d4e5-f6a7-8901-bcde-f12345678901\", \"manufacturerOrganizationId\": \"a1b2c3d4-e5f6-7890-abcd-ef1234567890\"}"
        }
      ],
      "assertion": "Audit/decision events should exist referencing Rana after the approval action"
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z",
  "auditStartTime": "2026-08-03T23:01:55.619Z",
  "emailEvidence": [
    {
      "subject": "Representative Account Approved - Vantis",
      "template": "Representative_Account_Approved_-_Vantis",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/emails/2026-08-03T23-02-45-081Z-Representative_Account_Approved_-_Vantis.png"
    },
    {
      "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-1",
      "template": "New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-1",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-005-uncertified-rep-orders/2026-08-03T23-01-57-259Z/screenshots/emails/2026-08-03T23-03-22-260Z-New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-1.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-08-03T23:02:03.742Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "rana.reyes@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:21.226Z",
      "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:02:33.050Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "mark.manufacturer@vantismedical.com",
      "userId": "d4e5f6a7-b8c9-0123-def1-234567890123",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "mark.manufacturer@vantismedical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:44.083Z",
      "eventType": "user_log",
      "action": "rep_onboarding_request_approved",
      "userEmail": "mark.manufacturer@vantismedical.com",
      "userId": "d4e5f6a7-b8c9-0123-def1-234567890123",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "95d6e7f8-a9b0-1234-9012-345678901234",
      "secondaryObjectId": "95d6e7f8-a9b0-1234-9012-345678901234",
      "payload": {
        "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
        "userName": "Rana Reyes",
        "userEmail": "rana.reyes@corvetasurgical.com",
        "distributorOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": "/representatives/[userId]",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:45.151Z",
      "eventType": "transactional_email",
      "action": "rep_created",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "95d6e7f8-a9b0-1234-9012-345678901234",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "rana.reyes@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9dd-8273-7392-8893-8c8de17aff6d/",
        "subject": "Representative Account Approved - Vantis",
        "messageId": "dev-console-log",
        "relatedEntityType": "organization_representation_relationship"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:53.797Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "rana.reyes@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:03:20.955Z",
      "eventType": "decision",
      "action": "bill_only.link_purchase_order",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
      "secondaryObjectId": null,
      "payload": {
        "reason": "no_purchase_order_selected",
        "performed": false,
        "entityType": "billing_order",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:20.955Z",
      "eventType": "decision",
      "action": "bill_only_order.inventory_items_decrement",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
      "secondaryObjectId": null,
      "payload": {
        "reason": "inventory_items_decremented",
        "itemCount": 1,
        "performed": true,
        "entityType": "billing_order",
        "inventorySource": "inventory_items"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:20.955Z",
      "eventType": "decision",
      "action": "bill_only_order.direct_po_import_on_create",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
      "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-08-03T23:03:20.955Z",
      "eventType": "billing_order",
      "action": "status_change",
      "userEmail": "rana.reyes@corvetasurgical.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
      "secondaryObjectId": null,
      "payload": {
        "reason": null,
        "toStatus": "submitted",
        "fromStatus": null,
        "automatedAction": false
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:22.311Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-0ec0-7591-8113-f2bdbf02d0c8",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "rana.reyes@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9de-13a1-75e0-8ec9-f9c9683b7f93/",
        "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-1",
        "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: Uncertified rep blocked",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 2: Certified rep control",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 3: Manufacturer approval",
      "expectedAction": "user_log:rep_onboarding_request_approved",
      "found": true
    },
    {
      "stepName": "Step 4: Certified order creation",
      "expectedAction": "decision:bill_only_order.direct_po_import_on_create",
      "found": true
    }
  ],
  "emailAssertions": [
    {
      "stepName": "Step 3: Manufacturer approval",
      "expectedTemplate": "Representative Account Approved",
      "found": true
    },
    {
      "stepName": "Step 4: Certified order creation",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    }
  ]
}