

Phase 1 — Driver KYC Request (Queue)

Driver App
    |
    |-- &quot;Start KYC&quot; button tap
    |
    └── Server pe queue mein add ho jao
            |
            └── Driver ko dikhao:
                &quot;Your token: #47&quot;
                &quot;People ahead: 3&quot;
                &quot;Estimated wait: ~15 mins&quot;

Phase 2 — Admin KYC Session Start
Admin Panel
    |
    └── Queue list dikhega:
        #45 - Rahul Kumar - Waiting
        #46 - Amit Singh - Waiting  
        #47 - Vijay Yadav - Waiting
            |
            └── Admin clicks &quot;Start KYC&quot; for next driver

Phase 3 — Live Video Session

Admin                          Driver
  |                               |
  |&lt;====== WebRTC Connected ======|
  |                               |
  | Step 1: Introduction          |
  | Admin talks to driver         |
  | (verify face liveness)        |
  |                               |
  | Step 2: Admin clicks          |
  | &quot;Show Aadhar&quot; button          |-- Notification → Driver App
  |                               |   &quot;Please show Aadhar card&quot;
  |                               |   Card frame overlay appears
  |                               |
  | Admin sees card in frame      |
  | Admin clicks &quot;Capture&quot;        |
  | Screenshot saved              |
  |                               |
  | Step 3: Admin clicks          |
  | &quot;Show PAN&quot; button             |-- Notification → Driver App  
  |                               |   &quot;Please show PAN card&quot;
  |                               |   Card frame overlay appears
  |                               |
  | Admin captures PAN            |
  |                               |
  | Step 4: Session End           |
  | Admin clicks &quot;Submit&quot;         |
  | All docs saved to DB          |
  | Recording saved               |

Phase 4 — Review &amp; Decision (Async — Later)

Admin Panel (anytime after session)
    |
    └── KYC Review Section
            |
            ├── Watch recording
            ├── View captured screenshots
            ├── Driver details
            └── Approve / Reject (with reason)
                    |
                    └── Driver ko push notification
                        &quot;KYC Approved ✅&quot; / &quot;KYC Rejected ❌&quot;

Table	          Purpose
tbl_kyc_queue-&gt;	Driver queue + token number
tbl_kyc_sessions-&gt;	Video session details + recording path
tbl_kyc_documents-&gt;	Aadhar/PAN/Face screenshots
tbl_driver.kyc_status-&gt;	Driver ka current KYC status
tbl_driver.kyc_session_id-&gt;	Latest session reference

these table i have created 
ALTER TABLE `tbl_driver` 
ADD COLUMN `kyc_status` enum(&#39;not_started&#39;,&#39;queued&#39;,&#39;in_progress&#39;,&#39;pending_review&#39;,&#39;approved&#39;,&#39;rejected&#39;) DEFAULT &#39;not_started&#39;,
ADD COLUMN `kyc_session_id` int DEFAULT NULL;


CREATE TABLE `tbl_kyc_queue` (
  `id` int NOT NULL AUTO_INCREMENT,
  `driver_id` int NOT NULL,
  `token_no` int NOT NULL,          -- e.g. 47
  `position` int NOT NULL,          -- queue mein kitne log pehle hain
  `status` enum(&#39;waiting&#39;,&#39;called&#39;,&#39;in_progress&#39;,&#39;completed&#39;,&#39;cancelled&#39;) DEFAULT &#39;waiting&#39;,
  `requested_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `called_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
);


CREATE TABLE `tbl_kyc_sessions` (
  `id` int NOT NULL AUTO_INCREMENT,
  `queue_id` int NOT NULL,
  `driver_id` int NOT NULL,
  `admin_id` int NOT NULL,
  `session_id` varchar(100) NOT NULL,   -- unique session identifier
  `status` enum(&#39;in_progress&#39;,&#39;pending_review&#39;,&#39;approved&#39;,&#39;rejected&#39;) DEFAULT &#39;in_progress&#39;,
  `recording_path` varchar(255) DEFAULT NULL,
  `started_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `ended_at` datetime DEFAULT NULL,
  `rejection_reason` text DEFAULT NULL,
  `reviewed_at` datetime DEFAULT NULL,
  `reviewed_by` int DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `tbl_kyc_documents` (
  `id` int NOT NULL AUTO_INCREMENT,
  `session_id` int NOT NULL,
  `driver_id` int NOT NULL,
  `doc_type` enum(&#39;face&#39;,&#39;aadhar&#39;,&#39;pan&#39;) NOT NULL,
  `image_path` varchar(255) NOT NULL,
  `captured_at` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);
 ab iske according index me events add kro

---

Looking at your existing code in `index.js`, I can see the KYC socket events are already implemented. Let me check what's there vs what's missing.

---

Check which KYC socket events already exist

---

shell

---

Check all KYC related lines

---

shell

---

Case-insensitive search for KYC events

---

shell

---

Check for KYC with special characters

---

shell

---

Check file line count and socket events count

---

shell

---

Use Python to search for KYC lines

---

shell

---

Check file encoding and last lines

---

shell

---

The file I read earlier via `fsRead` already had all the KYC events in it (the active file content shown in your message). But the actual file on disk doesn't have them — the active file content was showing a **partial/unsaved version**. The disk file is only 2350 lines with no KYC code.

Let me check what socket events exist and then add all KYC events:

---

List all existing socket.on events

---

shell

---

Now I know exactly what's on disk. I need to add all KYC events before the `disconnect` handler. Let me find that line:

---

Check exact content around disconnect handler

---

shell

---


Now I'll add all KYC events before the disconnect handler:

---

Adding all KYC socket events (Phase 1-4) before the disconnect handler.

---

Adding all KYC socket events before the disconnect handler.

---

please continue and add all events

---

Let me write the KYC events to a temp file and then inject them:

---

Find exact position of disconnect handler

---

shell

---

Create a Python script to inject KYC events into index.js before the disconnect handler

---

Run the injection script

---

shell

---

Verify all KYC socket events were added

---

shell

---

Verify the file has no syntax errors

---

shell

---

Clean up the temp injection script

---

shell

---


All 15 KYC socket events added successfully. Syntax is clean. Here's a summary of what was added:

Phase 1 — Driver Queue
- `kyc_join_queue` — join queue, get token number
- `kyc_get_queue_status` — check position & wait time
- `kyc_leave_queue` — leave queue

Phase 2 — Admin
- `kyc_get_queue_list` — see all waiting drivers
- `kyc_call_next_driver` — start session for next driver, notifies driver

Phase 3 — Live Session
- `kyc_join_session` — admin/driver join the session room
- `kyc_step_change` — admin changes step (introduction → show_aadhar → show_pan → complete)
- `kyc_capture_document` — admin captures face/aadhar/pan screenshot, saves to disk + DB
- `kyc_recording_start` — start recording
- `kyc_recording_chunk` — receive video chunks
- `kyc_recording_stop` — assemble & save .webm file
- `kyc_end_session` — end session, mark pending_review, notify driver

Phase 4 — Review & Decision
- `kyc_review_decision` — approve/reject with reason, push notification to driver
- `kyc_get_pending_reviews` — list all sessions pending review
- `kyc_get_session_details` — full session info with documents & recording URL

---

const kycQueue = [];
  let tokenCounter = 1;
  const kycSessions = new Map();
  const kycRecordingChunks = new Map();

  function getQueuePosition(driverId) {
    const index = kycQueue.findIndex(item =&gt; item.driver_id === driverId);
    return index === -1 ? -1 : index + 1;
  }

  function getQueueStats() {
    return {
      total_in_queue: kycQueue.length,
      current_token: kycQueue.length &gt; 0 ? kycQueue[0].token_no : null,
      next_tokens: kycQueue.slice(0, 5).map(item =&gt; ({
        token: item.token_no,
        driver_name: item.driver_name,
        waiting_time: Math.floor((Date.now() - item.requested_at) / 60000)
      }))
    };
  } these are in the connection i think this need to outside of the socket right?

---

Yes, exactly right. `kycQueue`, `kycSessions`, `kycRecordingChunks`, `tokenCounter`, `getQueuePosition`, and `getQueueStats` should be **outside** `io.on("connection", ...)` — otherwise they reset for every new socket connection.

---

Find exact lines of the KYC globals and io.on connection

---

shell

---

See exact content of the KYC globals block to know what to move

---

shell

---

Move KYC globals outside the connection handler

---

shell

---

Verify syntax is still clean

---

shell

---

Confirm globals are now before io.on connection

---

shell

---


Done. The globals are now at lines 83–103, outside `io.on("connection", ...)` which starts at line 105. This means:

- `kycQueue`, `kycSessions`, `kycRecordingChunks`, `tokenCounter` — persist across all socket connections (not reset per connection)
- `getQueuePosition()`, `getQueueStats()` — shared utility functions available to all socket handlers