# SAP Integration Testing Guide

## Postman Testing

### 1. Testing IO Validation API

**Endpoint:** `GET /api/v1/dealer-claims/:requestId/io`

**Method:** GET

**Headers:**
```
Authorization: Bearer <your_jwt_token>
Content-Type: application/json
```

**Note:** The CSRF error in Postman is likely coming from SAP, not our backend. Our backend doesn't have CSRF protection enabled.

### 2. Testing Budget Blocking API

**Endpoint:** `PUT /api/v1/dealer-claims/:requestId/io`

**Method:** PUT

**Headers:**
```
Authorization: Bearer <your_jwt_token>
Content-Type: application/json
```

**Body:**
```json
{
  "ioNumber": "600060",
  "ioRemark": "Test remark",
  "availableBalance": 1000000,
  "blockedAmount": 500,
  "remainingBalance": 999500
}
```

### 3. Direct SAP API Testing in Postman

If you want to test SAP API directly (bypassing our backend):

#### IO Validation
- **URL:** `https://RENOIHND01.Eichergroup.com:1443/sap/opu/odata/sap/ZFI_BUDGET_CHECK_API_SRV/GetSenderDataSet?$filter=IONumber eq '600060'&$select=Sender,ResponseDate,GetIODetailsSet01&$expand=GetIODetailsSet01&$format=json`
- **Method:** GET
- **Authentication:** Basic Auth
  - Username: Your SAP username
  - Password: Your SAP password
- **Headers:**
  - `Accept: application/json`
  - `Content-Type: application/json`

#### Budget Blocking
- **URL:** `https://RENOIHND01.Eichergroup.com:1443/sap/opu/odata/sap/ZFI_BUDGET_BLOCK_API_SRV/RequesterInputSet`
- **Method:** POST
- **Authentication:** Basic Auth
  - Username: Your SAP username
  - Password: Your SAP password
- **Headers:**
  - `Accept: application/json`
  - `Content-Type: application/json`
- **Body:**
```json
{
  "Request_Date_Time": "2025-08-29T10:51:00",
  "Requester": "REFMS",
  "lt_io_input": [
    {
      "IONumber": "600060",
      "Amount": "500"
    }
  ],
  "lt_io_output": [],
  "ls_response": []
}
```

## Common Errors and Solutions

### 1. CSRF Token Validation Error

**Error:** "CSRF token validation error"

**Possible Causes:**
- SAP API requires CSRF tokens for POST/PUT requests
- SAP might be checking for specific headers

**Solutions:**
1. **Get CSRF Token First:**
   - Make a GET request to the SAP service root to get CSRF token
   - Example: `GET https://RENOIHND01.Eichergroup.com:1443/sap/opu/odata/sap/ZFI_BUDGET_BLOCK_API_SRV/`
   - Look for `x-csrf-token` header in response
   - Add this token to subsequent POST/PUT requests as header: `X-CSRF-Token: <token>`

2. **Add Required Headers:**
   ```
   X-CSRF-Token: Fetch
   X-Requested-With: XMLHttpRequest
   ```

### 2. Authentication Failed

**Error:** "Authentication failed" or "401 Unauthorized"

**Possible Causes:**
1. Wrong username/password
2. Basic auth not being sent correctly
3. SSL certificate issues
4. SAP account locked or expired

**Solutions:**

1. **Verify Credentials:**
   - Double-check `SAP_USERNAME` and `SAP_PASSWORD` in `.env`
   - Ensure no extra spaces or special characters
   - Test credentials in browser first

2. **Check SSL Certificate:**
   - If using self-signed certificate, set `SAP_DISABLE_SSL_VERIFY=true` in `.env` (testing only!)
   - For production, ensure proper SSL certificates are configured

3. **Test Basic Auth Manually:**
   - Use Postman with Basic Auth enabled
   - Verify the Authorization header format: `Basic <base64(username:password)>`

4. **Check SAP Account Status:**
   - Verify account is active and not locked
   - Check if password has expired
   - Contact SAP administrator if needed

### 3. Connection Errors

**Error:** "ECONNREFUSED" or "ENOTFOUND"

**Solutions:**
1. Verify `SAP_BASE_URL` is correct
2. Check network connectivity to SAP server
3. Ensure firewall allows connections to port 1443
4. Verify Zscaler is configured correctly

### 4. Timeout Errors

**Error:** "Request timeout"

**Solutions:**
1. Increase `SAP_TIMEOUT_MS` in `.env` (default: 30000ms = 30 seconds)
2. Check SAP server response time
3. Verify network latency

## Debugging

### Enable Debug Logging

Set log level to debug in your `.env`:
```
LOG_LEVEL=debug
```

This will log:
- Request URLs
- Request payloads
- Response status codes
- Response data
- Error details

### Check Backend Logs

Look for `[SAP]` prefixed log messages:
```bash
# In development
npm run dev

# Check logs for SAP-related messages
```

### Test SAP Connection

You can test if SAP is reachable:
```bash
curl -u "username:password" \
  "https://RENOIHND01.Eichergroup.com:1443/sap/opu/odata/sap/ZFI_BUDGET_CHECK_API_SRV/"
```

## Environment Variables Checklist

Ensure these are set in your `.env`:

```bash
# Required
SAP_BASE_URL=https://RENOIHND01.Eichergroup.com:1443
SAP_USERNAME=your_username
SAP_PASSWORD=your_password

# Optional (with defaults)
SAP_TIMEOUT_MS=30000
SAP_SERVICE_NAME=ZFI_BUDGET_CHECK_API_SRV
SAP_BLOCK_SERVICE_NAME=ZFI_BUDGET_BLOCK_API_SRV
SAP_REQUESTER=REFMS
SAP_DISABLE_SSL_VERIFY=false  # Only for testing
```

## Next Steps

If you're still getting errors:

1. **Check Backend Logs:** Look for detailed error messages
2. **Test Directly in Postman:** Bypass backend and test SAP API directly
3. **Verify SAP Credentials:** Test with SAP administrator
4. **Check Network:** Ensure server can reach SAP URL
5. **Review SAP Documentation:** Check if there are additional requirements

