This commit is contained in:
2026-07-19 12:13:14 +02:00
commit 6337e63a77
46 changed files with 4411 additions and 0 deletions
+437
View File
@@ -0,0 +1,437 @@
using FinlyticAssets.Database;
using FinlyticAssets.Entities;
using FinlyticAssets.Models.DataToObject.TradeRepublic;
using FinlyticCore.Entities.Assets;
using Microsoft.EntityFrameworkCore;
namespace FinlyticAssets.Services;
/// <summary>
/// Defines database operations for managing Trade Republic asset entities.
/// </summary>
public interface IAssetsDbService
{
/// <summary>
/// Retrieves all active assets from the database that have been updated within the last 14 days, including their associated tags.
/// Assets older than 14 days are filtered out as they are considered de-listed or inactive.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of all active <see cref="AssetEntity"/> instances.</returns>
public Task<List<AssetEntity>> GetAllValidAssetsAsync();
/// <summary>
/// Adds a new asset or updates an existing one based on the composite key of ISIN and InstrumentType.
/// </summary>
/// <param name="dtoAsset">The incoming asset data transfer object from the API.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains <c>true</c> if the asset was updated or created; otherwise, <c>false</c>.</returns>
public Task<bool> AddOrUpdateAssetAsync(TradeRepublicAsset dtoAsset);
/// <summary>
/// Batches processing for a collection of asset items, returning the total amount of modified or newly added entries.
/// </summary>
/// <param name="dtoAssets">The collection of incoming asset objects to process.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the count of changed or added entries.</returns>
public Task<int> AddOrUpdateAssetsAsync(IEnumerable<TradeRepublicAsset> dtoAssets);
/// <summary>
/// Retrieves all assets matching a specific International Securities Identification Number (ISIN).
/// Can return multiple entities (e.g., both the stock and the derivative tracking asset for the same ISIN).
/// </summary>
/// <param name="isin">The ISIN value to look up.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of matching <see cref="AssetEntity"/> instances.</returns>
public Task<List<AssetEntity>> GetAssetsByIsinAsync(string isin);
/// <summary>
/// Retrieves all assets matching a specific ISIN only if they have been updated within the last 14 days.
/// Assets older than 14 days are considered de-listed or inactive.
/// </summary>
/// <param name="isin">The ISIN value to look up.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of matching active <see cref="AssetEntity"/> instances.</returns>
public Task<List<AssetEntity>> GetValidAssetsByIsinAsync(string isin);
/// <summary>
/// Scans the database for active assets matching a combined, comma-separated search query.
/// The search applies an AND-logic approach where every extracted keyword must be found within an asset's ISIN, name, or tags.
/// Evaluates local records first and utilizes a targeted JIT-fallback to the external API for any search term formatted as a valid, completely unknown ISIN.
/// </summary>
/// <param name="searchQuery">A comma-separated string containing the keywords, ISINs, or tags to search for (e.g., "Siemens, Medic" or "IE00B4L5Y983, ETF").</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of all affected active <see cref="AssetEntity"/> instances.</returns>
public Task<List<AssetEntity>> FindAffectedActiveAssetsAsync(string searchQuery);
/// <summary>
/// Deletes all asset records associated with a specific ISIN from the database.
/// </summary>
/// <remarks>
/// Use with caution. For standard maintenance and handling de-listed instruments,
/// rely on the 14-day recency filter provided by <see cref="GetValidAssetsByIsinAsync"/> instead of hard deletion.
/// </remarks>
/// <param name="isin">The ISIN value of the assets to remove.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains <c>true</c> if any records were successfully deleted; otherwise, <c>false</c>.</returns>
public Task<bool> DeleteAssetAsync(string isin);
}
/// <inheritdoc />
public class AssetsDbService : IAssetsDbService
{
private readonly AssetsDbContext _context;
private readonly ITradeRepublicService _tradeRepublicService;
private readonly ILogger<AssetsDbService> _logger;
private readonly Random _random = new();
/// <summary>
/// Initializes a new instance of the <see cref="AssetsDbService"/> class.
/// </summary>
public AssetsDbService(AssetsDbContext context, ILogger<AssetsDbService> logger,
ITradeRepublicService tradeRepublicService)
{
_context = context;
_logger = logger;
_tradeRepublicService = tradeRepublicService;
}
/// <inheritdoc />
public async Task<List<AssetEntity>> GetAllValidAssetsAsync()
{
var cutoff = DateTime.UtcNow.AddDays(-14);
var existingEntity = await _context.TradeRepublicAssets
.Include(a => a.Tags)
.Where(a => a.UpdateAt >= cutoff)
.ToListAsync();
return existingEntity;
}
/// <inheritdoc />
public async Task<bool> AddOrUpdateAssetAsync(TradeRepublicAsset dtoAsset)
{
var existingEntity = await _context.TradeRepublicAssets
.Include(a => a.Tags)
.FirstOrDefaultAsync(a => a.Isin == dtoAsset.Isin);
var now = DateTime.UtcNow;
var nextUpdateScheduledAt = await CalculateNextUpdateDateAsync(now);
if (existingEntity == null)
{
_logger.LogDebug("Asset with ISIN {Isin} not found. Mapping and creating a new entity.", dtoAsset.Isin);
var newEntity = MapDtoToEntity(dtoAsset);
newEntity.LastUpdatedAt = now;
newEntity.UpdateAt = nextUpdateScheduledAt;
newEntity.Tags = await MapTagsAsync(dtoAsset.Tags);
await _context.TradeRepublicAssets.AddAsync(newEntity);
await _context.SaveChangesAsync();
return true;
}
_logger.LogDebug("Asset with ISIN {Isin} exists. Merging properties and updating database record.",
dtoAsset.Isin);
existingEntity.Name = dtoAsset.Name;
existingEntity.Type = dtoAsset.Type;
existingEntity.InstrumentCategory = dtoAsset.InstrumentCategory;
existingEntity.HasCfd = dtoAsset.HasCfd;
existingEntity.ImageId = dtoAsset.ImageId;
existingEntity.LastUpdatedAt = now;
existingEntity.UpdateAt = nextUpdateScheduledAt;
UpdateSubtypeProperties(existingEntity, dtoAsset);
existingEntity.Tags = await MapTagsAsync(dtoAsset.Tags);
_context.TradeRepublicAssets.Update(existingEntity);
await _context.SaveChangesAsync();
return true;
}
/// <inheritdoc />
public async Task<int> AddOrUpdateAssetsAsync(IEnumerable<TradeRepublicAsset> dtoAssets)
{
int changedCount = 0;
foreach (var dto in dtoAssets)
{
var isChanged = await AddOrUpdateAssetAsync(dto);
if (isChanged)
{
changedCount++;
}
}
return changedCount;
}
/// <inheritdoc />
public async Task<List<AssetEntity>> GetAssetsByIsinAsync(string isin)
{
var localAssets = await _context.TradeRepublicAssets
.Include(a => a.Tags)
.Where(a => a.Isin == isin)
.ToListAsync();
if (localAssets.Any())
{
return localAssets;
}
var trAssetDto = await _tradeRepublicService.GetAsset(isin);
if (trAssetDto != null)
{
foreach (var asset in trAssetDto.Results)
{
_ = await AddOrUpdateAssetAsync(asset);
}
return await GetAssetsByIsinAsync(isin);
}
return [];
}
/// <inheritdoc />
public async Task<List<AssetEntity>> GetValidAssetsByIsinAsync(string isin)
{
var cutoff = DateTime.UtcNow.AddDays(-14);
return await _context.TradeRepublicAssets
.AsNoTracking()
.Include(a => a.Tags)
.Where(a => a.Isin == isin && a.UpdateAt >= cutoff)
.ToListAsync();
}
/// <inheritdoc />
public async Task<List<AssetEntity>> FindAffectedActiveAssetsAsync(string searchQuery)
{
if (string.IsNullOrWhiteSpace(searchQuery))
{
return [];
}
var cutoff = DateTime.UtcNow.AddDays(-14);
var searchTerms = searchQuery
.Split(',')
.Select(t => t.Trim().ToLower())
.Where(t => !string.IsNullOrEmpty(t))
.Distinct()
.ToList();
if (searchTerms.Count == 0) return [];
var query = _context.TradeRepublicAssets
.AsNoTracking()
.Where(a => a.UpdateAt >= cutoff)
.Include(a => a.Tags)
.AsQueryable();
foreach (var term in searchTerms)
{
query = query.Where(a =>
a.Isin.ToLower().Contains(term) ||
a.Name.ToLower().Contains(term) ||
a.Tags.Any(tag => tag.Name.ToLower().Contains(term)));
}
var localAssets = await query.ToListAsync();
var possibleIsins = searchTerms
.Where(t => t.Length == 12 && char.IsLetter(t[0]) && char.IsLetter(t[1]))
.Select(t => t.ToUpper())
.ToList();
if (possibleIsins.Any())
{
var foundIsins = localAssets.Select(a => a.Isin).ToHashSet();
var missingIsins = possibleIsins.Where(isin => !foundIsins.Contains(isin)).ToList();
if (missingIsins.Any())
{
var fetchedNewAsset = false;
foreach (var missingIsin in missingIsins)
{
var trAssetDto = await _tradeRepublicService.GetAsset(missingIsin);
if (trAssetDto?.Results != null)
{
foreach (var asset in trAssetDto.Results)
{
await AddOrUpdateAssetAsync(asset);
}
fetchedNewAsset = true;
}
}
if (fetchedNewAsset)
{
return await query.ToListAsync();
}
}
}
return localAssets;
}
/// <inheritdoc />
public async Task<bool> DeleteAssetAsync(string isin)
{
var asset = await _context.TradeRepublicAssets.FirstOrDefaultAsync(a => a.Isin == isin);
if (asset == null)
{
_logger.LogWarning("Delete execution cancelled. Asset with ISIN {Isin} does not exist.", isin);
return false;
}
_context.TradeRepublicAssets.Remove(asset);
await _context.SaveChangesAsync();
_logger.LogInformation("Asset with ISIN {Isin} has been successfully deleted.", isin);
return true;
}
#region Helper & Mapping Methods
/// <summary>
/// Calculates the next synchronization/update date for an asset using configured parameters and a random offset.
/// </summary>
/// <param name="baseDate">The baseline date to add the offsets to.</param>
/// <returns>A task representing the asynchronous operation, returning a DateTime representing the next scheduled update time (UTC).</returns>
private async Task<DateTime> CalculateNextUpdateDateAsync(DateTime baseDate)
{
var settings = await _context.Set<Settings>().FirstOrDefaultAsync() ?? new Settings();
int randomDays = _random.Next(settings.MinRandomUpdateDay, settings.MaxRandomUpdateDay + 1);
var targetDate = baseDate.AddDays(randomDays);
int randomHour = _random.Next(settings.UpdateDayTimeStart, settings.UpdateDayTimeStop);
int randomMinute = _random.Next(0, 60);
int randomSecond = _random.Next(0, 60);
return new DateTime(
targetDate.Year,
targetDate.Month,
targetDate.Day,
randomHour,
randomMinute,
randomSecond,
DateTimeKind.Utc
);
}
/// <summary>
/// Maps a raw Trade Republic asset data transfer object (DTO) to its matching database entity subtype.
/// </summary>
/// <param name="dto">The source Trade Republic asset data transfer object.</param>
/// <returns>A newly created subtype instance of <see cref="AssetEntity"/> mapped with the DTO properties.</returns>
/// <exception cref="NotSupportedException">Thrown when the DTO type is unrecognized or unsupported.</exception>
private AssetEntity MapDtoToEntity(TradeRepublicAsset dto)
{
AssetEntity entity = dto switch
{
TradeRepublicStock stock => new StockEntity
{ Isin = stock.Isin, DerivativeProductCategories = stock.DerivativeProductCategories.ToList() },
TradeRepublicCrypto crypto => new CryptoEntity
{ Isin = crypto.Isin, Subtitle = crypto.Subtitle, SearchSubtitle = crypto.SearchSubtitle },
TradeRepublicEtf etf => new EtfEntity
{
Isin = etf.Isin, EtfDescription = etf.EtfDescription, MappedEtfIndexName = etf.MappedEtfIndexName,
Subtitle = etf.Subtitle, SearchSubtitle = etf.SearchSubtitle,
DerivativeProductCategories = etf.DerivativeProductCategories.ToList()
},
TradeRepublicSynthetic synth => new SyntheticEntity
{ Isin = synth.Isin, DerivativeProductCategories = synth.DerivativeProductCategories.ToList() },
TradeRepublicBond bond => new BondEntity
{ Isin = bond.Isin, BondIssuerName = bond.BondIssuerName, SearchSubtitle = bond.SearchSubtitle },
TradeRepublicDerivative deriv => new DerivativeEntity
{
Isin = deriv.Isin, UnderlyingIsin = deriv.UnderlyingIsin,
DerivativeProductCategories = deriv.DerivativeProductCategories.ToList()
},
_ => throw new NotSupportedException($"Type {dto.GetType().Name} is not supported.")
};
return PopulateBaseProperties(entity, dto);
}
/// <summary>
/// Populates common base properties of a database asset entity using a Trade Republic DTO.
/// </summary>
/// <param name="entity">The target database entity.</param>
/// <param name="dto">The source Trade Republic DTO.</param>
/// <returns>The updated database asset entity.</returns>
private AssetEntity PopulateBaseProperties(AssetEntity entity, TradeRepublicAsset dto)
{
entity.Name = dto.Name;
entity.Type = dto.Type;
entity.InstrumentCategory = dto.InstrumentCategory;
entity.HasCfd = dto.HasCfd;
entity.ImageId = dto.ImageId;
return entity;
}
/// <summary>
/// Merges/updates the subtype-specific properties from a Trade Republic DTO into an existing database entity.
/// </summary>
/// <param name="entity">The existing database entity to update.</param>
/// <param name="dto">The source Trade Republic DTO.</param>
private void UpdateSubtypeProperties(AssetEntity entity, TradeRepublicAsset dto)
{
switch (entity)
{
case StockEntity stockEntity when dto is TradeRepublicStock stockDto:
stockEntity.DerivativeProductCategories = stockDto.DerivativeProductCategories.ToList();
break;
case CryptoEntity cryptoEntity when dto is TradeRepublicCrypto cryptoDto:
cryptoEntity.Subtitle = cryptoDto.Subtitle;
cryptoEntity.SearchSubtitle = cryptoDto.SearchSubtitle;
break;
case EtfEntity etfEntity when dto is TradeRepublicEtf etfDto:
etfEntity.DerivativeProductCategories = etfDto.DerivativeProductCategories.ToList();
etfEntity.EtfDescription = etfDto.EtfDescription;
etfEntity.MappedEtfIndexName = etfDto.MappedEtfIndexName;
etfEntity.Subtitle = etfDto.Subtitle;
etfEntity.SearchSubtitle = etfDto.SearchSubtitle;
break;
case SyntheticEntity synthEntity when dto is TradeRepublicSynthetic synthDto:
synthEntity.DerivativeProductCategories = synthDto.DerivativeProductCategories.ToList();
break;
case BondEntity bondEntity when dto is TradeRepublicBond bondDto:
bondEntity.BondIssuerName = bondDto.BondIssuerName;
bondEntity.SearchSubtitle = bondDto.SearchSubtitle;
break;
case DerivativeEntity derivEntity when dto is TradeRepublicDerivative derivDto:
derivEntity.DerivativeProductCategories = derivDto.DerivativeProductCategories.ToList();
derivEntity.UnderlyingIsin = derivDto.UnderlyingIsin;
break;
}
}
/// <summary>
/// Maps a list of Trade Republic tags to database entity instances, registering new tags in the database context if they do not yet exist.
/// </summary>
/// <param name="dtos">The read-only collection of Trade Republic tags.</param>
/// <returns>A task representing the asynchronous operation, returning the list of mapped database tag entities.</returns>
private async Task<List<TagEntity>> MapTagsAsync(IReadOnlyList<TradeRepublicTag> dtos)
{
var tags = new List<TagEntity>();
foreach (var tagDto in dtos)
{
var existingTag = await _context.TradeRepublicTags.FirstOrDefaultAsync(t => t.Id == tagDto.Id);
if (existingTag == null)
{
_logger.LogTrace("Creating missing asset tag object in storage cache context. ID: {TagId}", tagDto.Id);
existingTag = new TagEntity { Id = tagDto.Id, Name = tagDto.Name, Type = tagDto.Type };
await _context.TradeRepublicTags.AddAsync(existingTag);
}
tags.Add(existingTag);
}
return tags;
}
#endregion
}