Skip to content

Commit

Permalink
feat: or-2389 add locaties for power bi export
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy committed Sep 3, 2024
1 parent 50a92e9 commit edf0254
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task UploadListAsCsvToS3(IEnumerable<PowerBiExportDocument> documen
{
(WellKnownFileNames.Hoofdactiviteiten, await exporter.ExportHoofdactiviteiten(documents)),
(WellKnownFileNames.Basisgegevens, await exporter.ExportBasisgegevens(documents)),
(WellKnownFileNames.Locaties, await exporter.ExportLocaties(documents)),
};

foreach (var (fileName, stream) in streamsToWrite)
Expand All @@ -74,7 +75,7 @@ public async Task UploadListAsCsvToS3(IEnumerable<PowerBiExportDocument> documen
ContentType = "text/csv",
};

_logger.LogInformation($"Send file{fileName} to s3.");
_logger.LogInformation($"Send file {fileName} to s3.");
await _s3Client.PutObjectAsync(putRequest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ public async Task<MemoryStream> ExportBasisgegevens(IEnumerable<PowerBiExportDoc
return await CloseCsvAndCopyStream(basisgegevensSetup);
}

public async Task<MemoryStream> ExportLocaties(IEnumerable<PowerBiExportDocument> docs)
{
var basisgegevensSetup = await GetFileSetup<LocatiesRecord>();

foreach (var vereniging in docs)
{
foreach (var locatie in vereniging.Locaties)
{
basisgegevensSetup.CsvWriter.WriteRecord(new LocatiesRecord(
locatie.AdresId?.Broncode,

Check warning on line 75 in src/AssociationRegistry.PowerBi.ExportHost/PowerBiDocumentExporter.cs

View workflow job for this annotation

GitHub Actions / Build PowerBI Export Scheduled Task / build-image

Possible null reference argument for parameter 'AdresIdBroncode' in 'LocatiesRecord.LocatiesRecord(string AdresIdBroncode, string AdresIdBronwaarde, string Adresvoorstelling, string Bron, string Busnummer, string Gemeente, string Huisnummer, bool IsPrimair, string Land, int LocatieId, string LocatieType, string Naam, string PostCode, string Straatnaam, string VCode)'.
locatie.AdresId?.Bronwaarde,

Check warning on line 76 in src/AssociationRegistry.PowerBi.ExportHost/PowerBiDocumentExporter.cs

View workflow job for this annotation

GitHub Actions / Build PowerBI Export Scheduled Task / build-image

Possible null reference argument for parameter 'AdresIdBronwaarde' in 'LocatiesRecord.LocatiesRecord(string AdresIdBroncode, string AdresIdBronwaarde, string Adresvoorstelling, string Bron, string Busnummer, string Gemeente, string Huisnummer, bool IsPrimair, string Land, int LocatieId, string LocatieType, string Naam, string PostCode, string Straatnaam, string VCode)'.
locatie.Adresvoorstelling,
locatie.Bron,
locatie.Adres?.Busnummer,

Check warning on line 79 in src/AssociationRegistry.PowerBi.ExportHost/PowerBiDocumentExporter.cs

View workflow job for this annotation

GitHub Actions / Build PowerBI Export Scheduled Task / build-image

Possible null reference argument for parameter 'Busnummer' in 'LocatiesRecord.LocatiesRecord(string AdresIdBroncode, string AdresIdBronwaarde, string Adresvoorstelling, string Bron, string Busnummer, string Gemeente, string Huisnummer, bool IsPrimair, string Land, int LocatieId, string LocatieType, string Naam, string PostCode, string Straatnaam, string VCode)'.
locatie.Adres?.Gemeente,
locatie.Adres?.Huisnummer,
locatie.IsPrimair,
locatie.Adres?.Land,
locatie.LocatieId,
locatie.Locatietype,
locatie.Naam,
locatie.Adres?.Postcode,
locatie.Adres.Straatnaam,

Check warning on line 88 in src/AssociationRegistry.PowerBi.ExportHost/PowerBiDocumentExporter.cs

View workflow job for this annotation

GitHub Actions / Build PowerBI Export Scheduled Task / build-image

Dereference of a possibly null reference.
vereniging.VCode));

await basisgegevensSetup.CsvWriter.NextRecordAsync();
}
}

return await CloseCsvAndCopyStream(basisgegevensSetup);
}

public record FileSetup(MemoryStream Stream, CsvWriter CsvWriter);

private async Task<FileSetup> GetFileSetup<T>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AssociationRegistry.PowerBi.ExportHost.Records;

using CsvHelper.Configuration.Attributes;

record LocatiesRecord(
[property: Name("adresId.broncode"), Index(0)] string AdresIdBroncode,
[property: Name("adresId.bronwaarde"), Index(1)] string AdresIdBronwaarde,
[property: Name("adresvoorstelling"), Index(2)] string Adresvoorstelling,
[property: Name("bron"), Index(3)] string Bron,
[property: Name("busnummer"), Index(4)] string Busnummer,
[property: Name("gemeente"), Index(5)] string Gemeente,
[property: Name("huisnummer"), Index(6)] string Huisnummer,
[property: Name("isPrimair"), Index(7)] bool IsPrimair,
[property: Name("land"), Index(8)] string Land,
[property: Name("locatieId"), Index(9)] int LocatieId,
[property: Name("locatieType"), Index(10)] string LocatieType,
[property: Name("naam"), Index(11)] string Naam,
[property: Name("postcode"), Index(12)] string PostCode,
[property: Name("straatnaam"), Index(13)] string Straatnaam,
[property: Name("vCode"), Index(14)] string VCode);
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class WellKnownFileNames
{
public const string Hoofdactiviteiten = "Hoofdactiviteiten.csv";
public const string Basisgegevens = "Basisgegevens.csv";
public const string Locaties = "Locaties.csv";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace AssociationRegistry.Test.PowerBi.ExportHost;

using Admin.Schema.PowerBiExport;
using AssociationRegistry.PowerBi.ExportHost;
using AutoFixture;
using Common.AutoFixture;
using FluentAssertions;
using System.Text;
using Xunit;

public class LocatiesExportTests
{
[Fact]
public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
{

var fixture = new Fixture().CustomizeDomain();

var docs = fixture.CreateMany<PowerBiExportDocument>();

var content = await GenerateCsv(docs);
var stringBuilder = new StringBuilder();
stringBuilder.Append("adresId.broncode,adresId.bronwaarde,adresvoorstelling,bron,busnummer,gemeente,huisnummer,isPrimair,land,locatieId,locatieType,naam,postcode,straatnaam,vCode\r\n");

foreach (var doc in docs)
{
foreach (var locatie in doc.Locaties)
{
stringBuilder.Append(
$"{locatie.AdresId?.Broncode},{locatie.AdresId?.Bronwaarde},{locatie.Adresvoorstelling},{locatie.Bron},{locatie.Adres?.Busnummer},{locatie.Adres?.Gemeente},{locatie.Adres?.Huisnummer},{locatie.IsPrimair},{locatie.Adres?.Land},{locatie.LocatieId},{locatie.Locatietype},{locatie.Naam},{locatie.Adres?.Postcode},{locatie.Adres?.Straatnaam},{doc.VCode}\r\n");
}
}
content.Should().BeEquivalentTo(stringBuilder.ToString());
}

private static async Task<string> GenerateCsv(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new PowerBiDocumentExporter();

var exportStream = await exporter.ExportLocaties(docs);

using var reader = new StreamReader(exportStream, Encoding.UTF8);

var content = await reader.ReadToEndAsync();

return content;
}
}


0 comments on commit edf0254

Please sign in to comment.