Skip to content

Commit f2b7312

Browse files
Merge pull request #51 from StevanFreeborn/stevanfreeborn/fix/50-address-list-values-being-added-to-user-status-field
fix: don't add values to user status field
2 parents c73de41 + 9d3dc13 commit f2b7312

2 files changed

Lines changed: 137 additions & 31 deletions

File tree

src/Models/Processor.cs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -441,47 +441,52 @@ List<T> azureObjects
441441
{
442442
var field = listFields.FirstOrDefault(f => f.Id == kvp.Key);
443443

444-
foreach (var azureObject in azureObjects)
444+
if (field?.Id == _settings.Onspring.UsersStatusFieldId)
445445
{
446-
if (azureObject is null)
446+
continue;
447+
}
448+
449+
foreach (var azureObject in azureObjects)
447450
{
448-
continue;
449-
}
451+
if (azureObject is null)
452+
{
453+
continue;
454+
}
450455

451-
var propertyValue = azureObject
452-
.GetType()
453-
.GetProperty(
454-
kvp.Value,
455-
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance
456-
)
457-
?.GetValue(azureObject);
456+
var propertyValue = azureObject
457+
.GetType()
458+
.GetProperty(
459+
kvp.Value,
460+
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance
461+
)
462+
?.GetValue(azureObject);
458463

459-
var possibleNewListValues = new List<string?>();
464+
var possibleNewListValues = new List<string?>();
460465

461-
if (propertyValue is null)
462-
{
463-
continue;
464-
}
466+
if (propertyValue is null)
467+
{
468+
continue;
469+
}
465470

466-
if (propertyValue is List<string> propertyValueList)
467-
{
468-
possibleNewListValues.AddRange(propertyValueList);
469-
}
470-
else
471-
{
472-
var propertyValueString = propertyValue.ToString();
471+
if (propertyValue is List<string> propertyValueList)
472+
{
473+
possibleNewListValues.AddRange(propertyValueList);
474+
}
475+
else
476+
{
477+
var propertyValueString = propertyValue.ToString();
473478

474-
possibleNewListValues.Add(propertyValueString);
475-
}
479+
possibleNewListValues.Add(propertyValueString);
480+
}
476481

477-
foreach (var possibleNewListValue in possibleNewListValues)
478-
{
479-
if (TryGetNewListValue(field, possibleNewListValue, out var newListValue))
482+
foreach (var possibleNewListValue in possibleNewListValues)
480483
{
481-
newListValues.Add(newListValue);
484+
if (TryGetNewListValue(field, possibleNewListValue, out var newListValue))
485+
{
486+
newListValues.Add(newListValue);
487+
}
482488
}
483489
}
484-
}
485490
}
486491

487492
newListValues = [.. newListValues.DistinctBy(kvp => kvp.Value.ToLower(CultureInfo.InvariantCulture))];
@@ -514,7 +519,6 @@ internal static bool TryGetNewListValue(
514519
out KeyValuePair<int, string> newListValue
515520
)
516521
{
517-
518522
if (
519523
possibleNewListValue is null ||
520524
listField is null

tests/UnitTests/ProcessorTests.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,108 @@ public void TryGetNewListValue_WhenCalledAndFieldIsNull_ItShouldReturnFalseAndSe
23712371
newListValue.Value.Should().BeNull();
23722372
}
23732373

2374+
[Fact]
2375+
public async Task SyncListValues_WhenCalled_ItShouldAddPropertyValuesToUserStatusFieldList()
2376+
{
2377+
var userStatusFieldId = 1;
2378+
2379+
var onspringSettings = new OnspringSettings()
2380+
{
2381+
UsersStatusFieldId = userStatusFieldId,
2382+
};
2383+
2384+
_settingsMock
2385+
.SetupGet(static x => x.Onspring)
2386+
.Returns(onspringSettings);
2387+
2388+
_settingsMock
2389+
.SetupGet(static x => x.Azure)
2390+
.Returns(new AzureSettings());
2391+
2392+
var listFields = new List<ListField>
2393+
{
2394+
new()
2395+
{
2396+
Id = userStatusFieldId,
2397+
AppId = 1,
2398+
Name = "List Field",
2399+
Type = FieldType.List,
2400+
Status = FieldStatus.Enabled,
2401+
IsRequired = true,
2402+
IsUnique = true,
2403+
Multiplicity = Multiplicity.MultiSelect,
2404+
ListId = 1,
2405+
Values = [
2406+
new ListValue
2407+
{
2408+
Id = Guid.NewGuid(),
2409+
Name = "Active"
2410+
}
2411+
],
2412+
},
2413+
};
2414+
2415+
var fieldMappings = new Dictionary<int, string>
2416+
{
2417+
{ userStatusFieldId, "accountEnabled" },
2418+
};
2419+
2420+
var groups = new List<User?>
2421+
{
2422+
new()
2423+
{
2424+
Id = "user id",
2425+
DisplayName = "user Id",
2426+
AccountEnabled = false,
2427+
},
2428+
};
2429+
2430+
var updatedListFields = new List<Field>
2431+
{
2432+
new ListField()
2433+
{
2434+
Id = userStatusFieldId,
2435+
AppId = 1,
2436+
Name = "List Field",
2437+
Type = FieldType.List,
2438+
Status = FieldStatus.Enabled,
2439+
IsRequired = true,
2440+
IsUnique = true,
2441+
Multiplicity = Multiplicity.MultiSelect,
2442+
ListId = 1,
2443+
Values = [
2444+
new ListValue
2445+
{
2446+
Id = Guid.NewGuid(),
2447+
Name = "group type 1"
2448+
}
2449+
],
2450+
},
2451+
};
2452+
2453+
_onspringServiceMock
2454+
.Setup(static x => x.AddListValue(It.IsAny<int>(), It.IsAny<string>()))
2455+
.ReturnsAsync(new SaveListItemResponse(Guid.NewGuid()));
2456+
2457+
_onspringServiceMock
2458+
.Setup(static x => x.GetGroupFields())
2459+
.ReturnsAsync(updatedListFields);
2460+
2461+
await _processor.SyncListValues(
2462+
listFields,
2463+
fieldMappings,
2464+
groups
2465+
);
2466+
2467+
_onspringServiceMock.Verify(
2468+
static x => x.AddListValue(
2469+
It.IsAny<int>(),
2470+
It.IsAny<string>()
2471+
),
2472+
Times.Never
2473+
);
2474+
}
2475+
23742476
[Fact]
23752477
public async Task SyncListValues_WhenCalledAndNewValuesAreFoundForGroupsListFields_ItShouldAddTheListValuesAndUpdateGroupFields()
23762478
{

0 commit comments

Comments
 (0)