danylokhodus commited on
Commit
247fc9b
·
1 Parent(s): 96c7faf

fix(agent-canvas): change initial canvas ownership to system agent account to support unclaimed state

Browse files
FlowAPI/Controllers/AgentCanvasController.cs CHANGED
@@ -82,11 +82,35 @@ namespace FlowAPI.Controllers
82
 
83
  try
84
  {
85
- // Ensure user exists in database to prevent Foreign Key constraints failure
86
- var user = await _userRepo.GetByIdAsync(CurrentUserId);
87
- if (user == null)
 
88
  {
89
- user = new User
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  {
91
  Id = CurrentUserId,
92
  Email = User.FindFirst(ClaimTypes.Email)?.Value ?? $"{CurrentUserId}@questboard.ai",
@@ -94,23 +118,23 @@ namespace FlowAPI.Controllers
94
  PasswordHash = "",
95
  CreatedAt = DateTime.UtcNow
96
  };
97
- await _userRepo.CreateAsync(user);
98
  }
99
 
100
- // 1. Create the Graph record in the database
101
  var createDto = new CreateGraphDto
102
  {
103
  Title = string.IsNullOrWhiteSpace(schema.Title) ? "QuestboardAI Plan" : schema.Title.Trim(),
104
  Description = schema.Description
105
  };
106
- var graphResponse = await _graphService.CreateAsync(createDto, CurrentUserId);
107
 
108
  // 2. Perform Auto-Layout to assign visual coordinates PosX and PosY
109
- var fullGraph = AutoLayoutEngine.LayoutGraph(schema, CurrentUserId);
110
  fullGraph.Id = graphResponse.Id; // Sync graph ID
111
 
112
  // 3. Save all nodes and dependency edges in the database
113
- var saveResult = await _graphService.SaveFullGraphAsync(graphResponse.Id, fullGraph, CurrentUserId);
114
  if (!saveResult)
115
  {
116
  return BadRequest(new { message = "Failed to store generated task nodes in database." });
 
82
 
83
  try
84
  {
85
+ var agentId = Guid.Parse("33d1ae31-bf51-4b12-89d0-8e574e7916ab");
86
+ // Ensure default system agent user exists in database to prevent Foreign Key constraints failure
87
+ var agentUser = await _userRepo.GetByIdAsync(agentId);
88
+ if (agentUser == null)
89
  {
90
+ var existingByEmail = await _userRepo.GetByEmailAsync("dan9228335@gmail.com");
91
+ if (existingByEmail != null)
92
+ {
93
+ agentUser = existingByEmail;
94
+ }
95
+ else
96
+ {
97
+ agentUser = new User
98
+ {
99
+ Id = agentId,
100
+ Email = "dan9228335@gmail.com",
101
+ DisplayName = "Dodikuser",
102
+ PasswordHash = "",
103
+ CreatedAt = DateTime.UtcNow
104
+ };
105
+ await _userRepo.CreateAsync(agentUser);
106
+ }
107
+ }
108
+
109
+ // Also ensure the current authenticated user requesting this has a record in the database
110
+ var requestUser = await _userRepo.GetByIdAsync(CurrentUserId);
111
+ if (requestUser == null)
112
+ {
113
+ requestUser = new User
114
  {
115
  Id = CurrentUserId,
116
  Email = User.FindFirst(ClaimTypes.Email)?.Value ?? $"{CurrentUserId}@questboard.ai",
 
118
  PasswordHash = "",
119
  CreatedAt = DateTime.UtcNow
120
  };
121
+ await _userRepo.CreateAsync(requestUser);
122
  }
123
 
124
+ // 1. Create the Graph record in the database, owned by the agent system user (unclaimed)
125
  var createDto = new CreateGraphDto
126
  {
127
  Title = string.IsNullOrWhiteSpace(schema.Title) ? "QuestboardAI Plan" : schema.Title.Trim(),
128
  Description = schema.Description
129
  };
130
+ var graphResponse = await _graphService.CreateAsync(createDto, agentId);
131
 
132
  // 2. Perform Auto-Layout to assign visual coordinates PosX and PosY
133
+ var fullGraph = AutoLayoutEngine.LayoutGraph(schema, agentId);
134
  fullGraph.Id = graphResponse.Id; // Sync graph ID
135
 
136
  // 3. Save all nodes and dependency edges in the database
137
+ var saveResult = await _graphService.SaveFullGraphAsync(graphResponse.Id, fullGraph, agentId);
138
  if (!saveResult)
139
  {
140
  return BadRequest(new { message = "Failed to store generated task nodes in database." });