Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 8, 2024
1 parent b19fd4d commit 5e7fd1f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 59 deletions.
26 changes: 17 additions & 9 deletions lib/api/apis/investment_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ class InvestmentApi {
required final String? description,
required final int? basketId,
required final RiskLevel riskLevel,
required final double value,
required final DateTime valueUpdatedAt,
required final double? currentValue,
required final DateTime? currentValueUpdatedAt,
required final double? irr,
required final DateTime? maturityDate,
}) async {
return _db.into(_db.investmentTable).insert(InvestmentTableCompanion.insert(
name: name,
basketId: Value(basketId),
description: Value(description),
value: value,
riskLevel: riskLevel,
valueUpdatedOn: valueUpdatedAt));
currentValue: Value(currentValue),
irr: Value(irr),
maturityDate: Value(maturityDate),
currentValueUpdatedOn: Value(currentValueUpdatedAt),
riskLevel: riskLevel));
}

Future<int> createTransaction(
Expand Down Expand Up @@ -115,17 +119,21 @@ class InvestmentApi {
required final String? description,
required final int? basketId,
required final RiskLevel riskLevel,
required final double value,
required final DateTime valueUpdatedAt,
required final double? currentValue,
required final double? irr,
required final DateTime? maturityDate,
required final DateTime currentValueUpdatedOn,
}) async {
return (_db.update(_db.investmentTable)..where((t) => t.id.equals(id)))
.write(InvestmentTableCompanion(
name: Value(name),
description: Value(description),
basketId: Value(basketId),
riskLevel: Value(riskLevel),
value: Value(value),
valueUpdatedOn: Value(valueUpdatedAt)));
currentValue: Value(currentValue),
irr: Value(irr),
maturityDate: Value(maturityDate),
currentValueUpdatedOn: Value(currentValueUpdatedOn)));
}

Future<int> updateTransaction(
Expand Down
5 changes: 3 additions & 2 deletions lib/api/db/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class InvestmentTable extends Table {
RealColumn get irr => real().nullable().named('IRR')();

DateTimeColumn get currentValueUpdatedOn =>
dateTime().named('CURRENT_VALUE_UPDATED_ON')();
dateTime().nullable().named('CURRENT_VALUE_UPDATED_ON')();

DateTimeColumn get maturityDate =>
dateTime().nullable().named('MATURITY_DATE')();
Expand Down Expand Up @@ -131,6 +131,7 @@ abstract class InvestmentEnrichedView extends View {
investment.name,
investment.description,
investment.riskLevel,
investment.maturityDate,
investment.irr,
investment.currentValue,
investment.currentValueUpdatedOn,
Expand All @@ -140,7 +141,7 @@ abstract class InvestmentEnrichedView extends View {
totalTransactions,
totalSips
]).from(investment).join([
innerJoin(basket, basket.id.equalsExp(investment.basketId)),
leftOuterJoin(basket, basket.id.equalsExp(investment.basketId)),
leftOuterJoin(
transaction, transaction.investmentId.equalsExp(investment.id)),
leftOuterJoin(sip, sip.investmentId.equalsExp(investment.id)),
Expand Down
55 changes: 27 additions & 28 deletions lib/api/db/app_database.g.dart

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions lib/domain/models/basket.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/domain/models/investment.dart';

class Basket {
final int id;
final String name;
final String? description;
final List<InvestmentDO> investments;
final List<Investment> investments;

Basket(
{required this.id,
Expand All @@ -14,7 +15,7 @@ class Basket {

static Basket from(
{required final BasketDO basket,
required final List<InvestmentDO> investments}) {
required final List<Investment> investments}) {
return Basket(
id: basket.id,
name: basket.name,
Expand All @@ -24,7 +25,7 @@ class Basket {

double get totalValue {
return investments.fold(0, (previousValue, investment) {
return previousValue + investment.value;
return previousValue + investment.totalInvestedAmount;
});
}

Expand Down
8 changes: 2 additions & 6 deletions lib/domain/models/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ class Goal {
required this.importance,
required this.taggedInvestments});

double getIrr() {
double getIrr({bool considerFutureTransactions = false}) {
double totalValue = 0.0;
double weightedSum = 0.0;

for (var investment in taggedInvestments.keys) {
double share = taggedInvestments[investment] ?? 0;
double investedAmount = investment.value * share;
double growthRate = investment.getIrr() ?? 0;

totalValue += investedAmount;
totalValue += investment.getFutureValueOn(date);
weightedSum += investedAmount * growthRate;
}

Expand Down
19 changes: 14 additions & 5 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Investment {
final double? currentValue;
final double? irr;
final DateTime? currentValueUpdatedOn;
final int basketId;
final String basketName;
final DateTime? maturityDate;
final int? basketId;
final String? basketName;
final double totalInvestedAmount;
final int totalTransactions;
final List<Transaction> transactions;
Expand All @@ -32,6 +33,7 @@ class Investment {
required this.currentValueUpdatedOn,
required this.basketId,
required this.basketName,
required this.maturityDate,
required this.totalInvestedAmount,
required this.totalTransactions,
required this.transactions,
Expand All @@ -52,7 +54,7 @@ class Investment {
return null;
}

double getFutureValueOn(DateTime date) {
double getFutureValueOn({required DateTime date, required bool considerFutureTransactions}) {
double? irr = getIrr();
if (currentValue != null) {
if (irr == null) {
Expand All @@ -67,9 +69,14 @@ class Investment {
}
double futureValue = 0;
for (var transaction in transactions) {
//TODO consider maturity date of investment
//TODO filter transaction above the date given.
double years = date.difference(transaction.createdOn).inDays / 365;
futureValue += transaction.amount / pow(1 + irr, years);
}
for(var sip in sips) {
//Include future value of sip.
}

return futureValue;
}
Expand All @@ -86,8 +93,10 @@ class Investment {
name: investment.name,
description: investment.description,
riskLevel: investment.riskLevel,
value: investment.value,
valueUpdatedOn: investment.valueUpdatedOn,
maturityDate: investment.maturityDate,
currentValue: investment.currentValue,
currentValueUpdatedOn: investment.currentValueUpdatedOn,
irr: investment.irr,
basketId: investment.basketId ?? 0,
basketName: investment.basketName ?? '',
totalInvestedAmount: investment.totalInvestedAmount ?? 0,
Expand Down
16 changes: 10 additions & 6 deletions lib/domain/usecases/fetch_baskets_use_case.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import 'package:wealth_wave/api/apis/basket_api.dart';
import 'package:wealth_wave/api/apis/investment_api.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/domain/models/basket.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/usecases/fetch_investments_use_case.dart';

class FetchBasketsUseCase {
final BasketApi _basketApi;
final InvestmentApi _investmentApi;
final FetchInvestmentsUseCase _fetchInvestmentsUseCase;

FetchBasketsUseCase({BasketApi? basketApi, InvestmentApi? investmentApi})
FetchBasketsUseCase(
{BasketApi? basketApi, FetchInvestmentsUseCase? fetchInvestmentsUseCase})
: _basketApi = basketApi ?? BasketApi(),
_investmentApi = investmentApi ?? InvestmentApi();
_fetchInvestmentsUseCase =
fetchInvestmentsUseCase ?? FetchInvestmentsUseCase();

Future<List<Basket>> invoke() async {
List<BasketDO> baskets = await _basketApi.getBaskets();
List<InvestmentDO> investments = await _investmentApi.getInvestments();
List<Investment> investments =
await _fetchInvestmentsUseCase.fetchInvestments();

return baskets.map((basket) {
List<InvestmentDO> investmentsOfBasket = investments
List<Investment> investmentsOfBasket = investments
.where((investment) => investment.basketId == basket.id)
.toList();

Expand Down

0 comments on commit 5e7fd1f

Please sign in to comment.