# Mission Deletion Flow - Complete Analysis from Logs ## 🔍 Analysis of Your Deletion Flow Based on your logs, here's what's happening: --- ## ✅ What's Working 1. **Mission is fetched correctly** ✅ ``` SELECT "public"."Mission" WHERE "id" = '805c1d8c-1bd4-41e7-9cf1-d22631dae260' ``` 2. **Attachments are fetched** ✅ ``` SELECT "public"."Attachment" WHERE "missionId" = '805c1d8c-1bd4-41e7-9cf1-d22631dae260' ``` 3. **N8N deletion workflow is called** ✅ ``` Starting N8N deletion workflow Triggering n8n mission deletion workflow ``` 4. **N8N responds successfully** ✅ ``` Deletion webhook response { status: 200 } Parsed deletion workflow result { success: true, hasError: false } ``` 5. **Mission is deleted from database** ✅ ``` DELETE FROM "public"."Mission" WHERE "id" = '805c1d8c-1bd4-41e7-9cf1-d22631dae260' Mission deleted successfully from database ``` --- ## ❌ Critical Problems ### Problem 1: N8N_API_KEY Not Set ``` N8N_API_KEY is not set in environment variables API key present { present: false } ``` **Impact**: N8N workflow runs but may not have proper authentication. ### Problem 2: All Integration IDs Are NULL/Empty **What N8N Receives**: ```json { "missionId": "805c1d8c-1bd4-41e7-9cf1-d22631dae260", "name": "libra", "repoName": "", // ❌ EMPTY "leantimeProjectId": 0, // ❌ ZERO "documentationCollectionId": "", // ❌ EMPTY "rocketchatChannelId": "", // ❌ EMPTY "giteaRepositoryUrl": null, // ❌ NULL "outlineCollectionId": null, // ❌ NULL "rocketChatChannelId": null, // ❌ NULL "penpotProjectId": null // ❌ NULL } ``` **Root Cause**: The mission was created **without integration IDs being saved** because: 1. Mission was created in database ✅ 2. N8N workflow was triggered ✅ 3. N8N created integrations ✅ 4. N8N tried to call `/api/missions/mission-created` ❌ 5. **Endpoint returned 500 error** (N8N_API_KEY not configured) ❌ 6. **IDs were never saved to database** ❌ ### Problem 3: N8N Cannot Delete Integrations Because N8N receives empty IDs: - ❌ Cannot delete Gitea repository (no `repoName`) - ❌ Cannot close Leantime project (no `leantimeProjectId`) - ❌ Cannot delete Outline collection (no `documentationCollectionId`) - ❌ Cannot close RocketChat channel (no `rocketchatChannelId`) **Result**: External integrations remain orphaned even though mission is deleted. --- ## 🔄 Complete Flow Breakdown ### Step 1: Frontend Calls DELETE ``` DELETE /api/missions/805c1d8c-1bd4-41e7-9cf1-d22631dae260 ``` ### Step 2: Backend Fetches Mission ```sql SELECT "Mission" WHERE "id" = '805c1d8c-1bd4-41e7-9cf1-d22631dae260' ``` **Result**: Mission found, but all integration IDs are `null`: - `leantimeProjectId`: null - `outlineCollectionId`: null - `rocketChatChannelId`: null - `giteaRepositoryUrl`: null ### Step 3: Backend Prepares Deletion Data ```javascript { repoName: "", // Extracted from null giteaRepositoryUrl leantimeProjectId: 0, // null || 0 = 0 documentationCollectionId: "", // null || '' = '' rocketchatChannelId: "" // null || '' = '' } ``` ### Step 4: N8N Workflow Called ``` POST https://brain.slm-lab.net/webhook-test/mission-delete ``` **Headers**: - `x-api-key`: (empty - N8N_API_KEY not set) **Body**: (with empty IDs as shown above) ### Step 5: N8N Workflow Executes - ✅ Receives request - ❌ Cannot delete integrations (no IDs) - ✅ Returns success (but didn't actually delete anything) ### Step 6: Mission Deleted from Database ```sql DELETE FROM "Mission" WHERE "id" = '805c1d8c-1bd4-41e7-9cf1-d22631dae260' ``` **CASCADE deletes**: - ✅ MissionUsers - ✅ Attachments **But external integrations remain**: - ❌ Gitea repository still exists - ❌ Leantime project still exists - ❌ Outline collection still exists - ❌ RocketChat channel still exists --- ## 🎯 Root Cause Summary ### Why IDs Are NULL 1. **Mission Creation**: - Mission created in database ✅ - N8N workflow triggered ✅ - N8N created integrations ✅ 2. **N8N Callback Fails**: - N8N tries to call `/api/missions/mission-created` - Endpoint checks for `N8N_API_KEY` in environment - `N8N_API_KEY` is not set ❌ - Endpoint returns 500: "Server configuration error" ❌ - **IDs are never saved** ❌ 3. **Mission Deletion**: - Mission has no integration IDs (all null) - N8N receives empty IDs - Cannot delete integrations - **Integrations remain orphaned** ❌ --- ## ✅ Solutions ### Solution 1: Fix N8N_API_KEY (IMMEDIATE) **Add to environment variables**: ```env N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4 ``` **Then**: 1. Restart your application 2. Create a new mission 3. Verify IDs are saved to database 4. Delete mission - should work correctly ### Solution 2: Fix Existing Missions (MIGRATION) For missions that already exist without IDs: **Option A: Manual Update** ```sql UPDATE "Mission" SET "giteaRepositoryUrl" = 'https://gite.slm-lab.net/alma/repo-name', "leantimeProjectId" = '123', "outlineCollectionId" = 'collection-id', "rocketChatChannelId" = 'channel-id' WHERE "id" = 'mission-id'; ``` **Option B: Query External Services** - Query Gitea API to find repositories - Query Leantime API to find projects - Query Outline API to find collections - Query RocketChat API to find channels - Update database with found IDs **Option C: Re-create Missions** - Delete missions without IDs - Re-create them (with N8N_API_KEY fixed, IDs will be saved) ### Solution 3: Make N8N Callback More Resilient **Modify `/api/missions/mission-created` endpoint** to handle missing API key more gracefully: ```typescript // Instead of returning 500, log warning and continue if (!expectedApiKey) { logger.warn('N8N_API_KEY not configured, but continuing anyway', { missionId: body.missionId }); // Continue without API key validation (less secure but works) // OR require API key but provide better error message } ``` **Not recommended** for production (security risk), but could work for development. --- ## 📊 Current State vs Desired State ### Current State (Your Logs) ``` Mission in DB: - leantimeProjectId: null - outlineCollectionId: null - rocketChatChannelId: null - giteaRepositoryUrl: null N8N Receives: - repoName: "" - leantimeProjectId: 0 - documentationCollectionId: "" - rocketchatChannelId: "" Result: - Mission deleted ✅ - Integrations NOT deleted ❌ ``` ### Desired State ``` Mission in DB: - leantimeProjectId: "123" - outlineCollectionId: "collection-456" - rocketChatChannelId: "channel-789" - giteaRepositoryUrl: "https://gite.slm-lab.net/alma/repo-name" N8N Receives: - repoName: "repo-name" - leantimeProjectId: 123 - documentationCollectionId: "collection-456" - rocketchatChannelId: "channel-789" Result: - Mission deleted ✅ - Integrations deleted ✅ ``` --- ## 🔧 Immediate Actions Required 1. **✅ Add N8N_API_KEY to environment** ```env N8N_API_KEY=LwgeE1ntADD20OuWC88S3pR0EaO7FtO4 ``` 2. **✅ Restart application** 3. **✅ Test mission creation** - Create a new mission - Check database - IDs should be saved - Delete mission - should work correctly 4. **⚠️ Fix existing missions** - Update existing missions with their integration IDs - Or delete and re-create them --- ## 📝 Summary **The deletion flow is working correctly**, but: 1. **N8N_API_KEY is missing** → Endpoint returns 500 error 2. **IDs are never saved** → Mission has null integration IDs 3. **N8N receives empty IDs** → Cannot delete integrations 4. **Integrations remain orphaned** → External resources not cleaned up **Fix**: Add `N8N_API_KEY` to environment variables, then new missions will work correctly. Existing missions need manual update or re-creation. --- **Document Created**: $(date) **Status**: Deletion flow works, but integration cleanup fails due to missing IDs