# PHP to Node.js API Mapping

This document shows the mapping between the original PHP Android APIs and the new Node.js implementations.

## API Endpoint Mapping

| PHP Tag | PHP Endpoint | Node.js Endpoint | Description |
|---------|--------------|------------------|-------------|
| `login` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/login` | User authentication |
| `register` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/register` | User registration |
| `updateSqId` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/updateSqId` | Update sequence ID |
| `getSqId` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getSqId` | Get sequence ID |
| `getSyncId` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getSyncId` | Get sync ID |
| `treedetails` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/treedetails` | Get master data |
| `refreshsocietyroad` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/refreshsocietyroad` | Refresh society/road data |
| `storetreedetails` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/storetreedetails` | Store tree survey data |
| `updatetreedetails` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/updatetreedetails` | Update tree survey data |
| `getTreeDetailsByUserID` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getTreeDetailsByUserID` | Get user's tree data |
| `getTreeDetailsForQC` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getTreeDetailsForQC` | Get QC pending data |
| `qc` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/qc` | Update QC status |
| `submit_location` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/submit_location` | Store user location |
| `savetrackpath` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/savetrackpath` | Store tracking path |
| `getNearbyTrees` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getNearbyTrees` | Get nearby trees |
| `getExcelReport` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getExcelReport` | Generate Excel report |
| `getQcReport` | `POST /index.php` | `POST /api/mobile/project/:treelogy_id/getQcReport` | Get QC statistics |

## Key Differences

### 1. URL Structure
- **PHP**: Single endpoint `/index.php` with `tag` parameter
- **Node.js**: RESTful endpoints with project-specific routing

### 2. Database Connection
- **PHP**: Direct PostgreSQL connection per request
- **Node.js**: Project-specific database connections using `createProjectDatabase()`

### 3. Authentication
- **PHP**: Basic session-based authentication
- **Node.js**: JWT token-based authentication with bcrypt password hashing

### 4. Error Handling
- **PHP**: Basic error responses
- **Node.js**: Structured error responses with proper HTTP status codes

### 5. Response Format
- **PHP**: Mixed response formats
- **Node.js**: Consistent JSON responses with success/error indicators

## Database Schema Mapping

| PHP Table | Node.js Usage | Description |
|-----------|---------------|-------------|
| `treeusers` | `treeusers` | User accounts and authentication |
| `treedetailsentered` | `treedetailsentered` | Tree survey data |
| `treelist` | `treelist` | Master tree species data |
| `condition` | `condition` | Tree condition master data |
| `ownership` | `ownership` | Ownership type master data |
| `remarks` | `remarks` | Tree remarks master data |
| `spacial_comment` | `spacial_comment` | Spatial comment master data |
| `bird_nest` | `bird_nest` | Bird nest master data |
| `treebal_imbalance` | `treebal_imbalance` | Tree balance master data |

## Function Mapping

| PHP Function | Node.js Function | Description |
|--------------|------------------|-------------|
| `getUserByEmailAndPassword()` | `login()` | User authentication |
| `storeUser()` | `register()` | User registration |
| `updateSqId()` | `updateSqId()` | Update sequence ID |
| `getSqId()` | `getSqId()` | Get sequence ID |
| `getSyncId()` | `getSyncId()` | Get sync ID |
| `getTreeList()` | `getTreeDetails()` | Get master data |
| `storeTreeDetails()` | `storeTreeDetails()` | Store tree survey |
| `updateTreeDetails()` | `updateTreeDetails()` | Update tree survey |
| `getTreeDetailsByUserID()` | `getTreeDetailsByUserID()` | Get user's trees |
| `getTreeDetailsForQC()` | `getTreeDetailsForQC()` | Get QC data |
| `qc()` | `qc()` | Update QC status |
| `savelogindetails()` | `submitLocation()` | Store location |
| `savetrackpathuser()` | `saveTrackPath()` | Store tracking |
| `getNearbyTrees()` | `getNearbyTrees()` | Get nearby trees |

## Security Improvements

1. **Password Hashing**: Using bcrypt instead of plain text
2. **JWT Tokens**: Secure token-based authentication
3. **Input Validation**: Proper request validation
4. **SQL Injection Prevention**: Using parameterized queries
5. **CORS Support**: Cross-origin request handling
6. **Rate Limiting**: API rate limiting (to be implemented)

## Performance Improvements

1. **Connection Pooling**: Database connection pooling
2. **Async/Await**: Modern async programming
3. **Error Handling**: Comprehensive error handling
4. **Response Caching**: Caching for master data (to be implemented)
5. **Database Indexing**: Optimized database queries

## Migration Notes

1. **Backward Compatibility**: All original PHP API responses are maintained
2. **Database Schema**: Uses existing project database schema
3. **Authentication**: Enhanced security while maintaining compatibility
4. **Error Codes**: Maintains original success/error code structure
5. **Data Format**: Preserves original data structure and field names

## Testing

The Node.js APIs can be tested using the same Android applications that use the PHP APIs, as the response formats are maintained for backward compatibility.

## Deployment

1. Install Node.js dependencies: `npm install`
2. Configure environment variables
3. Start the server: `npm start`
4. APIs will be available at: `http://172.17.46.11:63318/api/mobile/project/{treelogy_id}/`

## Future Enhancements

1. **Image Upload**: Implement image upload endpoints
2. **Real-time Sync**: WebSocket support for real-time updates
3. **Offline Support**: Offline data synchronization
4. **Analytics**: Usage analytics and reporting
5. **Monitoring**: API monitoring and logging
