{
  "ursId": "URS-004",
  "title": "Admin approve or reject a sales representative",
  "timestamp": "2026-08-03T23:01:56.937Z",
  "durationMs": 67161,
  "config": {
    "inboxUrl": "http://localhost:37779",
    "dbHost": "localhost",
    "dbPort": 38971,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Admin navigates to rep management",
      "status": "pass",
      "description": "Step 1: Admin navigates to rep management",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-01-admin-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-01-rep-list.png"
      ],
      "explanation": "Proves that a Vantis admin (Alex) can authenticate and reach the Representatives management page. Both Corveta reps — Blair (pending approval) and Rana (active) — appear in the roster, establishing admin context for the approve/reject actions that follow.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:06.536Z",
      "finishedAt": "2026-08-03T23:02:13.032Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-01-admin-navigate.webm"
    },
    {
      "name": "Step 2: Admin rejects Blair",
      "status": "pass",
      "description": "Step 2: Admin rejects Blair",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-02-bob-detail-pending.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-02-reject-dialog.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-02-bob-detail-rejected.png"
      ],
      "explanation": "Alex opens Blair's detail page, clicks Reject Representative, supplies a rejection reason, and confirms. Proves that an admin has an in-app control to deny a rep onboarding. The relationship row moves to status='rejected', active=false, and a rep_creation_request_rejected notification is dispatched to the distributor.",
      "expectedEmailTemplates": [
        "Representative Request Rejected"
      ],
      "expectedAuditActions": [
        "user_log:rep_onboarding_request_rejected"
      ],
      "startedAt": "2026-08-03T23:02:19.747Z",
      "finishedAt": "2026-08-03T23:02:23.804Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-02-admin-rejects.webm"
    },
    {
      "name": "Step 3: Blair blocked from creating orders",
      "status": "pass",
      "description": "Step 3: Blair blocked from creating orders",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-03-bob-no-vantis.png"
      ],
      "explanation": "Blair logs in after being rejected and opens the order creation page. Vantis is filtered out of the manufacturer dropdown because `getAvailableFulfillingOrganizations` requires an active representation relationship. Proves the admin action is a hard restriction on downstream order creation — not just a cosmetic UI change.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:31.446Z",
      "finishedAt": "2026-08-03T23:02:31.446Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-03-bob-blocked.webm"
    },
    {
      "name": "Step 4: Rana (control) can still create orders",
      "status": "pass",
      "description": "Step 4: Rana (control) can still create orders",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-04-ryan-order-form.png"
      ],
      "explanation": "Rana, an unrelated Corveta rep, is unaffected by the action taken on Blair. He reaches Step 2 of the order form as normal, proving that admin decisions are scoped to the rep under review and do not inadvertently strip privileges from other reps.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:38.224Z",
      "finishedAt": "2026-08-03T23:02:38.224Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-04-ryan-control.webm"
    },
    {
      "name": "Step 5: Admin approves Blair",
      "status": "pass",
      "description": "Step 5: Admin approves Blair",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-05-bob-detail-pending-again.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-05-approve-dialog.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-05-bob-detail-approved.png"
      ],
      "explanation": "After the test harness returns Blair's status to 'proposed', Alex approves him through the UI's Approve Representative flow. The relationship row becomes status='active', active=true; a rep_created notification is dispatched to Corveta announcing the approval.",
      "expectedEmailTemplates": [
        "Representative Account Approved"
      ],
      "expectedAuditActions": [
        "user_log:rep_onboarding_request_approved"
      ],
      "startedAt": "2026-08-03T23:02:44.741Z",
      "finishedAt": "2026-08-03T23:02:46.929Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-05-admin-approves.webm"
    },
    {
      "name": "Step 6: Blair submits an order after approval",
      "status": "pass",
      "description": "Step 6: Blair submits an order after approval",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-06-order-step2.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-06-order-review.png",
        "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/step-06-order-submitted.png"
      ],
      "explanation": "Now-approved Blair logs back in, reaches the order form, picks a delivery location, adds a product, and submits. Proves the restriction lifts immediately on approval — the admin's status change takes effect without any further action. The order request persists in the database for the DB-validation phase to confirm.",
      "expectedAuditActions": [
        "user_log:user:login",
        "decision:order_request_created"
      ],
      "startedAt": "2026-08-03T23:02:56.153Z",
      "finishedAt": "2026-08-03T23:03:01.092Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/videos/step-06-bob-orders.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Blair's relationship ended the run as active",
      "query": "SELECT id, status, active, rejection_reason\n                   FROM organization_representation_relationships\n                  WHERE id = $1",
      "status": "pass",
      "rows": [
        {
          "id": "84c5d6e7-f8a9-0123-8901-234567890123",
          "status": "active",
          "active": true,
          "rejection_reason": null
        }
      ],
      "assertion": "After the approve flow Blair's relationship should be status='active', active=true."
    },
    {
      "name": "Rejection status transition recorded",
      "query": "SELECT from_status, to_status, reason, created_at\n                   FROM organization_representation_request_status_changes\n                  WHERE relationship_id = $1\n                    AND to_status = 'rejected'\n                    AND created_at > NOW() - INTERVAL '30 minutes'\n                  ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "from_status": "proposed",
          "to_status": "rejected",
          "reason": "URS-004 validation test: admin rejecting rep to verify order-creation is blocked.",
          "created_at": "2026-08-03T23:02:23.066Z"
        }
      ],
      "assertion": "A proposed→rejected transition should have been recorded during the test run."
    },
    {
      "name": "Approval status transition recorded",
      "query": "SELECT from_status, to_status, created_at\n                   FROM organization_representation_request_status_changes\n                  WHERE relationship_id = $1\n                    AND to_status = 'active'\n                    AND created_at > NOW() - INTERVAL '30 minutes'\n                  ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "from_status": "proposed",
          "to_status": "active",
          "created_at": "2026-08-03T23:02:46.236Z"
        }
      ],
      "assertion": "A proposed→active transition should have been recorded during the test run."
    },
    {
      "name": "Approved rep successfully submitted an order",
      "query": "SELECT id, request_number, status, order_type, created_at\n                   FROM order_requests\n                  WHERE requested_by_user_id = $1\n                    AND notes LIKE $2\n                  ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9dd-c030-787c-984c-7a3b0e7cdb7d",
          "request_number": "OR-1",
          "status": "submitted",
          "order_type": "dropship",
          "created_at": "2026-08-03T23:03:00.889Z"
        }
      ],
      "assertion": "At least one order_requests row should exist, tagged with the URS-004 marker."
    },
    {
      "name": "Audit events logged for approve and reject",
      "query": "SELECT event_type, action, created_at\n                   FROM audit_events\n                  WHERE event_type = 'user_log'\n                    AND action IN ('rep_onboarding_request_approved', 'rep_onboarding_request_rejected')\n                    AND created_at > NOW() - INTERVAL '30 minutes'\n                  ORDER BY created_at DESC",
      "status": "pass",
      "rows": [
        {
          "event_type": "user_log",
          "action": "rep_onboarding_request_approved",
          "created_at": "2026-08-03T23:02:46.309Z"
        },
        {
          "event_type": "user_log",
          "action": "rep_onboarding_request_rejected",
          "created_at": "2026-08-03T23:02:23.285Z"
        }
      ],
      "assertion": "audit_events should include both rep_onboarding_request_approved and rep_onboarding_request_rejected actions from this run."
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z",
  "auditStartTime": "2026-08-03T23:01:55.225Z",
  "emailEvidence": [
    {
      "subject": "Representative Request Rejected - Vantis",
      "template": "Representative_Request_Rejected_-_Vantis",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/emails/2026-08-03T23-02-24-427Z-Representative_Request_Rejected_-_Vantis.png"
    },
    {
      "subject": "Representative Account Approved - Vantis",
      "template": "Representative_Account_Approved_-_Vantis",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-004-admin-approve-reject-rep/2026-08-03T23-01-56-921Z/screenshots/emails/2026-08-03T23-02-47-073Z-Representative_Account_Approved_-_Vantis.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-08-03T23:02:04.062Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "alex.admin@vantismedical.com",
      "userId": "f6a7b8c9-d0e1-2345-f123-456789012345",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "alex.admin@vantismedical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:15.802Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "alex.admin@vantismedical.com",
      "userId": "f6a7b8c9-d0e1-2345-f123-456789012345",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "alex.admin@vantismedical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:23.285Z",
      "eventType": "user_log",
      "action": "rep_onboarding_request_rejected",
      "userEmail": "alex.admin@vantismedical.com",
      "userId": "f6a7b8c9-d0e1-2345-f123-456789012345",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "84c5d6e7-f8a9-0123-8901-234567890123",
      "secondaryObjectId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "payload": {
        "userName": "Blair Bennett",
        "userEmail": "blair.bennett@corvetasurgical.com",
        "rejectionReason": "URS-004 validation test: admin rejecting rep to verify order-creation is blocked."
      },
      "route": "/representatives/[userId]",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:24.516Z",
      "eventType": "transactional_email",
      "action": "rep_creation_request_rejected",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9dd-31c4-7f64-be5a-7064939dac1e/",
        "subject": "Representative Request Rejected - Vantis",
        "messageId": "dev-console-log",
        "relatedEntityId": "84c5d6e7-f8a9-0123-8901-234567890123",
        "relatedEntityType": "organization_representation_relationship"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:26.502Z",
      "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:35.132Z",
      "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:41.739Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "alex.admin@vantismedical.com",
      "userId": "f6a7b8c9-d0e1-2345-f123-456789012345",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "alex.admin@vantismedical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:46.309Z",
      "eventType": "user_log",
      "action": "rep_onboarding_request_approved",
      "userEmail": "alex.admin@vantismedical.com",
      "userId": "f6a7b8c9-d0e1-2345-f123-456789012345",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "84c5d6e7-f8a9-0123-8901-234567890123",
      "secondaryObjectId": "84c5d6e7-f8a9-0123-8901-234567890123",
      "payload": {
        "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
        "userName": "Blair Bennett",
        "userEmail": "blair.bennett@corvetasurgical.com",
        "distributorOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": "/representatives/[userId]",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:47.113Z",
      "eventType": "transactional_email",
      "action": "rep_created",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9dd-8a2f-700d-95b3-944f8005e9b3/",
        "subject": "Representative Account Approved - Vantis",
        "messageId": "dev-console-log",
        "relatedEntityId": "84c5d6e7-f8a9-0123-8901-234567890123",
        "relatedEntityType": "organization_representation_relationship"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:49.402Z",
      "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:03:00.889Z",
      "eventType": "checklist",
      "action": "checklists.create",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9dd-c041-7782-9d36-90b4c905af21",
      "secondaryObjectId": null,
      "payload": {
        "title": "Order OR-1",
        "source": {
          "type": "order_request",
          "orderRequestId": "019fc9dd-c030-787c-984c-7a3b0e7cdb7d"
        }
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:00.889Z",
      "eventType": "decision",
      "action": "order_request_created",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9dd-c030-787c-984c-7a3b0e7cdb7d",
      "secondaryObjectId": null,
      "payload": {
        "reason": "Order request OR-1 created (importSource=manual)",
        "priority": "normal",
        "itemCount": 1,
        "orderType": "dropship",
        "performed": true,
        "entityType": "order_request",
        "importSource": "manual",
        "requestNumber": "OR-1",
        "selfFulfilled": false,
        "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 1: Admin navigates to rep management",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 2: Admin rejects Blair",
      "expectedAction": "user_log:rep_onboarding_request_rejected",
      "found": true
    },
    {
      "stepName": "Step 3: Blair blocked from creating orders",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 4: Rana (control) can still create orders",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 5: Admin approves Blair",
      "expectedAction": "user_log:rep_onboarding_request_approved",
      "found": true
    },
    {
      "stepName": "Step 6: Blair submits an order after approval",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 6: Blair submits an order after approval",
      "expectedAction": "decision:order_request_created",
      "found": true
    }
  ],
  "emailAssertions": [
    {
      "stepName": "Step 2: Admin rejects Blair",
      "expectedTemplate": "Representative Request Rejected",
      "found": true
    },
    {
      "stepName": "Step 5: Admin approves Blair",
      "expectedTemplate": "Representative Account Approved",
      "found": true
    }
  ]
}