danylokhodus commited on
Commit
83f2320
·
1 Parent(s): b01b49e

feat: Add dynamic UpdateFromAgent endpoint to allow programmatical roadmap editing

Browse files
FlowAPI/Controllers/AgentCanvasController.cs CHANGED
@@ -155,6 +155,72 @@ namespace FlowAPI.Controllers
155
  }
156
  }
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  [AllowAnonymous]
159
  [HttpPost("claim/check")]
160
  public async Task<IActionResult> CheckClaimToken([FromBody] ClaimRequestDto dto)
 
155
  }
156
  }
157
 
158
+ [HttpPost("{id}/update")]
159
+ public async Task<IActionResult> UpdateFromAgent(Guid id, [FromBody] QuestSchemaDto schema)
160
+ {
161
+ if (CurrentUserId == Guid.Empty)
162
+ return Unauthorized(new { message = "User authentication required." });
163
+
164
+ if (schema == null || schema.Nodes == null || schema.Nodes.Count == 0)
165
+ {
166
+ return BadRequest(new { message = "QuestboardAI plan must contain at least one task node." });
167
+ }
168
+
169
+ try
170
+ {
171
+ var dbGraph = await _graphRepo.GetByIdAsync(id);
172
+ if (dbGraph == null)
173
+ return NotFound(new { message = "Board not found." });
174
+
175
+ // Secure check: verify that the user has edit permission to this graph (owner or shared editor)
176
+ var agentUser = await _userRepo.GetByEmailAsync("dan9228335@gmail.com");
177
+ var agentId = agentUser?.Id ?? Guid.Parse("33d1ae31-bf51-4b12-89d0-8e574e7916ab");
178
+
179
+ // Allow write if the current user owns it or if it is still owned by the agent system user (unclaimed)
180
+ if (dbGraph.UserId != CurrentUserId && dbGraph.UserId != agentId)
181
+ {
182
+ var sharedAccess = await _sharedGraphRepo.GetShareAsync(id, CurrentUserId);
183
+ if (sharedAccess == null || sharedAccess.Permission != "Edit")
184
+ {
185
+ return Unauthorized(new { message = "You do not have write access to edit this board." });
186
+ }
187
+ }
188
+
189
+ // If the title or description is provided, update the Graph metadata
190
+ if (!string.IsNullOrWhiteSpace(schema.Title))
191
+ {
192
+ dbGraph.Title = schema.Title.Trim();
193
+ }
194
+ if (schema.Description != null)
195
+ {
196
+ dbGraph.Description = schema.Description;
197
+ }
198
+ await _graphRepo.UpdateAsync(dbGraph);
199
+
200
+ // Perform Auto-Layout on the new nodes
201
+ var fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId);
202
+ fullGraph.Id = id; // Sync graph ID
203
+
204
+ // Save all new nodes and edges, completely replacing the old graph structure
205
+ var saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, dbGraph.UserId);
206
+ if (!saveResult)
207
+ {
208
+ return BadRequest(new { message = "Failed to update generated task nodes in database." });
209
+ }
210
+
211
+ return Ok(new
212
+ {
213
+ success = true,
214
+ graphId = id,
215
+ canvasUrl = $"/canvas/{id}"
216
+ });
217
+ }
218
+ catch (Exception ex)
219
+ {
220
+ return BadRequest(new { message = $"Internal error during plan update: {ex}" });
221
+ }
222
+ }
223
+
224
  [AllowAnonymous]
225
  [HttpPost("claim/check")]
226
  public async Task<IActionResult> CheckClaimToken([FromBody] ClaimRequestDto dto)