class SecurityDtoToModelMapper @Inject constructor(
private val securityDataProvider: SecurityDataProvider,
private val securityAssetClassDtoToModelMapper: SecurityAssetClassDtoToModelMapper,
private val securityDividendPolicyDtoToModelMapper: SecurityDividendPolicyDtoToModelMapper,
private val securityEsgRatingDtoToModelMapper: SecurityEsgRatingDtoToModelMapper,
private val securityTopHoldingDtoToModelMapper: SecurityTopHoldingDtoToModelMapper,
private val securityPerformancePointDtoToModelMapper: SecurityPerformancePointDtoToModelMapper,
private val securityTagsDtoToModelMapper: SecurityTagsDtoToModelMapper,
private val currencyDtoToModelMapper: CurrencyDtoToModelMapper,
) : Mapper<SecurityDto, Security> {
override suspend fun invoke(input: SecurityDto): Security {
val isin = input.isin ?: mappingTerminalError("null ISIN")
val securitiesData = securityDataProvider.getSecurityData(isin)
return Security(
isin = isin,
name = securitiesData?.name ?: isin,
assetClass = securitiesData?.assetClass?.let { securityAssetClassDtoToModelMapper(it) } ?: SecurityAssetClass.Stock,
instrument = SecurityInstrument.ETF,
currency = securitiesData?.currency?.let { currencyDtoToModelMapper(it) } ?: Currency.EUR,
dividendPolicy = securitiesData?.dividendPolicy?.let { securityDividendPolicyDtoToModelMapper(it) } ?: SecurityDividendPolicy.None,
riskClass = securitiesData?.riskClass?.let { SecurityRiskClass(it) } ?: SecurityRiskClass(7),
esgRating = securitiesData?.esgRating?.let { securityEsgRatingDtoToModelMapper(it) } ?: SecurityEsgRating.Unknown,
topHoldings = securitiesData?.topHoldings?.let { ListMapper(securityTopHoldingDtoToModelMapper)(it) } ?: emptyList(),
historicalPerformance = securitiesData?.historicalPerformance?.let { ListMapper(securityPerformancePointDtoToModelMapper)(it) } ?: emptyList(),
yearlyCost = securitiesData?.yearlyCost ?: 0.0,
tags = securitiesData?.tags?.let { securityTagsDtoToModelMapper(it) } ?: emptyList(),
updatedAt = securitiesData?.updatedAt ?: Date(0),
enabled = input.enabled ?: mappingErrorReturnDefault("enabled was null") { false },
)
}
}
class SecurityDtoToModelMapper @Inject constructor(
private val getSecurityUseCase: GetSecurityUseCase,
) : Mapper<SecurityDto, Security> {
override suspend fun invoke(input: SecurityDto): Security {
val isin = input.isin ?: mappingTerminalError("null ISIN")
return getSecurityUseCase(GetSecurityUseCase.Params(isin)).get()
}
}