Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove importId from Csv Import #1758

Merged
merged 10 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/api/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,55 +201,55 @@ private void ValidateBoreholeImports(int workgroupId, List<BoreholeImport> boreh
.ToList();

// Iterate over provided boreholes, validate them, and create error messages when necessary. Use a non-zero based index for error message keys (e.g. 'Row1').
foreach (var boreholeFromFile in boreholesFromFile.Select((value, index) => (value, index: index + 1)))
var indexedBoreholesFromFile = boreholesFromFile.Select((value, index) => (value, index: index + 1)).ToList();
foreach (var (borehole, index) in indexedBoreholesFromFile)
{
if (string.IsNullOrEmpty(boreholeFromFile.value.OriginalName))
if (string.IsNullOrEmpty(borehole.OriginalName))
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "original_name"));
ModelState.AddModelError($"Row{index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "original_name"));
}

if (boreholeFromFile.value.LocationX == null && boreholeFromFile.value.LocationXLV03 == null)
if (borehole.LocationX == null && borehole.LocationXLV03 == null)
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "location_x"));
ModelState.AddModelError($"Row{index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "location_x"));
}

if (boreholeFromFile.value.LocationY == null && boreholeFromFile.value.LocationYLV03 == null)
if (borehole.LocationY == null && borehole.LocationYLV03 == null)
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "location_y"));
ModelState.AddModelError($"Row{index}", string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "location_y"));
}

// Check if any borehole with same coordinates (in tolerance) and same total depth is duplicated in file
if (boreholesFromFile.Any(b =>
b.ImportId != boreholeFromFile.value.ImportId &&
CompareValuesWithTolerance(b.TotalDepth, boreholeFromFile.value.TotalDepth, 0) &&
CompareValuesWithTolerance(b.LocationX, boreholeFromFile.value.LocationX, 2) &&
CompareValuesWithTolerance(b.LocationY, boreholeFromFile.value.LocationY, 2)))
if (indexedBoreholesFromFile.Any(b =>
b.index != index &&
CompareValuesWithTolerance(b.value.TotalDepth, borehole.TotalDepth, 0) &&
CompareValuesWithTolerance(b.value.LocationX, borehole.LocationX, 2) &&
CompareValuesWithTolerance(b.value.LocationY, borehole.LocationY, 2)))
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", $"Borehole with same Coordinates (+/- 2m) and same {nameof(Borehole.TotalDepth)} is provided multiple times.");
ModelState.AddModelError($"Row{index}", $"Borehole with same Coordinates (+/- 2m) and same {nameof(Borehole.TotalDepth)} is provided multiple times.");
}

// Check if borehole with same coordinates (in tolerance) and same total depth already exists in db.
if (boreholesFromDb.Any(b =>
CompareValuesWithTolerance(b.TotalDepth, boreholeFromFile.value.TotalDepth, 0) &&
(CompareValuesWithTolerance(b.LocationX, boreholeFromFile.value.LocationX, 2) || CompareValuesWithTolerance(b.LocationXLV03, boreholeFromFile.value.LocationX, 2)) &&
(CompareValuesWithTolerance(b.LocationY, boreholeFromFile.value.LocationY, 2) || CompareValuesWithTolerance(b.LocationYLV03, boreholeFromFile.value.LocationY, 2))))
CompareValuesWithTolerance(b.TotalDepth, borehole.TotalDepth, 0) &&
(CompareValuesWithTolerance(b.LocationX, borehole.LocationX, 2) || CompareValuesWithTolerance(b.LocationXLV03, borehole.LocationX, 2)) &&
(CompareValuesWithTolerance(b.LocationY, borehole.LocationY, 2) || CompareValuesWithTolerance(b.LocationYLV03, borehole.LocationY, 2))))
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", $"Borehole with same Coordinates (+/- 2m) and same {nameof(Borehole.TotalDepth)} already exists in database.");
ModelState.AddModelError($"Row{index}", $"Borehole with same Coordinates (+/- 2m) and same {nameof(Borehole.TotalDepth)} already exists in database.");
}

// Checks if each file name in the comma separated string is present in the list of the attachments.
var attachmentFileNamesToLink = boreholeFromFile.value.Attachments?
var attachmentFileNamesToLink = borehole.Attachments?
.Split(",")
.Select(s => s.Replace(" ", "", StringComparison.OrdinalIgnoreCase))
.Where(s => !string.IsNullOrEmpty(s))
.ToList()
?? new List<string>();
.ToList() ?? new List<string>();

foreach (var attachmentFileNameToLink in attachmentFileNamesToLink)
{
if (attachments?.Any(a => a.FileName.Equals(attachmentFileNameToLink, StringComparison.OrdinalIgnoreCase)) == false)
{
ModelState.AddModelError($"Row{boreholeFromFile.index}", $"Attachment file '{attachmentFileNameToLink}' not found.");
ModelState.AddModelError($"Row{index}", $"Attachment file '{attachmentFileNameToLink}' not found.");
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/api/Models/BoreholeImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ public class BoreholeImport : Borehole
/// e.g. "borehole_1.pdf,borehole_2.pdf".
/// </summary>
public string Attachments { get; set; }

/// <summary>
/// Gets or sets the <see cref="BoreholeImport"/>'s import id.
/// </summary>
public int ImportId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import_id;alternate_name;original_name;location_x;location_y
1;BH-1001;Wellington 1;
2;BH-1002;Wellington 2;
3;BH-1003;Wellington 3;2189456;1334567
4;BH-1004;Wellington 4;2312345;1200987
5;BH-1005;Wellington 5;2312345;1200987
alternate_name;original_name;location_x;location_y
BH-1001;Wellington 1;
BH-1002;Wellington 2;
BH-1003;Wellington 3;2189456;1334567
BH-1004;Wellington 4;2312345;1200987
BH-1005;Wellington 5;2312345;1200987
10 changes: 5 additions & 5 deletions src/client/cypress/fixtures/import/boreholes-multiple-valid.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import_id;alternate_name;original_name;location_x;location_y;attachments;
344;BH-1001;Wellington 1;2156784;1154321;borehole_attachment_1.pdf,borehole_attachment_2.zip;
33;BH-1002;Wellington 2;2367999;1276543;
2;BH-1003;Wellington 3;2189456;1334567;
55;BH-1004;Wellington 4;2312345;1200987;
alternate_name;original_name;location_x;location_y;attachments;
BH-1001;Wellington 1;2156784;1154321;borehole_attachment_1.pdf,borehole_attachment_2.zip;
BH-1002;Wellington 2;2367999;1276543;
BH-1003;Wellington 3;2189456;1334567;
BH-1004;Wellington 4;2312345;1200987;

This file was deleted.

2 changes: 0 additions & 2 deletions src/client/cypress/fixtures/import/lithology-single-valid.csv

This file was deleted.

1 change: 0 additions & 1 deletion src/client/docs/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Die zu importierenden Daten müssen gemäss obigen Anforderungen im CSV-Format v

| Feldname | Datentyp | Pflichtfeld | Beschreibung |
| --------------------------- | -------------- | ----------- | ------------------------------------------------------------------------------------- |
| import_id | Zahl | Ja | Zufällig gewählte Zahl. Wird nicht gepeichert. Muss in der Datei einzigartig sein |
| id_geodin_shortname | Zahl | Nein | ID GeODin-Shortname |
| id_info_geol | Zahl | Nein | ID InfoGeol |
| id_original | Zahl | Nein | ID Original |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const ImportModalContent = ({ setSelectedBoreholeAttachments, setSelectedFile, s
<StackHalfWidth direction="column">
{t("csvFormatExplanation")}
{ExampleHeadings(
"import_id;id_geodin_shortname;id_info_geol;id_original;" +
"id_geodin_shortname;id_info_geol;id_original;" +
"id_canton;id_geo_quat;id_geo_mol;id_geo_therm;id_top_fels;" +
"id_geodin;id_kernlager;original_name;project_name;alternate_name;" +
"restriction_id;restriction_until;national_interest;location_x;location_y;" +
Expand Down
9 changes: 4 additions & 5 deletions tests/api/Controllers/UploadControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public async Task UploadBoreholeCsvFileWithoutAttachmentsButWithProvidedFilesSho
.Returns(() => new HttpClient())
.Verifiable();

var boreholeCsvFile = GetFormFileByContent(fileContent: "import_id;original_name;location_x;location_y\r\n123;Frank Place;2000000;1000000", fileName: "boreholes.csv");
var boreholeCsvFile = GetFormFileByContent(fileContent: "original_name;location_x;location_y\r\nFrank Place;2000000;1000000", fileName: "boreholes.csv");
var firstPdfFormFile = GetFormFileByExistingFile("borehole_attachment_1.pdf");
var secondPdfFormFile = GetFormFileByExistingFile("borehole_attachment_2.pdf");

Expand All @@ -458,8 +458,8 @@ public async Task UploadBoreholeCsvFileWithAttachmentsLinkedPdfsShouldCreateBore
var firstAttachmentFileName = "borehole_attachment_1.pdf";
var secondAttachmentFileName = "borehole_attachment_2.pdf";

var pdfContent = @"import_id;original_name;location_x;location_y;attachments
123;Frank Place;2000000;1000000;borehole_attachment_1.pdf,borehole_attachment_2.pdf";
var pdfContent = @"original_name;location_x;location_y;attachments
Frank Place;2000000;1000000;borehole_attachment_1.pdf,borehole_attachment_2.pdf";
var boreholeCsvFile = GetFormFileByContent(fileContent: pdfContent, fileName: "boreholes.csv");
var firstPdfFormFile = GetFormFileByExistingFile(firstAttachmentFileName);
var secondPdfFormFile = GetFormFileByExistingFile(secondAttachmentFileName);
Expand Down Expand Up @@ -588,11 +588,10 @@ public async Task UploadRequiredHeadersMissingShouldReturnError()
ActionResultAssert.IsBadRequest(result);

ProblemDetails problemDetails = (ProblemDetails)result.Value!;
Assert.AreEqual(4, Regex.Matches(problemDetails.Detail!, "Header with name ").Count);
Assert.AreEqual(3, Regex.Matches(problemDetails.Detail!, "Header with name ").Count);
StringAssert.Contains(problemDetails.Detail, "Header with name 'Location_x'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'Location_y'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'OriginalName'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'ImportId'[0] was not found.");
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions tests/api/TestData/borehole_and_location_data.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;municipality;canton;country;location_x;location_y
1;ACORNFLEA;OASIS;DOGLEAK;GOAT-XVI;0;0
original_name;municipality;canton;country;location_x;location_y
ACORNFLEA;OASIS;DOGLEAK;GOAT-XVI;0;0
4 changes: 2 additions & 2 deletions tests/api/TestData/borehole_with_attachments.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y;attachments
123456;ACORNFLEA;0;0;attachment_1.pdf,attachment_2.txt,attachment_3.zip,attachment_4.jpg,attachment_5.csv,borehole_attachment_1.pdf,borehole_attachment_2.pdf,borehole_attachment_3.csv,borehole_attachment_4.zip,borehole_attachment_5.png
original_name;location_x;location_y;attachments
ACORNFLEA;0;0;attachment_1.pdf,attachment_2.txt,attachment_3.zip,attachment_4.jpg,attachment_5.csv,borehole_attachment_1.pdf,borehole_attachment_2.pdf,borehole_attachment_3.csv,borehole_attachment_4.zip,borehole_attachment_5.png
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y;attachments
123456;ACORNFLEA;0;0;Borehole_Attachment_1.pdf,borehole_attachment_2.pdf
original_name;location_x;location_y;attachments
ACORNFLEA;0;0;Borehole_Attachment_1.pdf,borehole_attachment_2.pdf
4 changes: 2 additions & 2 deletions tests/api/TestData/borehole_with_not_present_attachments.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y;attachments
123456;ACORNFLEA;0;0;borehole_attachment_1.pdf,is_not_present_in_upload_files.pdf
original_name;location_x;location_y;attachments
ACORNFLEA;0;0;borehole_attachment_1.pdf,is_not_present_in_upload_files.pdf
8 changes: 4 additions & 4 deletions tests/api/TestData/boreholes_not_all_have_attachments.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import_id;original_name;location_x;location_y;attachments
123;ACORNFLEA;2000000;1000000;borehole_attachment_1.pdf,borehole_attachment_2.pdf
234;BERRYSNAIL;2000010;1000010;
456;BLUEBIRDY;2000020;1000020;;
original_name;location_x;location_y;attachments
ACORNFLEA;2000000;1000000;borehole_attachment_1.pdf,borehole_attachment_2.pdf
BERRYSNAIL;2000010;1000010;
BLUEBIRDY;2000020;1000020;;
8 changes: 4 additions & 4 deletions tests/api/TestData/duplicateBoreholesInDb.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import_id;original_name;location_x;location_y;total_depth
54564;Madrid Perez;2100000;1100000;855;
787;Seoul Park;2500000;1500000;
123;Secret Zack;676700;185081;1000
original_name;location_x;location_y;total_depth
Madrid Perez;2100000;1100000;855;
Seoul Park;2500000;1500000;
Secret Zack;676700;185081;1000
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import_id;original_name;location_x;location_y;total_depth
1;Sophia Lee;2100000;1100000;855;
2;Liam Kim;2500000;1500000;
original_name;location_x;location_y;total_depth
Sophia Lee;2100000;1100000;855;
Liam Kim;2500000;1500000;
6 changes: 3 additions & 3 deletions tests/api/TestData/duplicateBoreholesInFile.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import_id;original_name;location_x;location_y;total_depth
1;Isaac Finn;2100000;1100000;855;
2;Chloe David;2100000;1100000;855;
original_name;location_x;location_y;total_depth
Isaac Finn;2100000;1100000;855;
Chloe David;2100000;1100000;855;
4 changes: 2 additions & 2 deletions tests/api/TestData/lv03_coordinates_provided_testdata.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y
123;Unit_Test_ LV03 - All coordinates set;649258.3612564525;131551.85893587855
original_name;location_x;location_y
Unit_Test_ LV03 - All coordinates set;649258.3612564525;131551.85893587855
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y
456;Unit_Test_ LV03 - LV03 x out of range;999999;-999999
original_name;location_x;location_y
Unit_Test_ LV03 - LV03 x out of range;999999;-999999
4 changes: 2 additions & 2 deletions tests/api/TestData/lv95_coordinates_provided_testdata.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y
33;Unit_Test_ LV95 - All coordinates set;2631690;1170516
original_name;location_x;location_y
Unit_Test_ LV95 - All coordinates set;2631690;1170516
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import_id;original_name;location_x;location_y
1;Unit_Test_ LV95 - LV95 x missing;;1178661
original_name;location_x;location_y
Unit_Test_ LV95 - LV95 x missing;;1178661
Loading
Loading