Skip to content

Commit

Permalink
fix: return multiple error messages at once for propose for municipal…
Browse files Browse the repository at this point in the history
…ity merger
  • Loading branch information
rikdepeuter authored and ArneD committed Sep 24, 2024
1 parent cc63aee commit 5cf5823
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(

try
{
var errorMessages = new List<string>();
var records = new List<CsvRecord>();
using (var stream = new MemoryStream())
{
Expand All @@ -84,51 +85,62 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(
{
recordNr++;

var recordErrorMessages = new List<string>();

var oldNisCode = csv.GetField<string>("OUD NIS code");
var oldStreetNamePersistentLocalIdAsString = csv.GetField<string>("OUD straatnaamid");
var newNisCode = csv.GetField<string>("NIEUW NIS code");
var streetName = csv.GetField<string>("NIEUW straatnaam");
var homonymAddition = csv.GetField<string>("NIEUW homoniemtoevoeging");

if (string.IsNullOrWhiteSpace(oldNisCode))
return BadRequest($"OldNisCode is required at record number {recordNr}");
{
recordErrorMessages.Add($"OldNisCode is required at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(oldStreetNamePersistentLocalIdAsString))
return BadRequest($"OldStreetNamePersistentLocalId is required at record number {recordNr}");
{
recordErrorMessages.Add($"OldStreetNamePersistentLocalId is required at record number {recordNr}");
}

if (!int.TryParse(oldStreetNamePersistentLocalIdAsString, out var oldStreetNamePersistentLocalId))
return BadRequest($"OldStreetNamePersistentLocalId is NaN at record number {recordNr}");
if (!string.IsNullOrWhiteSpace(oldStreetNamePersistentLocalIdAsString)
& !int.TryParse(oldStreetNamePersistentLocalIdAsString, out var oldStreetNamePersistentLocalId))
{
recordErrorMessages.Add($"OldStreetNamePersistentLocalId is NaN at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(newNisCode))
return BadRequest($"NisCode is required at record number {recordNr}");

if (newNisCode.Trim() != nisCode)
return BadRequest(
$"NisCode {newNisCode} does not match the provided NisCode {nisCode} at record number {recordNr}");
{
recordErrorMessages.Add($"NisCode is required at record number {recordNr}");
}
else if (newNisCode.Trim() != nisCode)
{
recordErrorMessages.Add($"NisCode {newNisCode} does not match the provided NisCode {nisCode} at record number {recordNr}");
}

if (string.IsNullOrWhiteSpace(streetName))
return BadRequest($"StreetName is required at record number {recordNr}");
{
recordErrorMessages.Add($"StreetName is required at record number {recordNr}");
}

if (recordErrorMessages.Any())
{
errorMessages.AddRange(recordErrorMessages);
continue;
}

records.Add(new CsvRecord
{
OldNisCode = oldNisCode.Trim(),
OldNisCode = oldNisCode!.Trim(),
OldStreetNamePersistentLocalId = oldStreetNamePersistentLocalId,
NisCode = nisCode.Trim(),
StreetName = streetName.Trim(),
StreetName = streetName!.Trim(),
HomonymAddition = string.IsNullOrWhiteSpace(homonymAddition) ? null : homonymAddition.Trim()
});
}
}
}

var streetNamesByNisCodeRecords = records
.GroupBy(x => x.NisCode)
.ToDictionary(
x => x.Key,
y => y.ToList())
.Single()
.Value;

var oldMunicipalities = new List<MunicipalityConsumerItem>();
foreach (var oldMunicipalityNisCode in records.Select(x => x.OldNisCode).Distinct())
{
Expand All @@ -137,12 +149,26 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(

if (oldMunicipality is null)
{
return BadRequest($"No municipality found for {oldMunicipalityNisCode}");
errorMessages.Add($"No municipality found for NisCode '{oldMunicipalityNisCode}'");
continue;
}

oldMunicipalities.Add(oldMunicipality);
}

if (errorMessages.Any())
{
return BadRequest(errorMessages);
}

var streetNamesByNisCodeRecords = records
.GroupBy(x => x.NisCode)
.ToDictionary(
x => x.Key,
y => y.ToList())
.Single()
.Value;

// group by streetname and homonym addition
var streetNamesByNisCode = streetNamesByNisCodeRecords
.GroupBy(x => (x.StreetName, x.HomonymAddition))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void WithNoNisCode_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("NisCode is required at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "NisCode is required at record number 1" });
}

[Fact]
Expand All @@ -86,7 +86,7 @@ public void WithDifferentNisCodeThanRoute_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo($"NisCode 11001 does not match the provided NisCode {nisCode} at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { $"NisCode 11001 does not match the provided NisCode {nisCode} at record number 1" });
}

[Fact]
Expand All @@ -102,7 +102,7 @@ public void WithNoStreetName_ThenReturnsBadRequest()
CancellationToken.None).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("StreetName is required at record number 1");
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "StreetName is required at record number 1" });
}

[Fact]
Expand Down

0 comments on commit 5cf5823

Please sign in to comment.