# Deployment Guide: Org-Recruiter API Integration

## Overview

This document covers deploying the org-recruiter API integration that allows the new recruiter to call org-recruiter V3 APIs for notifications (WhatsApp, Firebase push, in-app).

---

## Prerequisites

Before deploying, ensure:
1. Local testing is complete
2. Feature flag `USE_ORG_RECRUITER_API` tested as `true`
3. WhatsApp notifications re-enabled in NotificationService.php (remove the early return)

---

## Part 1: Deploy org-recruiter Changes

### Files Changed
- `application/core/MY_Controller.php` - Added internal API key bypass

### Step-by-Step

```bash
cd /path/to/org-recruiter

# 1. Stash current changes (if on production branch)
git stash

# 2. Checkout and pull latest uat
git checkout uat
git pull origin uat

# 3. Create feature branch FROM uat
git checkout -b feature/internal-api-key-bypass

# 4. Apply your change
git stash pop
# OR if starting fresh, manually add the code block to MY_Controller.php

# 5. Verify the change
git diff application/core/MY_Controller.php

# 6. Stage and commit
git add application/core/MY_Controller.php
git commit -m "Add internal API key bypass for new recruiter integration

- Allows server-to-server API calls from new recruiter dashboard
- Reads INTERNAL_API_KEY from environment variable
- Falls back to default for local testing
- Does not affect existing mobile app or NextJS flows"

# 7. Push feature branch
git push origin feature/internal-api-key-bypass
```

### Create Merge Request (GitLab)

1. Go to: https://gitlab.innowrap.co.in/innowrap/mission-even/-/merge_requests/new
2. **Source branch:** `feature/internal-api-key-bypass`
3. **Target branch:** `uat`
4. **Title:** "Add internal API key bypass for new recruiter integration"
5. **Description:**
   ```
   ## Summary
   Adds internal API key bypass to allow server-to-server communication from the new recruiter dashboard.

   ## Changes
   - Added check for X-Internal-Api-Key header in validate_request()
   - Reads INTERNAL_API_KEY from environment variable
   - Falls back to default for local testing

   ## Testing
   - Tested locally with new recruiter calling changeAppliedCandidateStatus API
   - Existing mobile app and NextJS flows unaffected (they don't send this header)

   ## Deployment Notes
   Set INTERNAL_API_KEY environment variable on server after merge
   ```

### After UAT Testing

Create another MR:
- **Source:** `uat`
- **Target:** `production`

---

## Part 2: Server Environment Variables

### UAT Server

Add to Apache envvars, nginx config, or systemd service:

```bash
# Option 1: Apache (/etc/apache2/envvars)
export INTERNAL_API_KEY="kaabil-uat-internal-key-2026"

# Option 2: Nginx (in location block)
fastcgi_param INTERNAL_API_KEY "kaabil-uat-internal-key-2026";

# Option 3: systemd service file
Environment="INTERNAL_API_KEY=kaabil-uat-internal-key-2026"

# Option 4: .env file (if using dotenv)
INTERNAL_API_KEY=kaabil-uat-internal-key-2026
```

### PROD Server

Same as above, but with production key:

```bash
export INTERNAL_API_KEY="kaabil-prod-internal-key-2026"
```

### Important
- Both org-recruiter AND new recruiter on the same server must use the SAME key
- The code reads from `getenv('INTERNAL_API_KEY')`
- If not set, falls back to `kaabil-internal-api-2026` (local testing only)

---

## Part 3: Deploy new recruiter Changes

### Files Changed

| File | Change |
|------|--------|
| `app/Services/OrgRecruiterClient.php` | New file - API client |
| `app/Services/ProductionDBAdapter.php` | Added org-recruiter API integration |
| `app/helpers/FeatureFlags.php` | New file - Feature flag helper |
| `app/config/config.php` | Added USE_ORG_RECRUITER_API flag |
| `app/views/job_detail.php` | Fixed interview scheduling status |
| `app/Services/NotificationService.php` | WhatsApp disable for testing (REVERT before deploy) |

### Before Deploying

1. **Re-enable WhatsApp** in NotificationService.php:
   ```php
   // REMOVE these lines:
   // error_log("WhatsApp notification DISABLED for testing...");
   // return null;
   ```

2. **Set feature flag** in config.php:
   ```php
   define('USE_ORG_RECRUITER_API', true);
   ```

3. **Set ORG_RECRUITER_URL** for production:
   ```php
   define('ORG_RECRUITER_URL', 'https://kaabilprogram.org');
   ```

### Deployment Method

Since new recruiter is pulled from AWS (not git):
- Copy modified files to the deployment source (S3 bucket or CodePipeline source)
- Trigger deployment pipeline
- OR manually copy to EC2 server

---

## Part 4: Verification Steps

### After UAT Deployment

1. Set `INTERNAL_API_KEY` on UAT server (both apps)
2. Test shortlist flow:
   - Shortlist a candidate
   - Verify WhatsApp received
   - Verify in-app notification received
   - Check logs for "OrgRecruiter API: Status change notification sent successfully"

3. Test interview scheduling:
   - Schedule interview for a candidate
   - Verify notification received
   - Status should be "Scheduled" not "Selected"

### After PROD Deployment

Same verification steps with production data.

---

## Rollback Plan

If issues occur after deployment:

### Quick Rollback (Feature Flag)

In new recruiter `app/config/config.php`:
```php
define('USE_ORG_RECRUITER_API', false);
```

This will revert to using local NotificationService for all notifications.

### Full Rollback (org-recruiter)

Revert the MY_Controller.php change:
```bash
git revert <commit-hash>
git push origin uat  # or production
```

---

## Architecture Summary

```
┌─────────────────────────────────────────────────────────────────┐
│                      NEW RECRUITER                               │
│                                                                  │
│  ┌──────────────┐    ┌───────────────────┐    ┌──────────────┐ │
│  │ job_detail   │───►│ ProductionDB      │───►│ OrgRecruiter │ │
│  │ .php         │    │ Adapter           │    │ Client       │ │
│  └──────────────┘    └───────────────────┘    └──────┬───────┘ │
│                                                       │         │
└───────────────────────────────────────────────────────┼─────────┘
                                                        │
                    HTTP POST with X-Internal-Api-Key   │
                    + token header                      │
                                                        ▼
┌─────────────────────────────────────────────────────────────────┐
│                      ORG-RECRUITER                               │
│                                                                  │
│  ┌──────────────┐    ┌───────────────────┐    ┌──────────────┐ │
│  │ MY_Controller│───►│ V3/Company        │───►│ CompanyModel │ │
│  │ (validates   │    │ Controller        │    │ (sends       │ │
│  │  API key)    │    │                   │    │  WhatsApp,   │ │
│  └──────────────┘    └───────────────────┘    │  Firebase,   │ │
│                                                │  in-app)     │ │
│                                                └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```

---

## Contacts

- **GitLab Repo:** https://gitlab.innowrap.co.in/innowrap/mission-even
- **Branches:** `uat`, `production`
- **Documentation:** `recruiter/docs/org-recruiter-api-integration.md`
