NeahNew/N8N_SAVE_MISSION_API_FIX.md
2026-01-09 11:19:32 +01:00

268 lines
8.7 KiB
Markdown

# N8N Save Mission To API Node - Fix Required
## 🔍 Problem Analysis
Based on the N8N workflow configuration you provided, I've identified **TWO CRITICAL ISSUES**:
---
## ❌ Issue 1: Incorrect URL
### Current Configuration
```
URL: {{ $node['Process Mission Data'].json.config.MISSION_API_URL + '/mission-created' }}
```
### What This Produces
- `MISSION_API_URL` = `https://hub.slm-lab.net` (from your config)
- Result: `https://hub.slm-lab.net/mission-created`
### Actual Endpoint
- Should be: `https://hub.slm-lab.net/api/missions/mission-created`
### Fix Required
```
URL: {{ $node['Process Mission Data'].json.config.MISSION_API_URL }}/api/missions/mission-created
```
**Note**: Remove the `+` operator and add `/api/missions` before `/mission-created`
---
## ❌ Issue 2: Missing `missionId` in Body
### Current Configuration
Looking at your `base.json`, I can see the body parameters, but **`missionId` is MISSING**!
### What the Endpoint Expects
From `app/api/missions/mission-created/route.ts`:
- `missionId` ⚠️ **REQUIRED** - Used to find the mission (preferred over name + creatorId)
- `gitRepoUrl` → maps to `giteaRepositoryUrl` in database
- `leantimeProjectId` → maps to `leantimeProjectId` in database
- `documentationCollectionId` → maps to `outlineCollectionId` in database
- `rocketchatChannelId` → maps to `rocketChatChannelId` in database
- `creatorId` ✅ (you have this)
- `name` ✅ (you have this)
### What the Endpoint Expects
From `app/api/missions/mission-created/route.ts`:
- `gitRepoUrl` → maps to `giteaRepositoryUrl` in database
- `leantimeProjectId` → maps to `leantimeProjectId` in database
- `documentationCollectionId` → maps to `outlineCollectionId` in database
- `rocketchatChannelId` → maps to `rocketChatChannelId` in database
- `missionId` ✅ (you have this)
- `creatorId` ✅ (you have this)
- `name` ✅ (you have this)
### What N8N Should Send
**Body Parameters** (in N8N HTTP Request node):
| Field Name | Value Expression |
|------------|------------------|
| `name` | `{{ $node['Process Mission Data'].json.missionProcessed.name }}` |
| `niveau` | `{{ $node['Process Mission Data'].json.missionProcessed.niveau || 'default' }}` |
| `intention` | `{{ $node['Process Mission Data'].json.missionProcessed.intention }}` |
| `gitRepoUrl` | `{{ $node['Combine Results'].json.gitRepo?.html_url || '' }}` |
| `leantimeProjectId` | `{{ $node['Combine Results'].json.leantimeProject?.result?.[0] || '' }}` |
| `documentationCollectionId` | `{{ $node['Combine Results'].json.docCollection?.data?.id || '' }}` |
| `rocketchatChannelId` | `{{ $node['Combine Results'].json.rocketChatChannel?.channel?._id || '' }}` |
| `missionId` | `{{ $node['Process Mission Data'].json.missionId }}` |
| `creatorId` | `{{ $node['Process Mission Data'].json.creatorId }}` |
**⚠️ CRITICAL**: The field names must match exactly:
- `gitRepoUrl` (not `gitRepo` or `giteaRepositoryUrl`)
- `leantimeProjectId` (not `leantimeProject` or `leantimeId`)
- `documentationCollectionId` (not `docCollection` or `outlineCollectionId`)
- `rocketchatChannelId` (not `rocketChatChannel` or `rocketChatChannelId`)
---
## ✅ Complete Fix for N8N Node
### Step 1: Fix the URL
In the "Save Mission To API" HTTP Request node:
**Current (WRONG)**:
```
{{ $node['Process Mission Data'].json.config.MISSION_API_URL + '/mission-created' }}
```
**Fixed (CORRECT)**:
```
{{ $node['Process Mission Data'].json.config.MISSION_API_URL }}/api/missions/mission-created
```
### Step 2: Configure Body Parameters
In the "Save Mission To API" HTTP Request node, set **Body Parameters**:
**Method**: `POST`
**Send Body**: `Yes`
**Body Content Type**: `JSON` (or use Body Parameters)
**Body Parameters** (add each as a parameter):
1. **Parameter Name**: `name`
**Value**: `{{ $node['Process Mission Data'].json.missionProcessed.name }}`
2. **Parameter Name**: `niveau`
**Value**: `{{ $node['Process Mission Data'].json.missionProcessed.niveau || 'default' }}`
3. **Parameter Name**: `intention`
**Value**: `{{ $node['Process Mission Data'].json.missionProcessed.intention }}`
4. **Parameter Name**: `gitRepoUrl` ⚠️ (MUST be this exact name)
**Value**: `{{ $node['Combine Results'].json.gitRepo?.html_url || '' }}`
5. **Parameter Name**: `leantimeProjectId` ⚠️ (MUST be this exact name)
**Value**: `{{ $node['Combine Results'].json.leantimeProject?.result?.[0] || '' }}`
6. **Parameter Name**: `documentationCollectionId` ⚠️ (MUST be this exact name)
**Value**: `{{ $node['Combine Results'].json.docCollection?.data?.id || '' }}`
7. **Parameter Name**: `rocketchatChannelId` ⚠️ (MUST be this exact name)
**Value**: `{{ $node['Combine Results'].json.rocketChatChannel?.channel?._id || '' }}`
8. **Parameter Name**: `missionId` ⚠️ **MISSING - MUST ADD THIS**
**Value**: `{{ $node['Process Mission Data'].json.missionId }}`
9. **Parameter Name**: `creatorId`
**Value**: `{{ $node['Process Mission Data'].json.creatorId }}`
**⚠️ CRITICAL**: The `missionId` field is **MISSING** from your current configuration. The endpoint prefers `missionId` over `name + creatorId` for more reliable mission lookup.
### Step 3: Verify Headers
**Headers** should include:
- `Content-Type`: `application/json`
- `x-api-key`: `{{ $node['Process Mission Data'].json.config.N8N_API_KEY }}`
---
## 🧪 Testing the Fix
### Test 1: Check URL
After fixing, the URL should resolve to:
```
https://hub.slm-lab.net/api/missions/mission-created
```
### Test 2: Check Request Body
After fixing, the request body should look like:
```json
{
"name": "Mission Name",
"niveau": "default",
"intention": "Mission description",
"gitRepoUrl": "https://gite.slm-lab.net/alma/repo-name",
"leantimeProjectId": "123",
"documentationCollectionId": "collection-id",
"rocketchatChannelId": "channel-id",
"missionId": "mission-uuid",
"creatorId": "user-uuid"
}
```
### Test 3: Check Server Response
The endpoint should return:
```json
{
"success": true,
"message": "Mission updated successfully",
"mission": {
"id": "mission-uuid",
"name": "Mission Name",
"giteaRepositoryUrl": "https://gite.slm-lab.net/alma/repo-name",
"leantimeProjectId": "123",
"outlineCollectionId": "collection-id",
"rocketChatChannelId": "channel-id"
}
}
```
---
## 📋 Verification Checklist
After applying the fix:
- [ ] URL is correct: `{{ MISSION_API_URL }}/api/missions/mission-created`
- [ ] Body includes `gitRepoUrl` field (not `gitRepo` or `giteaRepositoryUrl`)
- [ ] Body includes `leantimeProjectId` field (not `leantimeProject` or `leantimeId`)
- [ ] Body includes `documentationCollectionId` field (not `docCollection` or `outlineCollectionId`)
- [ ] Body includes `rocketchatChannelId` field (not `rocketChatChannel`)
- [ ] Body includes `missionId` field
- [ ] Body includes `creatorId` field
- [ ] Headers include `x-api-key`
- [ ] Headers include `Content-Type: application/json`
- [ ] Test execution shows 200 OK response
- [ ] Database shows IDs saved after mission creation
---
## 🔍 Debugging
### If Still Not Working
1. **Check N8N Execution Logs**:
- Look at "Save Mission To API" node execution
- Check the actual URL being called
- Check the actual body being sent
- Check the response status code
2. **Check Server Logs**:
- Look for `/api/missions/mission-created` endpoint calls
- Check for 404 errors (wrong URL)
- Check for 400 errors (missing fields)
- Check for 401 errors (wrong API key)
3. **Test Manually**:
```bash
curl -X POST https://hub.slm-lab.net/api/missions/mission-created \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_N8N_API_KEY" \
-d '{
"missionId": "test-mission-id",
"name": "Test Mission",
"creatorId": "test-user-id",
"gitRepoUrl": "https://gite.slm-lab.net/alma/test",
"leantimeProjectId": "123",
"documentationCollectionId": "collection-456",
"rocketchatChannelId": "channel-789"
}'
```
---
## 📝 Summary
**Two critical fixes required**:
1. **URL Fix**: Change from:
```
{{ $node['Process Mission Data'].json.config.MISSION_API_URL + '/mission-created' }}
```
To:
```
{{ $node['Process Mission Data'].json.config.MISSION_API_URL }}/api/missions/mission-created
```
2. **Add Missing `missionId` Field**: Add this parameter to the body:
- **Name**: `missionId`
- **Value**: `{{ $node['Process Mission Data'].json.missionId }}`
**Note**: Your field names are already correct (`gitRepoUrl`, `leantimeProjectId`, etc.), but `missionId` is missing which is critical for reliable mission lookup.
After these fixes, the N8N workflow should successfully save integration IDs to the database, and mission deletion should work correctly.
---
**Document Created**: $(date)
**Priority**: CRITICAL - Blocks mission deletion functionality