Skip to content

Commit 23e4c9a

Browse files
CopilotPhantomDave
andauthored
Fix CSV import: populate Name and Currency fields with schema enforcement (#166)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PhantomDave <34485699+PhantomDave@users.noreply.github.com>
1 parent 4eb785f commit 23e4c9a

File tree

7 files changed

+1224
-4
lines changed

7 files changed

+1224
-4
lines changed

.config/dotnet-tools.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"version": 1,
33
"isRoot": true,
4-
"tools": {}
4+
"tools": {
5+
"dotnet-ef": {
6+
"version": "10.0.1",
7+
"commands": [
8+
"dotnet-ef"
9+
],
10+
"rollForward": false
11+
}
12+
}
513
}

PhantomDave.BankTracking.Api/Services/FileImportService.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,30 @@ public IEnumerable<FinanceRecord> FromParsedData(int accountId, ParsedFileData p
277277
record.Description = descriptionColumnValue;
278278
}
279279

280+
if (input.ColumnMappings.TryGetValue("Name", out var nameColumn) && row.TryGetValue(nameColumn, out var nameColumnValue))
281+
{
282+
var trimmedName = nameColumnValue?.Trim() ?? string.Empty;
283+
if (string.IsNullOrWhiteSpace(trimmedName))
284+
{
285+
record.Name = "Untitled";
286+
}
287+
else if (trimmedName.Length > 200)
288+
{
289+
_logger.LogWarning("Name value for row {RowIndex} exceeds 200 characters and will be truncated during import", records.Count + failedCount + 1);
290+
record.Name = trimmedName.Substring(0, 200);
291+
}
292+
else
293+
{
294+
record.Name = trimmedName;
295+
}
296+
}
297+
298+
record.Currency = input.ColumnMappings.TryGetValue("Currency", out var currencyColumn)
299+
&& row.TryGetValue(currencyColumn, out var currencyColumnValue)
300+
&& !string.IsNullOrWhiteSpace(currencyColumnValue)
301+
? NormalizeCurrency(currencyColumnValue)
302+
: "USD";
303+
280304
record.AccountId = accountId;
281305
record.Imported = true;
282306

@@ -303,4 +327,16 @@ public IEnumerable<FinanceRecord> FromParsedData(int accountId, ParsedFileData p
303327

304328
return records;
305329
}
330+
331+
private static string NormalizeCurrency(string currency)
332+
{
333+
var normalized = currency.Trim().ToUpperInvariant();
334+
335+
if (normalized.Length < 1 || normalized.Length > 3)
336+
{
337+
return "USD";
338+
}
339+
340+
return normalized;
341+
}
306342
}

PhantomDave.BankTracking.Data/Context/BankTrackerDbContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ private static void ConfigureFinanceRecord(ModelBuilder modelBuilder)
4545
entity.Property(fr => fr.Id).ValueGeneratedOnAdd();
4646
entity.Property(fr => fr.Amount)
4747
.HasColumnType("numeric(18,2)");
48+
entity.Property(fr => fr.Name)
49+
.IsRequired()
50+
.HasMaxLength(200)
51+
.HasDefaultValue("Untitled");
4852
entity.Property(fr => fr.Currency)
4953
.IsRequired()
50-
.HasMaxLength(3);
54+
.HasMaxLength(3)
55+
.HasDefaultValue("USD");
5156
entity.Property(fr => fr.Description)
5257
.HasMaxLength(500);
5358
entity.HasOne<Account>()

PhantomDave.BankTracking.Data/Migrations/20251211094646_AddRequiredNameAndCurrencyDefaults.Designer.cs

Lines changed: 273 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)