{
  "ursId": "URS-055",
  "title": "Restrict uncertified reps from actions until approved",
  "timestamp": "2026-04-23T03:43:31.759Z",
  "durationMs": 114852,
  "config": {
    "inboxUrl": "http://localhost:65013",
    "dbHost": "localhost",
    "dbPort": 65014,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Bill-only blocked",
      "status": "pass",
      "description": "Step 1: Bill-only blocked",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-01-ryan-dashboard.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-01-billing-blocked.png"
      ],
      "explanation": "An uncertified sales rep attempts to create a bill-only order. The system blocks access to the bill-only form, confirming that the certification requirement is enforced before any order can be submitted.",
      "startedAt": "2026-04-23T03:43:36.634Z",
      "finishedAt": "2026-04-23T03:43:41.943Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-01-billing-blocked.webm"
    },
    {
      "name": "Step 2: Order request blocked",
      "status": "pass",
      "description": "Step 2: Order request blocked",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-02-order-request-blocked.png"
      ],
      "explanation": "The same uncertified rep attempts to submit a standard order request. The system again blocks access, demonstrating that both order types are restricted until the rep obtains manufacturer certification.",
      "startedAt": "2026-04-23T03:43:51.677Z",
      "finishedAt": "2026-04-23T03:43:51.677Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-02-order-request-blocked.webm"
    },
    {
      "name": "Step 3: Certified rep control",
      "status": "pass",
      "description": "Step 3: Certified rep control",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-03-bob-billing-form.png"
      ],
      "explanation": "A certified sales rep navigates through the bill-only form without being blocked. The form heading and step indicator both render, and the \"Manufacturer Approval Required\" warning is absent — confirming the restriction is applied only to uncertified reps.\n\nBob's relationship row has `active = true` in the fixtures, which is the column the gating logic (`getAvailableFulfillingOrganizations`) consults. The `status` column in the demo data may read `proposed_pending_onboarding` rather than `active` because the seed set preserves the original onboarding history — but the boolean `active` flag is authoritative for whether the rep may place orders, which is why the DB assertion in this run checks `active = true` for the control case rather than the `status` string.",
      "startedAt": "2026-04-23T03:44:01.462Z",
      "finishedAt": "2026-04-23T03:44:01.462Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-03-certified-control.webm"
    },
    {
      "name": "Step 4: Manufacturer approval",
      "status": "pass",
      "description": "Step 4: Manufacturer approval",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-04-ryan-pending.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-04-approval-dialog.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-04-ryan-active.png"
      ],
      "explanation": "A manufacturer user reviews the pending rep certification request and approves it, changing the relationship status from pending to active. This step generates the approval notification that certifies the previously blocked rep.",
      "expectedAuditActions": [
        "user_log:rep_onboarding_request_approved"
      ],
      "startedAt": "2026-04-23T03:44:11.248Z",
      "finishedAt": "2026-04-23T03:44:18.786Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-04-manufacturer-approval.webm"
    },
    {
      "name": "Step 5: Bill-only after certification",
      "status": "pass",
      "description": "Step 5: Bill-only after certification",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-05-billing-form-accessible.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-05-devices-selected.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-05-review.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-05-order-submitted.png"
      ],
      "explanation": "The newly certified rep navigates to the bill-only form and is no longer blocked. The form is accessible and the order is submitted successfully, confirming that certification takes immediate effect.",
      "startedAt": "2026-04-23T03:44:28.430Z",
      "finishedAt": "2026-04-23T03:44:51.534Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-05-billing-submitted.webm"
    },
    {
      "name": "Step 6: Order request after certification",
      "status": "pass",
      "description": "Step 6: Order request after certification",
      "screenshots": [
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-06-order-request-form-accessible.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-06-order-request-product.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-06-order-request-review.png",
        "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/step-06-order-request-submitted.png"
      ],
      "explanation": "The certified rep navigates to the standard order request form without the approval warning, then submits a consignment order end-to-end. A DB assertion subsequently confirms that a row was persisted to the `order_requests` table — proving the post-certification path is unlocked at the database level, not just the UI level.",
      "startedAt": "2026-04-23T03:45:01.340Z",
      "finishedAt": "2026-04-23T03:45:21.755Z",
      "videoPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/videos/step-06-order-request-submitted.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Ryan 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-04-23T03:44:15.773Z",
          "responded_by_user_id": "d4e5f6a7-b8c9-0123-def1-234567890123"
        }
      ],
      "assertion": "Ryan's representation relationship should be active after manufacturer approval"
    },
    {
      "name": "Bob 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": "Bob's relationship should remain `active = true` (the column the certification gate actually reads) and be unaffected by Ryan's approval. The `status` string in this row is informational only and may read `proposed_pending_onboarding` from the seed data."
    },
    {
      "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": "019db870-399e-7a4f-84a9-4f518efc6f49",
          "to_status": "active",
          "from_status": "proposed",
          "changed_by_user_id": "d4e5f6a7-b8c9-0123-def1-234567890123",
          "created_at": "2026-04-23T03:44:15.744Z"
        }
      ],
      "assertion": "Status change to \"active\" should be recorded in history table"
    },
    {
      "name": "Bill-only order created by Ryan 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": "019db870-bc20-789f-a38d-55a1432563ae",
          "order_number": "BO-1",
          "status": "submitted",
          "created_at": "2026-04-23T03:44:49.158Z",
          "created_by_user_id": "28c9d0e1-f2a3-4567-2345-678901234567"
        }
      ],
      "assertion": "At least one bill-only order should have been created by Ryan after being certified"
    },
    {
      "name": "Order request created by Ryan after certification",
      "query": "SELECT id, request_number, order_type, status, sales_account_id, notes, created_at\n      FROM order_requests\n      WHERE requested_by_user_id = $1\n        AND notes LIKE $2\n        AND created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY created_at DESC\n      LIMIT 5",
      "status": "pass",
      "rows": [
        {
          "id": "019db871-3336-7dc9-bd5b-28e6e1bc4387",
          "request_number": "OR-2",
          "order_type": "consignment",
          "status": "submitted",
          "sales_account_id": "fea7b8c9-d0e1-2345-0123-456789012345",
          "notes": "URS-055: post-certification order request",
          "created_at": "2026-04-23T03:45:19.655Z"
        }
      ],
      "assertion": "A standard order request should have been persisted by Ryan after certification (Step 6) — tagged with the URS-055 notes marker"
    },
    {
      "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": "019db870-39b0-7bc3-b69d-4076f39563f4",
          "event_type": "transactional_email",
          "action": "rep_created",
          "created_at": "2026-04-23T03:44:15.804Z",
          "user_id": null,
          "object_id": "95d6e7f8-a9b0-1234-9012-345678901234",
          "payload_preview": "{\"to\": [\"ryan.delauintana@stellartech.com\"], \"s3Path\": \"email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db870-39b0-7bc3-b69d-4076f39563f4/\", \"subject\": \"Representative Account Approved - ZuriMED\", \"messageId\": \"dev-console-log\", \"relatedEntityType\": \"organization_representation_relationship\"}"
        },
        {
          "id": "019db870-399f-770b-84a3-365054e1902b",
          "event_type": "user_log",
          "action": "rep_onboarding_request_approved",
          "created_at": "2026-04-23T03:44:15.781Z",
          "user_id": "d4e5f6a7-b8c9-0123-def1-234567890123",
          "object_id": "95d6e7f8-a9b0-1234-9012-345678901234",
          "payload_preview": "{\"userId\": \"28c9d0e1-f2a3-4567-2345-678901234567\", \"userName\": \"Ryan Delauintana\", \"userEmail\": \"ryan.delauintana@stellartech.com\", \"distributorOrganizationId\": \"b2c3d4e5-f6a7-8901-bcde-f12345678901\", \"manufacturerOrganizationId\": \"a1b2c3d4-e5f6-7890-abcd-ef1234567890\"}"
        }
      ],
      "assertion": "Audit/decision events should exist referencing Ryan after the approval action"
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z",
  "auditStartTime": "2026-04-23T03:43:29.845Z",
  "emailEvidence": [
    {
      "subject": "Representative Account Approved - ZuriMED",
      "template": "Representative_Account_Approved_-_ZuriMED",
      "screenshotPath": "/Users/jason/projects/casecohort/code/validation_test_results/urs-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/emails/2026-04-23T03-44-15-788Z-Representative_Account_Approved_-_ZuriMED.png"
    },
    {
      "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-055-uncertified-rep-restrictions/2026-04-23T03-43-31-758Z/screenshots/emails/2026-04-23T03-44-50-446Z-New_Bill-Only_Order_-_4_22_2026_-_ZuriMED_BO-1.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-04-23T03:43:33.980Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "ryan.delauintana@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:43:46.420Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "ryan.delauintana@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:43:56.183Z",
      "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:44:05.909Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "mark.manufacturer@zurimed.com",
      "userId": "d4e5f6a7-b8c9-0123-def1-234567890123",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "mark.manufacturer@zurimed.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:44:15.781Z",
      "eventType": "user_log",
      "action": "rep_onboarding_request_approved",
      "userEmail": "mark.manufacturer@zurimed.com",
      "userId": "d4e5f6a7-b8c9-0123-def1-234567890123",
      "organizationName": "ZuriMED",
      "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": "Ryan Delauintana",
        "userEmail": "ryan.delauintana@stellartech.com",
        "distributorOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": "/representatives/[userId]",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:44:15.804Z",
      "eventType": "transactional_email",
      "action": "rep_created",
      "userEmail": null,
      "userId": null,
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "95d6e7f8-a9b0-1234-9012-345678901234",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "ryan.delauintana@stellartech.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db870-39b0-7bc3-b69d-4076f39563f4/",
        "subject": "Representative Account Approved - ZuriMED",
        "messageId": "dev-console-log",
        "relatedEntityType": "organization_representation_relationship"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-04-23T03:44:23.237Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "ryan.delauintana@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:44:49.158Z",
      "eventType": "decision",
      "action": "bill_only_order.enqueue_upload_classification",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019db870-bc20-789f-a38d-55a1432563ae",
      "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:44:50.459Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019db870-bc20-789f-a38d-55a1432563ae",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "ryan.delauintana@stellartech.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019db870-c112-7402-a598-041bff46eee8/",
        "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:44:56.030Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "StellarTech Medical Solutions",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "ryan.delauintana@stellartech.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-04-23T03:45:19.655Z",
      "eventType": "decision",
      "action": "order_request_created",
      "userEmail": "ryan.delauintana@stellartech.com",
      "userId": "28c9d0e1-f2a3-4567-2345-678901234567",
      "organizationName": "ZuriMED",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019db871-3336-7dc9-bd5b-28e6e1bc4387",
      "secondaryObjectId": null,
      "payload": {
        "reason": "Order request OR-2 created (importSource=manual)",
        "priority": "normal",
        "itemCount": 1,
        "orderType": "consignment",
        "performed": true,
        "entityType": "order_request",
        "importSource": "manual",
        "requestNumber": "OR-2",
        "resolvedLocationId": "6ea3b4c5-d6e7-8901-6789-012345678901",
        "resolvedSalesAccountId": "fea7b8c9-d0e1-2345-0123-456789012345",
        "fulfillingOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
        "requestingOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901"
      },
      "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 4: Manufacturer approval",
      "expectedAction": "user_log:rep_onboarding_request_approved",
      "found": true
    }
  ],
  "emailAssertions": []
}