fix(assets): safeguard against null InstrumentCategory during ETF/Fund scans

This commit is contained in:
2026-09-01 17:38:46 +02:00
parent d161efb370
commit 2fe8cc0dca
2 changed files with 22 additions and 7 deletions
+15 -6
View File
@@ -221,8 +221,10 @@ public class AssetsDbService : IAssetsDbService
HasSubtypeChanges(existingEntity, dto)) HasSubtypeChanges(existingEntity, dto))
{ {
existingEntity.Name = dto.Name; existingEntity.Name = dto.Name;
existingEntity.Type = dto.Type; existingEntity.Type = dto.Type ?? existingEntity.Type ?? "stock";
existingEntity.InstrumentCategory = dto.InstrumentCategory; existingEntity.InstrumentCategory = !string.IsNullOrWhiteSpace(dto.InstrumentCategory)
? dto.InstrumentCategory
: (existingEntity.InstrumentCategory ?? dto.Type ?? "stock");
existingEntity.HasCfd = dto.HasCfd; existingEntity.HasCfd = dto.HasCfd;
existingEntity.LastUpdatedAt = now; existingEntity.LastUpdatedAt = now;
existingEntity.Tags = mappedTags; existingEntity.Tags = mappedTags;
@@ -242,6 +244,13 @@ public class AssetsDbService : IAssetsDbService
private static AssetEntity MapDtoToEntity(TradeRepublicAsset dto) private static AssetEntity MapDtoToEntity(TradeRepublicAsset dto)
{ {
static string ResolveCategory(string? category, string? fallbackType, string defaultCategory)
{
if (!string.IsNullOrWhiteSpace(category)) return category;
if (!string.IsNullOrWhiteSpace(fallbackType)) return fallbackType;
return defaultCategory;
}
return dto switch return dto switch
{ {
TradeRepublicStock stock => new StockEntity TradeRepublicStock stock => new StockEntity
@@ -249,7 +258,7 @@ public class AssetsDbService : IAssetsDbService
Isin = stock.Isin, Isin = stock.Isin,
Name = stock.Name, Name = stock.Name,
Type = stock.Type, Type = stock.Type,
InstrumentCategory = stock.InstrumentCategory, InstrumentCategory = ResolveCategory(stock.InstrumentCategory, stock.Type, "stock"),
HasCfd = stock.HasCfd, HasCfd = stock.HasCfd,
DerivativeProductCategories = stock.DerivativeProductCategories?.ToList() ?? new List<string>() DerivativeProductCategories = stock.DerivativeProductCategories?.ToList() ?? new List<string>()
}, },
@@ -258,7 +267,7 @@ public class AssetsDbService : IAssetsDbService
Isin = etf.Isin, Isin = etf.Isin,
Name = etf.Name, Name = etf.Name,
Type = etf.Type, Type = etf.Type,
InstrumentCategory = etf.InstrumentCategory, InstrumentCategory = ResolveCategory(etf.InstrumentCategory, etf.Type, "fund"),
HasCfd = etf.HasCfd, HasCfd = etf.HasCfd,
DerivativeProductCategories = etf.DerivativeProductCategories?.ToList() ?? new List<string>() DerivativeProductCategories = etf.DerivativeProductCategories?.ToList() ?? new List<string>()
}, },
@@ -267,7 +276,7 @@ public class AssetsDbService : IAssetsDbService
Isin = syn.Isin, Isin = syn.Isin,
Name = syn.Name, Name = syn.Name,
Type = syn.Type, Type = syn.Type,
InstrumentCategory = syn.InstrumentCategory, InstrumentCategory = ResolveCategory(syn.InstrumentCategory, syn.Type, "synthetic"),
HasCfd = syn.HasCfd, HasCfd = syn.HasCfd,
DerivativeProductCategories = syn.DerivativeProductCategories?.ToList() ?? new List<string>() DerivativeProductCategories = syn.DerivativeProductCategories?.ToList() ?? new List<string>()
}, },
@@ -276,7 +285,7 @@ public class AssetsDbService : IAssetsDbService
Isin = dto.Isin, Isin = dto.Isin,
Name = dto.Name, Name = dto.Name,
Type = dto.Type, Type = dto.Type,
InstrumentCategory = dto.InstrumentCategory, InstrumentCategory = ResolveCategory(dto.InstrumentCategory, dto.Type, "stock"),
HasCfd = dto.HasCfd HasCfd = dto.HasCfd
} }
}; };
@@ -27,7 +27,13 @@ public record TradeRepublicAsset
[JsonPropertyName("isin")] public string Isin { get; init; } = ""; [JsonPropertyName("isin")] public string Isin { get; init; } = "";
[JsonPropertyName("name")] public string Name { get; init; } = ""; [JsonPropertyName("name")] public string Name { get; init; } = "";
[JsonPropertyName("type")] public string Type { get; init; } = ""; [JsonPropertyName("type")] public string Type { get; init; } = "";
[JsonPropertyName("instrumentCategory")] public string InstrumentCategory { get; init; } = ""; private readonly string _instrumentCategory = "";
[JsonPropertyName("instrumentCategory")]
public string InstrumentCategory
{
get => _instrumentCategory;
init => _instrumentCategory = value ?? "";
}
[JsonPropertyName("hasCfd")] public bool HasCfd { get; init; } [JsonPropertyName("hasCfd")] public bool HasCfd { get; init; }
[JsonPropertyName("imageId")] public string? ImageId { get; init; } [JsonPropertyName("imageId")] public string? ImageId { get; init; }