# MongoDB Write Conflict and Response Format Fixes

## Issues Fixed

### 1. **MongoDB Write Conflict Error**
**Problem**: The CreateProfile API was failing with MongoDB write conflict error (code: 112):
```
MongoServerError: Caused by :: Write conflict during plan execution and yielding is disabled. :: Please retry your operation or multi-document transaction.
```

**Solution**: 
- Created `src/utils/mongoRetry.js` with retry logic for MongoDB write conflicts
- Added exponential backoff with jitter to avoid thundering herd problems
- Wrapped the CreateProfile transaction with `retryTransaction()` function
- Added specific error handling for write conflicts in ResHandler

### 2. **Inconsistent Response Format**
**Problem**: Response had inconsistent field naming:
- Some responses used `StatusCode` (uppercase) instead of `statusCode` (lowercase)
- Missing `success` boolean field in responses

**Solution**:
- Updated `src/utils/success.ts` to include `success: true` field
- Updated `src/utils/errors.ts` to include `success: false` field
- Fixed `src/utils/responses/resHandler.js` to use consistent `statusCode` naming
- Updated all tests to expect the new response format

### 3. **Enhanced Error Handling**
**Added specific handling for**:
- MongoDB write conflicts (409 status with retry suggestion)
- Joi validation errors (400 status)
- Custom error objects with proper field mapping
- Standard JavaScript Error objects

## Response Format

### Success Response
```json
{
  "success": true,
  "statusCode": 200,
  "message": "Operation successful",
  "data": { /* optional data */ }
}
```

### Error Response
```json
{
  "success": false,
  "statusCode": 400,
  "message": "Error description",
  "error": "ErrorType" /* optional */
}
```

## Files Modified

1. **`src/utils/success.ts`** - Added `success: true` field
2. **`src/utils/errors.ts`** - Added `success: false` field  
3. **`src/utils/responses/resHandler.js`** - Fixed response format and added write conflict handling
4. **`src/utils/mongoRetry.js`** - New utility for MongoDB retry logic
5. **`src/controllers/profile/index.js`** - Wrapped CreateProfile with retry logic
6. **`src/utils/__tests__/response.test.ts`** - Updated tests for new response format
7. **`jest.config.js`** - Fixed Jest configuration for ES modules
8. **`tsconfig.json`** - Updated for ES module support
9. **`README.md`** - Updated response format documentation

## Retry Logic Details

- **Maximum Retries**: 3 attempts
- **Base Delay**: 50ms for transactions, 100ms for general operations  
- **Exponential Backoff**: delay = baseDelay * 2^attempt + random(0-100ms)
- **Only Retries**: MongoDB write conflicts (code: 112 or codeName: 'WriteConflict')
- **No Retry**: Validation errors, authentication errors, business logic errors

## Testing

All response utility tests pass (10/10):
- Success response structure validation
- Error response structure validation  
- CRUD operation responses
- Common error responses (auth, not found)

## Benefits

1. **Improved Reliability**: Write conflicts are automatically retried
2. **Consistent API**: All responses have uniform structure with `success` field
3. **Better Client Experience**: Clients can easily determine success/failure
4. **Proper Error Handling**: Different error types handled appropriately
5. **Maintainability**: Centralized error handling and response formatting
