Skip to content

Commit 75c71b2

Browse files
Copilotdwarwick
andcommitted
Addressing PR comments
Co-authored-by: dwarwick <15970276+dwarwick@users.noreply.github.com>
1 parent 1794fae commit 75c71b2

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

JwtIdentity.Tests/ServiceTests/SurveyServiceTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task ValidateSurveyForPublishing_ReturnsInvalid_WhenGroupIsEmpty()
8888
Published = false,
8989
Questions = new System.Collections.Generic.List<Question>
9090
{
91-
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 }
91+
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 0 }
9292
},
9393
QuestionGroups = new System.Collections.Generic.List<QuestionGroup>
9494
{
@@ -120,8 +120,8 @@ public async Task ValidateSurveyForPublishing_ReturnsInvalid_WhenGroupIsOrphaned
120120
Published = false,
121121
Questions = new System.Collections.Generic.List<Question>
122122
{
123-
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 },
124-
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 }
123+
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 0 },
124+
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 1 }
125125
},
126126
QuestionGroups = new System.Collections.Generic.List<QuestionGroup>
127127
{
@@ -153,12 +153,12 @@ public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedVia
153153
Published = false,
154154
Questions = new System.Collections.Generic.List<Question>
155155
{
156-
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 },
157-
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 }
156+
new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 0 },
157+
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 1 }
158158
},
159159
QuestionGroups = new System.Collections.Generic.List<QuestionGroup>
160160
{
161-
new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default", NextGroupId = 2 },
161+
new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default", NextGroupId = 1 },
162162
new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Connected Group" }
163163
}
164164
};
@@ -191,10 +191,10 @@ public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedVia
191191
Text = "Q1",
192192
QuestionNumber = 1,
193193
QuestionType = QuestionType.TrueFalse,
194-
GroupId = 1,
195-
BranchToGroupIdOnTrue = 2
194+
GroupId = 0,
195+
BranchToGroupIdOnTrue = 1
196196
},
197-
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 }
197+
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 1 }
198198
},
199199
QuestionGroups = new System.Collections.Generic.List<QuestionGroup>
200200
{
@@ -223,15 +223,15 @@ public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedVia
223223
Text = "Q1",
224224
QuestionNumber = 1,
225225
QuestionType = QuestionType.MultipleChoice,
226-
GroupId = 1
226+
GroupId = 0
227227
};
228228

229229
var choiceOption = new ChoiceOption
230230
{
231231
Id = 1,
232232
OptionText = "Option 1",
233233
MultipleChoiceQuestionId = 1,
234-
BranchToGroupId = 2
234+
BranchToGroupId = 1
235235
};
236236

237237
var survey = new Survey
@@ -243,7 +243,7 @@ public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedVia
243243
Questions = new System.Collections.Generic.List<Question>
244244
{
245245
mcQuestion,
246-
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 }
246+
new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 1 }
247247
},
248248
QuestionGroups = new System.Collections.Generic.List<QuestionGroup>
249249
{

JwtIdentity/Services/SurveyService.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public async Task GenerateDemoSurveyResponsesAsync(Survey survey, int numberOfUs
194194

195195
// Check for empty groups
196196
var emptyGroups = survey.QuestionGroups
197-
.Where(g => !survey.Questions.Any(q => q.GroupId == g.Id))
197+
.Where(g => !survey.Questions.Any(q => q.GroupId == g.GroupNumber))
198198
.ToList();
199199

200200
if (emptyGroups.Any())
@@ -206,62 +206,58 @@ public async Task GenerateDemoSurveyResponsesAsync(Survey survey, int numberOfUs
206206
}
207207

208208
// Find all reachable groups starting from group 0
209-
var reachableGroups = new HashSet<int>();
210-
var groupsToCheck = new Queue<int>();
209+
var reachableGroupNumbers = new HashSet<int>();
210+
var groupNumbersToCheck = new Queue<int>();
211211

212212
// Start with group 0 (default group) - it's always the entry point
213-
var defaultGroup = survey.QuestionGroups.FirstOrDefault(g => g.GroupNumber == 0);
214-
if (defaultGroup != null)
215-
{
216-
reachableGroups.Add(defaultGroup.Id);
217-
groupsToCheck.Enqueue(defaultGroup.Id);
218-
}
213+
reachableGroupNumbers.Add(0);
214+
groupNumbersToCheck.Enqueue(0);
219215

220216
// Traverse all possible paths
221-
while (groupsToCheck.Count > 0)
217+
while (groupNumbersToCheck.Count > 0)
222218
{
223-
var currentGroupId = groupsToCheck.Dequeue();
224-
var currentGroup = survey.QuestionGroups.FirstOrDefault(g => g.Id == currentGroupId);
219+
var currentGroupNumber = groupNumbersToCheck.Dequeue();
220+
var currentGroup = survey.QuestionGroups.FirstOrDefault(g => g.GroupNumber == currentGroupNumber);
225221

226222
if (currentGroup == null)
227223
continue;
228224

229-
// Check NextGroupId
230-
if (currentGroup.NextGroupId.HasValue && !reachableGroups.Contains(currentGroup.NextGroupId.Value))
225+
// Check NextGroupId (if it stores GroupNumber)
226+
if (currentGroup.NextGroupId.HasValue && !reachableGroupNumbers.Contains(currentGroup.NextGroupId.Value))
231227
{
232-
reachableGroups.Add(currentGroup.NextGroupId.Value);
233-
groupsToCheck.Enqueue(currentGroup.NextGroupId.Value);
228+
reachableGroupNumbers.Add(currentGroup.NextGroupId.Value);
229+
groupNumbersToCheck.Enqueue(currentGroup.NextGroupId.Value);
234230
}
235231

236232
// Check branching from questions in this group
237-
var questionsInGroup = survey.Questions.Where(q => q.GroupId == currentGroupId).ToList();
233+
var questionsInGroup = survey.Questions.Where(q => q.GroupId == currentGroupNumber).ToList();
238234

239235
foreach (var question in questionsInGroup)
240236
{
241237
// Check True/False branching
242238
if (question is TrueFalseQuestion tfQuestion)
243239
{
244-
if (tfQuestion.BranchToGroupIdOnTrue.HasValue && !reachableGroups.Contains(tfQuestion.BranchToGroupIdOnTrue.Value))
240+
if (tfQuestion.BranchToGroupIdOnTrue.HasValue && !reachableGroupNumbers.Contains(tfQuestion.BranchToGroupIdOnTrue.Value))
245241
{
246-
reachableGroups.Add(tfQuestion.BranchToGroupIdOnTrue.Value);
247-
groupsToCheck.Enqueue(tfQuestion.BranchToGroupIdOnTrue.Value);
242+
reachableGroupNumbers.Add(tfQuestion.BranchToGroupIdOnTrue.Value);
243+
groupNumbersToCheck.Enqueue(tfQuestion.BranchToGroupIdOnTrue.Value);
248244
}
249245

250-
if (tfQuestion.BranchToGroupIdOnFalse.HasValue && !reachableGroups.Contains(tfQuestion.BranchToGroupIdOnFalse.Value))
246+
if (tfQuestion.BranchToGroupIdOnFalse.HasValue && !reachableGroupNumbers.Contains(tfQuestion.BranchToGroupIdOnFalse.Value))
251247
{
252-
reachableGroups.Add(tfQuestion.BranchToGroupIdOnFalse.Value);
253-
groupsToCheck.Enqueue(tfQuestion.BranchToGroupIdOnFalse.Value);
248+
reachableGroupNumbers.Add(tfQuestion.BranchToGroupIdOnFalse.Value);
249+
groupNumbersToCheck.Enqueue(tfQuestion.BranchToGroupIdOnFalse.Value);
254250
}
255251
}
256252
// Check Multiple Choice branching
257253
else if (question is MultipleChoiceQuestion mcQuestion && mcQuestion.Options != null)
258254
{
259255
foreach (var option in mcQuestion.Options)
260256
{
261-
if (option.BranchToGroupId.HasValue && !reachableGroups.Contains(option.BranchToGroupId.Value))
257+
if (option.BranchToGroupId.HasValue && !reachableGroupNumbers.Contains(option.BranchToGroupId.Value))
262258
{
263-
reachableGroups.Add(option.BranchToGroupId.Value);
264-
groupsToCheck.Enqueue(option.BranchToGroupId.Value);
259+
reachableGroupNumbers.Add(option.BranchToGroupId.Value);
260+
groupNumbersToCheck.Enqueue(option.BranchToGroupId.Value);
265261
}
266262
}
267263
}
@@ -270,10 +266,10 @@ public async Task GenerateDemoSurveyResponsesAsync(Survey survey, int numberOfUs
270266
{
271267
foreach (var option in satQuestion.Options)
272268
{
273-
if (option.BranchToGroupId.HasValue && !reachableGroups.Contains(option.BranchToGroupId.Value))
269+
if (option.BranchToGroupId.HasValue && !reachableGroupNumbers.Contains(option.BranchToGroupId.Value))
274270
{
275-
reachableGroups.Add(option.BranchToGroupId.Value);
276-
groupsToCheck.Enqueue(option.BranchToGroupId.Value);
271+
reachableGroupNumbers.Add(option.BranchToGroupId.Value);
272+
groupNumbersToCheck.Enqueue(option.BranchToGroupId.Value);
277273
}
278274
}
279275
}
@@ -282,7 +278,7 @@ public async Task GenerateDemoSurveyResponsesAsync(Survey survey, int numberOfUs
282278

283279
// Find orphaned groups (groups that are not reachable)
284280
var orphanedGroups = survey.QuestionGroups
285-
.Where(g => g.GroupNumber != 0 && !reachableGroups.Contains(g.Id))
281+
.Where(g => g.GroupNumber != 0 && !reachableGroupNumbers.Contains(g.GroupNumber))
286282
.ToList();
287283

288284
if (orphanedGroups.Any())

0 commit comments

Comments
 (0)