{
  "ursId": "URS-030",
  "title": "Attach/Generate Required Documents for Returns",
  "timestamp": "2026-08-03T23:03:18.187Z",
  "durationMs": 106229,
  "config": {
    "inboxUrl": "http://localhost:38443",
    "dbHost": "localhost",
    "dbPort": 44149,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Navigate to returns",
      "status": "pass",
      "description": "Step 1: Navigate to returns",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-01-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-01-returns-list.png"
      ],
      "explanation": "A sales rep logs in and navigates to the returns section of the application. This verifies the returns feature is accessible before the document-generation flow begins.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:03:24.185Z",
      "finishedAt": "2026-08-03T23:03:25.756Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/videos/step-01-navigate-returns.webm"
    },
    {
      "name": "Step 2: Submit return",
      "status": "pass",
      "description": "Step 2: Submit return",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-02-review-before-submit.png",
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-02-submitted-with-document-notice.png"
      ],
      "explanation": "The sales rep completes the multi-step return form (account, event details with notes, product with lot number, required product question, locations) and submits it. The confirmation page must state that a return form document is being generated, showing the system attaches required documents to returns automatically.",
      "startedAt": "2026-08-03T23:04:07.795Z",
      "finishedAt": "2026-08-03T23:04:10.312Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/videos/step-02-submit-return.webm"
    },
    {
      "name": "Step 3: Document linked to return",
      "status": "pass",
      "description": "Step 3: Document linked to return",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-03-documents-card.png"
      ],
      "explanation": "The asynchronous document pipeline (Restate workflow → document generator → S3) is given time to complete, then the return details page is opened. The Documents card must list the generated return form with a \"System generated\" badge and a generation timestamp, proving the document is linked to the return.",
      "startedAt": "2026-08-03T23:04:16.825Z",
      "finishedAt": "2026-08-03T23:04:16.825Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/videos/step-03-document-linked.webm"
    },
    {
      "name": "Step 4: Download and verify PDF",
      "status": "pass",
      "description": "Step 4: Download and verify PDF",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-04-generated-pdf-content.png"
      ],
      "explanation": "The generated PDF is downloaded through the application (authenticated, org-scoped endpoint) and its text is extracted. The document must contain the return number, product name and SKU, lot number, facility, rep details, the notes entered on the form, and the product question answer — the required content for a return document. The rendered first page is captured as visual evidence.",
      "startedAt": "2026-08-03T23:04:54.991Z",
      "finishedAt": "2026-08-03T23:04:54.991Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/videos/step-04-download-verify-pdf.webm"
    },
    {
      "name": "Step 5: Document persists after reload",
      "status": "pass",
      "description": "Step 5: Document persists after reload",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/step-05-document-persists-after-reload.png"
      ],
      "explanation": "After navigating away, returning, and fully reloading the page, the generated document must still be listed on the return. This proves the document is durably attached to the return record rather than session state.",
      "startedAt": "2026-08-03T23:05:02.070Z",
      "finishedAt": "2026-08-03T23:05:02.070Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/videos/step-05-document-persists.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Return record created with test item",
      "query": "SELECT r.id, r.return_number, r.status, r.created_at, ri.lot_number\n      FROM returns r\n      JOIN return_items ri ON ri.return_id = r.id\n      WHERE r.created_by_user_id = $1\n        AND r.created_at > NOW() - INTERVAL '30 minutes'\n        AND ri.lot_number = $2\n      ORDER BY r.created_at DESC\n      LIMIT 1",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-cdc8-75e4-af68-fba296a40a87",
          "return_number": "RET-1",
          "status": "submitted",
          "created_at": "2026-08-03T23:04:09.785Z",
          "lot_number": "URS030-RETURN-001"
        }
      ],
      "assertion": "A return created by Blair Bennett in the last 30 minutes should contain an item with lot \"URS030-RETURN-001\""
    },
    {
      "name": "Generated document linked to return with S3 storage",
      "query": "SELECT d.id, d.return_id, r.return_number, d.status,\n        d.s3_bucket, d.s3_key, d.generated_by_user_id, d.organization_id,\n        d.created_at, d.updated_at\n      FROM return_form_documents d\n      JOIN returns r ON r.id = d.return_id\n      JOIN return_items ri ON ri.return_id = r.id\n      WHERE r.created_by_user_id = $1\n        AND r.created_at > NOW() - INTERVAL '30 minutes'\n        AND ri.lot_number = $2\n      ORDER BY d.created_at DESC",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-cf70-73f8-9837-c5532a75a431",
          "return_id": "019fc9de-cdc8-75e4-af68-fba296a40a87",
          "return_number": "RET-1",
          "status": "created",
          "s3_bucket": "df-inbox-documents-private-dev",
          "s3_key": "returns/a1b2c3d4-e5f6-7890-abcd-ef1234567890/019fc9de-cdc8-75e4-af68-fba296a40a87/2026-08-03T23-04-10-921Z-019fc9de.pdf",
          "generated_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456",
          "organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "created_at": "2026-08-03T23:04:10.353Z",
          "updated_at": "2026-08-03T23:04:10.942Z"
        }
      ],
      "assertion": "A return_form_documents row for the return should have status 'created' with s3_bucket and s3_key set"
    },
    {
      "name": "Document records the generating user and manufacturer org",
      "query": "SELECT d.id, d.return_id, r.return_number, d.status,\n        d.s3_bucket, d.s3_key, d.generated_by_user_id, d.organization_id,\n        d.created_at, d.updated_at\n      FROM return_form_documents d\n      JOIN returns r ON r.id = d.return_id\n      JOIN return_items ri ON ri.return_id = r.id\n      WHERE r.created_by_user_id = $1\n        AND r.created_at > NOW() - INTERVAL '30 minutes'\n        AND ri.lot_number = $2\n      ORDER BY d.created_at DESC",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-cf70-73f8-9837-c5532a75a431",
          "return_id": "019fc9de-cdc8-75e4-af68-fba296a40a87",
          "return_number": "RET-1",
          "status": "created",
          "s3_bucket": "df-inbox-documents-private-dev",
          "s3_key": "returns/a1b2c3d4-e5f6-7890-abcd-ef1234567890/019fc9de-cdc8-75e4-af68-fba296a40a87/2026-08-03T23-04-10-921Z-019fc9de.pdf",
          "generated_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456",
          "organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "created_at": "2026-08-03T23:04:10.353Z",
          "updated_at": "2026-08-03T23:04:10.942Z"
        }
      ],
      "assertion": "The generated document should record generated_by_user_id = Blair Bennett (submitter) and organization_id = Vantis (manufacturer)"
    },
    {
      "name": "No failed or stuck document generation attempts",
      "query": "SELECT d.id, d.status, d.created_at\n      FROM return_form_documents d\n      JOIN returns r ON r.id = d.return_id\n      JOIN return_items ri ON ri.return_id = r.id\n      WHERE r.created_by_user_id = $1\n        AND r.created_at > NOW() - INTERVAL '30 minutes'\n        AND ri.lot_number = $2\n        AND d.status != 'created'",
      "status": "pass",
      "rows": [],
      "assertion": "Every return_form_documents row for the return should have status 'created' (no 'error' or lingering 'pending' rows)"
    },
    {
      "name": "Document generated promptly after submission",
      "query": "SELECT d.id, r.return_number,\n        r.created_at AS return_created_at,\n        d.updated_at AS document_completed_at,\n        EXTRACT(EPOCH FROM (d.updated_at - r.created_at)) AS seconds_after_submission\n      FROM return_form_documents d\n      JOIN returns r ON r.id = d.return_id\n      JOIN return_items ri ON ri.return_id = r.id\n      WHERE r.created_by_user_id = $1\n        AND r.created_at > NOW() - INTERVAL '30 minutes'\n        AND ri.lot_number = $2\n        AND d.status = 'created'\n      ORDER BY d.created_at DESC\n      LIMIT 1",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-cf70-73f8-9837-c5532a75a431",
          "return_number": "RET-1",
          "return_created_at": "2026-08-03T23:04:09.785Z",
          "document_completed_at": "2026-08-03T23:04:10.942Z",
          "seconds_after_submission": "1.156673"
        }
      ],
      "assertion": "The document should be generated within 5 minutes of return submission (async pipeline)"
    },
    {
      "name": "No orphaned or cross-org documents",
      "query": "SELECT d.id, d.return_id, d.organization_id, r.manufacturer_organization_id\n      FROM return_form_documents d\n      LEFT JOIN returns r ON r.id = d.return_id\n      WHERE d.created_at > NOW() - INTERVAL '30 minutes'\n        AND (r.id IS NULL OR r.manufacturer_organization_id != d.organization_id)",
      "status": "pass",
      "rows": [],
      "assertion": "Every recent return_form_documents row must reference an existing return and carry that return's manufacturer organization id"
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z",
  "auditStartTime": "2026-08-03T23:03:16.280Z",
  "emailEvidence": [
    {
      "subject": "Return Submitted - RET-1",
      "template": "Return_Submitted_-_RET-1",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-030-return-required-documents/2026-08-03T23-03-18-187Z/screenshots/emails/2026-08-03T23-04-12-949Z-Return_Submitted_-_RET-1.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-08-03T23:03:16.307Z",
      "eventType": "decision",
      "action": "ensure_lot_expiration_checklist",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "secondaryObjectId": null,
      "payload": {
        "reason": "created_1_reopened_0_already_open_1",
        "performed": true,
        "entityType": "organization"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:22.540Z",
      "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:27.476Z",
      "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:04:09.785Z",
      "eventType": "decision",
      "action": "create_replacement_order_on_return",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-cdc8-75e4-af68-fba296a40a87",
      "secondaryObjectId": null,
      "payload": {
        "reason": "manufacturer_setting_disabled",
        "performed": false,
        "entityType": "return"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:09.785Z",
      "eventType": "return_request",
      "action": "created",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-cdc8-75e4-af68-fba296a40a87",
      "secondaryObjectId": null,
      "payload": {
        "itemCount": 1,
        "returnNumber": "RET-1",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:12.982Z",
      "eventType": "transactional_email",
      "action": "return_submitted",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-cdc8-75e4-af68-fba296a40a87",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9de-d9a3-73a5-8660-83757ccd17ef/",
        "subject": "Return Submitted - RET-1",
        "messageId": "dev-console-log",
        "attachmentFiles": [
          {
            "s3Key": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9de-d9a3-73a5-8660-83757ccd17ef/attachments/1-RET-1-return-form-2026-08-03.pdf",
            "filename": "RET-1-return-form-2026-08-03.pdf"
          }
        ],
        "relatedEntityType": "return"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:13.466Z",
      "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:04:50.770Z",
      "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:04:56.892Z",
      "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"
    }
  ],
  "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: Navigate to returns",
      "expectedAction": "user_log:user:login",
      "found": true
    }
  ],
  "emailAssertions": []
}